diff --git a/2022/13/part1.js b/2022/13/part1.js index 5289c93..c19ce7c 100644 --- a/2022/13/part1.js +++ b/2022/13/part1.js @@ -7,40 +7,36 @@ const readInput = (...args) => readFile(...args), (lines) => lines.split("\n\n"), R.compact, // remove last line - R.map( - block => block.split("\n").map( line => eval(line) ) - ) + R.map((block) => block.split("\n").map((line) => eval(line))) ); export const puzzleInput = readInput(import.meta.url, "input"); export const sample = readInput(import.meta.url, "sample"); -export function isLessThan(x,y) { - if( [x,y].every(R.isNumber) ) return x < y ? -1 : x == y ? 0 : 1; +export function isLessThan(x, y) { + if ([x, y].every(R.isNumber)) return x < y ? -1 : x == y ? 0 : 1; - if( [x,y].every(R.isArray) ) { - for ( const [a,b] of R.zip(x,y)) { - const res = isLessThan(a,b); - if(res !== 0) return res; - } - if( x.length === y.length ) return 0 - return x.length < y.length ? -1 : 1; + if ([x, y].every(R.isArray)) { + for (const [a, b] of R.zip(x, y)) { + const res = isLessThan(a, b); + if (res !== 0) return res; } + if (x.length === y.length) return 0; + return x.length < y.length ? -1 : 1; + } - return isLessThan(...[x,y].map( z => R.isNumber(z)? [z] : z )); + return isLessThan(...[x, y].map((z) => (R.isNumber(z) ? [z] : z))); } const passthru = (x) => (arg) => { - x(arg); - return arg; -} + x(arg); + return arg; +}; export default R.createPipe( - R.map( ([x,y]) => isLessThan(x,y) ), - (results) => results.map( - (value,index) => ({ value, index: index+1 }) - ), - R.filter( ({ value }) => value !== 1 ), - R.map( R.prop('index') ), - (results) => results.reduce((a,b) => a+b) + R.map(([x, y]) => isLessThan(x, y)), + (results) => results.map((value, index) => ({ value, index: index + 1 })), + R.filter(({ value }) => value !== 1), + R.map(R.prop("index")), + (results) => results.reduce((a, b) => a + b) ); diff --git a/2022/13/part2.js b/2022/13/part2.js index 328da14..79f8c62 100644 --- a/2022/13/part2.js +++ b/2022/13/part2.js @@ -1,13 +1,14 @@ import * as R from "remeda"; import { isLessThan } from "./part1"; -const dividers = [[[2]],[[6]]]; +const dividers = [[[2]], [[6]]]; export default R.createPipe( - R.flatten, - R.concat(dividers), - (packets) => packets.sort(isLessThan), - packets => packets.map( (p,i) => ({ i: i+1, divider: dividers.includes(p) }) ), - R.filter( ({divider}) => divider ), - R.map(R.prop('i')), - (v) => v.reduce((a,b)=>a*b), + R.flatten, + R.concat(dividers), + (packets) => packets.sort(isLessThan), + (packets) => + packets.map((p, i) => ({ i: i + 1, divider: dividers.includes(p) })), + R.filter(({ divider }) => divider), + R.map(R.prop("i")), + (v) => v.reduce((a, b) => a * b) ); diff --git a/2022/13/puzzle.md b/2022/13/puzzle.md index 0f922e7..0948392 100644 --- a/2022/13/puzzle.md +++ b/2022/13/puzzle.md @@ -1,11 +1,10 @@ -\--- Day 13: Distress Signal --- ----------- +## \--- Day 13: Distress Signal --- -You climb the hill and again try contacting the Elves. However, you instead receive a signal you weren't expecting: a *distress signal*. +You climb the hill and again try contacting the Elves. However, you instead receive a signal you weren't expecting: a _distress signal_. -Your handheld device must still not be working properly; the packets from the distress signal got decoded *out of order*. You'll need to re-order the list of received packets (your puzzle input) to decode the message. +Your handheld device must still not be working properly; the packets from the distress signal got decoded _out of order_. You'll need to re-order the list of received packets (your puzzle input) to decode the message. -Your list consists of pairs of packets; pairs are separated by a blank line. You need to identify *how many pairs of packets are in the right order*. +Your list consists of pairs of packets; pairs are separated by a blank line. You need to identify _how many pairs of packets are in the right order_. For example: @@ -38,11 +37,11 @@ For example: Packet data consists of lists and integers. Each list starts with `[`, ends with `]`, and contains zero or more comma-separated values (either integers or other lists). Each packet is always a list and appears on its own line. -When comparing two values, the first value is called *left* and the second value is called *right*. Then: +When comparing two values, the first value is called _left_ and the second value is called _right_. Then: -* If *both values are integers*, the *lower integer* should come first. If the left integer is lower than the right integer, the inputs are in the right order. If the left integer is higher than the right integer, the inputs are not in the right order. Otherwise, the inputs are the same integer; continue checking the next part of the input. -* If *both values are lists*, compare the first value of each list, then the second value, and so on. If the left list runs out of items first, the inputs are in the right order. If the right list runs out of items first, the inputs are not in the right order. If the lists are the same length and no comparison makes a decision about the order, continue checking the next part of the input. -* If *exactly one value is an integer*, convert the integer to a list which contains that integer as its only value, then retry the comparison. For example, if comparing `[0,0,0]` and `2`, convert the right value to `[2]` (a list containing `2`); the result is then found by instead comparing `[0,0,0]` and `[2]`. +- If _both values are integers_, the _lower integer_ should come first. If the left integer is lower than the right integer, the inputs are in the right order. If the left integer is higher than the right integer, the inputs are not in the right order. Otherwise, the inputs are the same integer; continue checking the next part of the input. +- If _both values are lists_, compare the first value of each list, then the second value, and so on. If the left list runs out of items first, the inputs are in the right order. If the right list runs out of items first, the inputs are not in the right order. If the lists are the same length and no comparison makes a decision about the order, continue checking the next part of the input. +- If _exactly one value is an integer_, convert the integer to a list which contains that integer as its only value, then retry the comparison. For example, if comparing `[0,0,0]` and `2`, convert the right value to `[2]` (a list containing `2`); the result is then found by instead comparing `[0,0,0]` and `[2]`. Using these rules, you can determine which of the pairs in the example are in the right order: @@ -114,12 +113,12 @@ Using these rules, you can determine which of the pairs in the example are in th ``` -What are the indices of the pairs that are already *in the right order*? (The first pair has index 1, the second pair has index 2, and so on.) In the above example, the pairs in the right order are 1, 2, 4, and 6; the sum of these indices is `*13*`. +What are the indices of the pairs that are already _in the right order_? (The first pair has index 1, the second pair has index 2, and so on.) In the above example, the pairs in the right order are 1, 2, 4, and 6; the sum of these indices is `*13*`. -Determine which pairs of packets are already in the right order. *What is the sum of the indices of those pairs?* +Determine which pairs of packets are already in the right order. _What is the sum of the indices of those pairs?_ To begin, [get your puzzle input](13/input). Answer: -You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=%22Distress+Signal%22+%2D+Day+13+%2D+Advent+of+Code+2022&url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F13&related=ericwastl&hashtags=AdventOfCode) [Mastodon](javascript:void(0);)] this puzzle. \ No newline at end of file +You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=%22Distress+Signal%22+%2D+Day+13+%2D+Advent+of+Code+2022&url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F13&related=ericwastl&hashtags=AdventOfCode) [Mastodon]()] this puzzle. diff --git a/2022/13/test.js b/2022/13/test.js index 554b8ae..7fb35cb 100644 --- a/2022/13/test.js +++ b/2022/13/test.js @@ -5,17 +5,16 @@ import part1, { sample, puzzleInput, isLessThan } from "./part1.js"; import part2 from "./part2.js"; describe("part 1", () => { - test( "readInput", () => { - expect(sample[0][0]).toEqual([1,1,3,1,1]); - - }); - test( "lessThan", () => { - expect( isLessThan(1,2) ).toBe(-1); - expect( isLessThan(2,1) ).toBe(1); - expect( isLessThan(2,2) ).toBe(0); - expect( isLessThan(2,[1]) ).toBe(1); - expect( isLessThan([1],[2]) ).toBe(-1); - }); + test("readInput", () => { + expect(sample[0][0]).toEqual([1, 1, 3, 1, 1]); + }); + test("lessThan", () => { + expect(isLessThan(1, 2)).toBe(-1); + expect(isLessThan(2, 1)).toBe(1); + expect(isLessThan(2, 2)).toBe(0); + expect(isLessThan(2, [1])).toBe(1); + expect(isLessThan([1], [2])).toBe(-1); + }); test("sample", () => { expect(part1(sample)).toEqual(13); });