diff --git a/.aocrunner.json b/.aocrunner.json index 0bfc8b7..0501f56 100644 --- a/.aocrunner.json +++ b/.aocrunner.json @@ -131,10 +131,10 @@ }, { "part1": { - "solved": false, - "result": null, + "solved": true, + "result": "16880", "attempts": [], - "time": null + "time": 0.137569 }, "part2": { "solved": false, diff --git a/README.md b/README.md index e53b344..9e5d4ab 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ [![Day](https://badgen.net/badge/07/%E2%98%85%E2%98%85/green)](src/day07) [![Day](https://badgen.net/badge/08/%E2%98%85%E2%98%85/green)](src/day08) [![Day](https://badgen.net/badge/09/%E2%98%85%E2%98%85/green)](src/day09) -![Day](https://badgen.net/badge/10/%E2%98%86%E2%98%86/gray) +[![Day](https://badgen.net/badge/10/%E2%98%85%E2%98%86/yellow)](src/day10) ![Day](https://badgen.net/badge/11/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/12/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/13/%E2%98%86%E2%98%86/gray) @@ -132,9 +132,9 @@ Both parts: 27.610987ms ``` Day 10 -Time part 1: - +Time part 1: 0.179ms Time part 2: - -Both parts: - +Both parts: 0.178526ms ``` ``` @@ -243,8 +243,8 @@ Both parts: - ``` ``` -Total stars: 18/50 -Total time: 68.695ms +Total stars: 19/50 +Total time: 68.874ms ``` diff --git a/src/day10/README.md b/src/day10/README.md new file mode 100644 index 0000000..c01a830 --- /dev/null +++ b/src/day10/README.md @@ -0,0 +1,9 @@ +# 🎄 Advent of Code 2022 - day 10 🎄 + +## Info + +Task description: [link](https://adventofcode.com/2022/day/10) + +## Notes + +... \ No newline at end of file diff --git a/src/day10/index.ts b/src/day10/index.ts new file mode 100644 index 0000000..14788f3 --- /dev/null +++ b/src/day10/index.ts @@ -0,0 +1,245 @@ +import run from "aocrunner" + +type instructionType = + | { op: "noop"; arg: undefined } + | { op: "addx"; arg: number } + +const delays = { + noop: 1, + addx: 2, +} as const + +function parseInput(rawInput: string): instructionType[] { + return rawInput.split("\n").map((l) => { + const [op, arg] = l.split(" ") + if (op === "noop") return { op: "noop", arg: undefined } + return { op: "addx", arg: +arg } + }) +} + +const part1 = (rawInput: string) => { + const input = parseInput(rawInput) + const isCycleSampled = (cycle: number) => (cycle - 20) % 40 === 0 + const registers = { x: 1 } + let sum = 0 + let cycle = 0 + for (const { op, arg } of input) { + for (let i = 0; i < delays[op]; i++) { + cycle++ + if (isCycleSampled(cycle)) { + sum += cycle * registers.x + } + } + // between cycles + if (op === "addx") { + registers.x += arg + } else if (op === "noop") { + // do nothing + } + } + return sum +} + +const part2 = (rawInput: string) => { + const input = parseInput(rawInput) + const registers = { x: 1 } + let cycle = 0 + let display = "" + for (const { op, arg } of input) { + for (let i = 0; i < delays[op]; i++) { + if ([-1, 0, 1].includes(registers.x - (cycle % 40))) { + display += "#" + } else { + display += "." + } + cycle++ + if (cycle % 40 === 0) { + display += "\n" + } + } + // between cycles + if (op === "addx") { + registers.x += arg + } else if (op === "noop") { + // do nothing + } + } + console.log(display) + return +} + +const largeExample = `addx 15 +addx -11 +addx 6 +addx -3 +addx 5 +addx -1 +addx -8 +addx 13 +addx 4 +noop +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx -35 +addx 1 +addx 24 +addx -19 +addx 1 +addx 16 +addx -11 +noop +noop +addx 21 +addx -15 +noop +noop +addx -3 +addx 9 +addx 1 +addx -3 +addx 8 +addx 1 +addx 5 +noop +noop +noop +noop +noop +addx -36 +noop +addx 1 +addx 7 +noop +noop +noop +addx 2 +addx 6 +noop +noop +noop +noop +noop +addx 1 +noop +noop +addx 7 +addx 1 +noop +addx -13 +addx 13 +addx 7 +noop +addx 1 +addx -33 +noop +noop +noop +addx 2 +noop +noop +noop +addx 8 +noop +addx -1 +addx 2 +addx 1 +noop +addx 17 +addx -9 +addx 1 +addx 1 +addx -3 +addx 11 +noop +noop +addx 1 +noop +addx 1 +noop +noop +addx -13 +addx -19 +addx 1 +addx 3 +addx 26 +addx -30 +addx 12 +addx -1 +addx 3 +addx 1 +noop +noop +noop +addx -9 +addx 18 +addx 1 +addx 2 +noop +noop +addx 9 +noop +noop +noop +addx -1 +addx 2 +addx -37 +addx 1 +addx 3 +noop +addx 15 +addx -21 +addx 22 +addx -6 +addx 1 +noop +addx 2 +addx 1 +noop +addx -10 +noop +noop +addx 20 +addx 1 +addx 2 +addx 2 +addx -6 +addx -11 +noop +noop +noop` + +run({ + part1: { + tests: [ + // { + // input: `noop + // addx 3 + // addx -5`, + // expected: 0, + // }, + { + input: largeExample, + expected: 13140, + }, + ], + solution: part1, + }, + part2: { + tests: [ + { + input: largeExample, + expected: "", + }, + ], + solution: part2, + }, + trimTestInputs: true, + onlyTests: false, +})