diff --git a/solutions/day10/input.txt b/solutions/day10/input.txt new file mode 100644 index 0000000..5b0d755 --- /dev/null +++ b/solutions/day10/input.txt @@ -0,0 +1,102 @@ +153 +17 +45 +57 +16 +147 +39 +121 +75 +70 +85 +134 +128 +115 +51 +139 +44 +65 +119 +168 +122 +72 +105 +31 +103 +89 +154 +114 +55 +25 +48 +38 +132 +157 +84 +71 +113 +143 +83 +64 +109 +129 +120 +100 +151 +79 +125 +22 +161 +167 +19 +26 +118 +142 +4 +158 +11 +35 +56 +18 +40 +7 +150 +99 +54 +152 +60 +27 +164 +78 +47 +82 +63 +46 +91 +32 +135 +3 +108 +10 +159 +127 +69 +110 +126 +133 +28 +15 +104 +138 +160 +98 +90 +144 +1 +2 +92 +41 +86 +66 +95 +12 diff --git a/solutions/day10/solution.js b/solutions/day10/solution.js new file mode 100644 index 0000000..2ac2a20 --- /dev/null +++ b/solutions/day10/solution.js @@ -0,0 +1,48 @@ +const path = require('path') +const { read, position } = require('promise-path') +const fromHere = position(__dirname) +const report = (...messages) => console.log(`[${require(fromHere('../../package.json')).logName} / ${__dirname.split(path.sep).pop()}]`, ...messages) + +async function run () { + const input = (await read(fromHere('input.txt'), 'utf8')).trim().split('\n').map(x => 1 * x).sort((a, b) => b - a) + input.push(0) + input.unshift(input[0] + 3) + + solveForFirstStar(input) + solveForSecondStar(input) +} + +function solveForFirstStar (input) { + let ones = 0 + let threes = 0 + input.forEach((s, i, S) => { + const diff = s - S[i + 1] + if (diff === 1) { + ones++ + } + if (diff === 3) { + threes++ + } + }) + const solution = ones * threes + report('Solution 1:', solution) +} + +function solveForSecondStar (input) { + const choices = [] + input.forEach((s, i, S) => { + choices[i] = i === 0 ? 1 : 0 + for (let j = 1; ; j++) { + if (S[i - j] - s <= 3) { + choices[i] += choices[i - j] + } else { + break + } + } + }) + + const solution = choices[choices.length - 1] + report('Solution 2:', solution) +} + +run() diff --git a/solutions/day10/viewer.html b/solutions/day10/viewer.html new file mode 100644 index 0000000..d583fae --- /dev/null +++ b/solutions/day10/viewer.html @@ -0,0 +1,43 @@ + + + + Solution Viewer + + + + + +
+

Solution Viewer ({{ solutionTitle }})

+

For interesting problems; this page can be used as a dynamic viewer.

+

input.txt

+
{{ inputText }}
+

solution.js

+
{{ solutionText }}
+
+ + + \ No newline at end of file