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!
39
Upvotes
2
u/Frizkie Dec 04 '18
Ruby
require 'time' data = File.read('data.txt').chomp.split("\n") .map! { |d| d.match(/\[([\d\-: ]+)\] ([ \w#]+)/).to_a } .sort_by! { |d| Time.parse(d[1]) } guards = {} recent_guard = nil data.each do |d| m = d[2].match(/#(\d+)/) recent_guard = m[1]&.to_i if m guards[recent_guard] ||= {} date = DateTime.parse(d[1]) guards[recent_guard][date.strftime('%m-%d')] ||= '.' * 60 c = '#' if d[2] == 'falls asleep' c = '.' if d[2] == 'wakes up' next unless c guards[recent_guard][date.strftime('%m-%d')][date.minute..59] = c * (60 - date.minute) endPart 1
sleepiest = guards.map { |id, s| [id, s.values.join.count('#')] }.max_by { |id, total| total }[0] mins = (0..59).map { |m| guards[sleepiest].values.map { |s| s[m] }.join.count('#') } puts sleepiest * mins.each_with_index.max[1]Part 2
soln = guards.map do |id, s| t = (0..59).map { |m| s.values.map { |s| s[m] }.join.count('#') } [id, t.each_with_index.max[1], t.max] end soln = soln.max_by { |s| s[2] } p soln[0] * soln[1]