diff --git a/.aocrunner.json b/.aocrunner.json index 212f95b..39fe576 100644 --- a/.aocrunner.json +++ b/.aocrunner.json @@ -23,16 +23,18 @@ }, { "part1": { - "solved": false, - "result": null, + "solved": true, + "result": "1882980", "attempts": [], - "time": null + "time": 2.58 }, "part2": { - "solved": false, - "result": null, - "attempts": [], - "time": null + "solved": true, + "result": "1971232560", + "attempts": [ + "1882980" + ], + "time": 2.49 } }, { diff --git a/README.md b/README.md index 025cdd1..4f6e886 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ [![Day](https://badgen.net/badge/01/%E2%98%85%E2%98%85/green)](src/day01) -![Day](https://badgen.net/badge/02/%E2%98%86%E2%98%86/gray) +[![Day](https://badgen.net/badge/02/%E2%98%85%E2%98%85/green)](src/day02) ![Day](https://badgen.net/badge/03/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/04/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/05/%E2%98%86%E2%98%86/gray) @@ -76,9 +76,9 @@ Both parts: 0.79ms ``` Day 02 -Time part 1: - -Time part 2: - -Both parts: - +Time part 1: 2.04ms +Time part 2: 1.61ms +Both parts: 3.6500000000000004ms ``` ``` @@ -243,8 +243,8 @@ Both parts: - ``` ``` -Total stars: 2/50 -Total time: 0.79ms +Total stars: 4/50 +Total time: 4.44ms ``` diff --git a/src/day02/README.md b/src/day02/README.md new file mode 100644 index 0000000..8f78bc5 --- /dev/null +++ b/src/day02/README.md @@ -0,0 +1,9 @@ +# 🎄 Advent of Code 2021 - day 2 🎄 + +## Info + +Task description: [link](https://adventofcode.com/2021/day/2) + +## Notes + +... \ No newline at end of file diff --git a/src/day02/index.js b/src/day02/index.js new file mode 100644 index 0000000..99abb9e --- /dev/null +++ b/src/day02/index.js @@ -0,0 +1,68 @@ +import run from "aocrunner" + +const parseInput = (rawInput) => + rawInput + .split("\n") + .map((l) => l.split(" ")) + .map((i) => [i[0], +i[1]]) + +const actions = { + forward: ([h, v], x) => [h + x, v], + down: ([h, v], x) => [h, v + x], + up: ([h, v], x) => [h, v - x], +} +const actions2 = { + forward: ([h, v, a], x) => [h + x, v + a * x, a], + down: ([h, v, a], x) => [h, v, a + x], + up: ([h, v, a], x) => [h, v, a - x], +} + +const part1 = (rawInput) => { + const input = parseInput(rawInput) + const pos = input.reduce( + (acc, [command, val]) => actions[command](acc, val), + [0, 0], + ) + return pos[0] * pos[1] +} + +const part2 = (rawInput) => { + const input = parseInput(rawInput) + const pos = input.reduce( + (acc, [command, val]) => actions2[command](acc, val), + [0, 0, 0], + ) + return pos[0] * pos[1] +} + +run({ + part1: { + tests: [ + { + input: `forward 5 +down 5 +forward 8 +up 3 +down 8 +forward 2`, + expected: 150, + }, + ], + solution: part1, + }, + part2: { + tests: [ + { + input: `forward 5 +down 5 +forward 8 +up 3 +down 8 +forward 2`, + expected: 900, + }, + ], + solution: part2, + }, + trimTestInputs: true, +})