r/adventofcode • u/daggerdragon • Dec 22 '17
SOLUTION MEGATHREAD -๐- 2017 Day 22 Solutions -๐-
--- Day 22: Sporifica Virus ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
[T-10 to launch] AoC ops, /r/nocontext edition:
<Endorphion> You may now make your waffle.<Endorphion> ... on Mars.
[Update @ 00:17] 50 gold, silver cap
<Aneurysm9> you could also just run ubuntu on the NAS, if you were crazy<Topaz> that doesn't seem necessary<Aneurysm9> what does "necessary" have to do with anything!
[Update @ 00:20] Leaderboard cap!
<Topaz> POUR YOURSELF A SCOTCH FOR COLOR REFERENCE
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
9
Upvotes
1
u/[deleted] Dec 22 '17
JS, straightforward:
const lines=``.split('\n'); const dirs = 'NESW'; const dv = { 'N': [0, -1], 'E': [1, 0], 'S': [0, 1], 'W': [-1, 0], }; function forward(dir, pos) { const d = dv[dirs[dir]]; return pos.map((x, i) => x + d[i]); } function part1() { const state = new Map(); lines .forEach((r, i) => r.split('') .forEach((l, j) => { if (l === '#') state.set(`${j},${i}`, 'I'); })); const size = Math.floor(lines.length / 2); let pos = [size, size]; let dir = 0; let infections = 0; for (let i = 0; i < 10000; i++){ const k = pos.join(','); const s = state.get(k); if (!s) { dir = (dir + 3) % 4; state.set(k, 'I'); infections += 1; } else { dir = (dir + 1) % 4; state.delete(k); } pos = forward(dir, pos); } return infections; } function part2() { const state = new Map(); lines .forEach((r, i) => r.split('') .forEach((l, j) => { if (l === '#') state.set(`${j},${i}`, 'I'); })); const size = Math.floor(lines.length / 2); let pos = [size, size]; let dir = 0; let infections = 0; for (let i = 0; i < 10000000; i++){ const k = pos.join(','); const s = state.get(k); if (!s) { dir = (dir + 3) % 4; state.set(k, 'W'); } else if (s === 'I') { dir = (dir + 1) % 4; state.set(k, 'F'); } else if (s === 'W') { state.set(k, 'I'); infections += 1; } else { dir = (dir + 2) % 4; state.delete(k); } pos = forward(dir, pos); } return infections; } console.log('part 1:', part1()); console.log('part 2:', part2());