From 760409604d8bef117ef40a1863f8768e5b436e14 Mon Sep 17 00:00:00 2001 From: Joshua Seigler Date: Fri, 10 Dec 2021 00:44:22 -0500 Subject: [PATCH] day 10 --- .aocrunner.json | 12 ++--- src/day10/README.md | 9 ++++ src/day10/index.js | 117 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 src/day10/README.md create mode 100644 src/day10/index.js diff --git a/.aocrunner.json b/.aocrunner.json index 27522a4..327d5f7 100644 --- a/.aocrunner.json +++ b/.aocrunner.json @@ -139,16 +139,16 @@ }, { "part1": { - "solved": false, - "result": null, + "solved": true, + "result": "315693", "attempts": [], - "time": null + "time": 7.37 }, "part2": { - "solved": false, - "result": null, + "solved": true, + "result": "1870887234", "attempts": [], - "time": null + "time": 2.29 } }, { diff --git a/src/day10/README.md b/src/day10/README.md new file mode 100644 index 0000000..4af7637 --- /dev/null +++ b/src/day10/README.md @@ -0,0 +1,9 @@ +# 🎄 Advent of Code 2021 - day 10 🎄 + +## Info + +Task description: [link](https://adventofcode.com/2021/day/10) + +## Notes + +... \ No newline at end of file diff --git a/src/day10/index.js b/src/day10/index.js new file mode 100644 index 0000000..4187c3d --- /dev/null +++ b/src/day10/index.js @@ -0,0 +1,117 @@ +import run from "aocrunner" + +const parseInput = (rawInput) => rawInput.trim().split`\n` + +const match = { + ")": "(", + "]": "[", + "}": "{", + ">": "<", +} +const push = Object.values(match) +const pop = Object.keys(match) + +const part1 = (rawInput) => { + const errorScores = { + ")": 3, + "]": 57, + "}": 1197, + ">": 25137, + } + const input = parseInput(rawInput) + let score = 0 + for (const line of input) { + const stack = [] + check: for (let i = 0; i < line.length; i++) { + const c = line[i] + if (push.includes(c)) { + stack.push(c) + } else if (pop.includes(c)) { + if (match[c] === stack[stack.length - 1]) { + stack.pop() + } else { + score += errorScores[c] + break check + } + } + } + } + return score +} + +const part2 = (rawInput) => { + const autocompleteScores = { + "(": 1, + "[": 2, + "{": 3, + "<": 4, + } + const input = parseInput(rawInput) + const scores = [] + for (const line of input) { + const stack = [] + let error = false + let score = 0 + check: for (let i = 0; i < line.length; i++) { + const c = line[i] + if (push.includes(c)) { + stack.push(c) + } else if (pop.includes(c)) { + if (match[c] === stack[stack.length - 1]) { + stack.pop() + } else { + error = true + break check + } + } + } + if (!error) { + while (stack.length !== 0) { + score = score * 5 + autocompleteScores[stack.pop()] + } + scores.push(score) + } + } + scores.sort((a, b) => a - b) + return scores[(scores.length - 1) / 2] +} + +run({ + part1: { + tests: [ + { + input: `[({(<(())[]>[[{[]{<()<>> +[(()[<>])]({[<{<<[]>>( +{([(<{}[<>[]}>{[]{[(<()> +(((({<>}<{<{<>}{[]{[]{} +[[<[([]))<([[{}[[()]]] +[{[{({}]{}}([{[{{{}}([] +{<[[]]>}<{[{[{[]{()[[[] +[<(<(<(<{}))><([]([]() +<{([([[(<>()){}]>(<<{{ +<{([{{}}[<[[[<>{}]]]>[]]`, + expected: 26397, + }, + ], + solution: part1, + }, + part2: { + tests: [ + { + input: `[({(<(())[]>[[{[]{<()<>> +[(()[<>])]({[<{<<[]>>( +{([(<{}[<>[]}>{[]{[(<()> +(((({<>}<{<{<>}{[]{[]{} +[[<[([]))<([[{}[[()]]] +[{[{({}]{}}([{[{{{}}([] +{<[[]]>}<{[{[{[]{()[[[] +[<(<(<(<{}))><([]([]() +<{([([[(<>()){}]>(<<{{ +<{([{{}}[<[[[<>{}]]]>[]]`, + expected: 288957, + }, + ], + solution: part2, + }, + trimTestInputs: true, +})