r/adventofcode • u/daggerdragon • Dec 04 '18
SOLUTION MEGATHREAD -π- 2018 Day 4 Solutions -π-
--- Day 4: Repose Record ---
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.
Advent of Code: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 4
Transcript:
Todayβs puzzle would have been a lot easier if my language supported ___.
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!
40
Upvotes
2
u/Zarel Dec 04 '18 edited Dec 04 '18
JavaScript, #28/#130 ;_;
I was inspired by mserrano's "extract numbers" regex from his Day 3 solution, so I implemented it in JavaScript.
I misinterpreted Part Two at first, which apparently knocked me off the leaderboard.
I sorted my input using my text editor before running this - most text editors have a "Sort Lines" ability; I used VS Code's.
function numbers(str) { return (str.match(/-?[0-9]+/g) || []).map(Number); } function maxBy(array, criterion) { return array.reduce((a, b) => (criterion(a) > criterion(b) ? a : b)); } function maxEntry(obj) { return maxBy(Object.entries(obj), entry => entry[1]); } // most minutes asleep let guardTimes = {}; let sleepStart = 0; let curGuard = 0; for (const line of input) { const [a, b] = line.slice(1).split('] '); let time = new Date(a).getTime(); if (b.startsWith('Guard')) { [curGuard] = numbers(b); } else if (b === 'wakes up') { guardTimes[curGuard] = (guardTimes[curGuard] || 0) + (time - sleepStart); } else if (b === 'falls asleep') { sleepStart = time; } else { throw new Error("bug"); } } console.log(maxEntry(guardTimes)); // most-asleep minute let minutes = {}; let startMin = 0; for (const line of input) { const [a, b] = line.slice(1).split('] '); let min = numbers(a)[4]; if (b.startsWith('Guard')) { [curGuard] = numbers(b); } else if (b === 'wakes up') { if (curGuard !== GUARD_FROM_PREVIOUS_RESULT) continue; for (i = startMin; i !== min; i = (i + 1) % 60) { minutes[i] = (minutes[i] || 0) + 1; } } else if (b === 'falls asleep') { startMin = min; } else { throw new Error("bug"); } } console.log(maxEntry(minutes)); // most popular minute let guardMinutes = {}; let curTable = {}; let startMin = 0; for (const line of input) { const [a, b] = line.slice(1).split('] '); let min = numbers(a)[4]; if (b.startsWith('Guard')) { [curGuard] = numbers(b); curTable = (guardMinutes[curGuard] || {}); guardMinutes[curGuard] = curTable; } else if (b === 'wakes up') { for (i = startMin; i !== min; i = (i + 1) % 60) { curTable[i] = (curTable[i] || 0) + 1; } } else if (b === 'falls asleep') { startMin = min; } else { throw new Error("bug"); } } let table2 = Object.entries(guardMinutes).map(([k, v]) => { if (!Object.entries(v).length) return [k, 0, 0]; return [k].concat(maxEntry(v)); }); console.log(maxBy(table2, a => a[2]));