diff options
Diffstat (limited to 'day10')
-rw-r--r-- | day10/data.js | 103 | ||||
-rw-r--r-- | day10/day10-1.js | 19 | ||||
-rw-r--r-- | day10/day10-2.js | 19 |
3 files changed, 141 insertions, 0 deletions
diff --git a/day10/data.js b/day10/data.js new file mode 100644 index 0000000..8b5a64f --- /dev/null +++ b/day10/data.js @@ -0,0 +1,103 @@ +data=[ +151, +94, +14, +118, +25, +143, +33, +23, +80, +95, +87, +44, +150, +39, +148, +51, +138, +121, +70, +69, +90, +155, +144, +40, +77, +8, +97, +45, +152, +58, +65, +63, +128, +101, +31, +112, +140, +86, +30, +55, +104, +135, +115, +16, +26, +60, +96, +85, +84, +48, +4, +131, +54, +52, +139, +76, +91, +46, +15, +17, +37, +156, +134, +98, +83, +111, +72, +34, +7, +108, +149, +116, +32, +110, +47, +157, +75, +13, +10, +145, +1, +127, +41, +53, +2, +3, +117, +71, +109, +105, +64, +27, +38, +59, +24, +20, +124, +9, +66, +] + +module.exports = data diff --git a/day10/day10-1.js b/day10/day10-1.js new file mode 100644 index 0000000..36363cc --- /dev/null +++ b/day10/day10-1.js @@ -0,0 +1,19 @@ +const data = require('./data') +let jolts = 0, diff1 = 0, diff3 = 1 + +const next = () => { + const possible = data.filter(a => [jolts+1, jolts+2, jolts+3].indexOf(a) > -1) + return possible.length > 0 && Math.min.apply(Math, possible) +} + +while (next()) { + if ((next() - jolts) === 1) { + diff1 += 1 + } else if ((next() - jolts) === 3) { + diff3 += 1 + } + jolts = next() +} + +console.log(diff1*diff3) + diff --git a/day10/day10-2.js b/day10/day10-2.js new file mode 100644 index 0000000..83435bf --- /dev/null +++ b/day10/day10-2.js @@ -0,0 +1,19 @@ +const data = require('./data') +const sorted = data.concat(0).sort((a,b) => a - b) +sorted.push(sorted[sorted.length-1] + 3) +const paths = [] + +const count = (index) => { + if (index === sorted.length - 1) { return 1 } + if (paths[index]) { return paths[index] } + + let pathsCount = 0 + sorted + .filter(s => s > sorted[index] && s <= sorted[index] + 3) + .forEach(f => pathsCount += count(sorted.indexOf(f))) + paths[index] = pathsCount + return pathsCount +} + +console.log(count(0)) + |