diff options
author | Nat Lasseter <user@4574.co.uk> | 2018-12-04 14:30:14 +0000 |
---|---|---|
committer | Nat Lasseter <user@4574.co.uk> | 2018-12-04 14:30:14 +0000 |
commit | 76056832683da277b2c68794cd49897da711d456 (patch) | |
tree | 25ca45fcce4b48d3ea41f28ada91a470db3e1207 /day04 | |
parent | 0d2a3c69cdec606598d9b03732fb4fa0de4ca3ed (diff) |
[day04] part2 done
Diffstat (limited to 'day04')
-rwxr-xr-x | day04/part2 | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/day04/part2 b/day04/part2 new file mode 100755 index 0000000..5127bb3 --- /dev/null +++ b/day04/part2 @@ -0,0 +1,49 @@ +#!/usr/bin/env ruby + +input = $stdin.readlines.map(&:chomp).sort + +currentguard = nil +guards = Hash.new { |h,k| h[k] = Hash.new { |h,k| h[k] = Array.new(60, false) } } + +(0...input.length).each do |i| + if input[i] =~ /\[1518-\d\d-\d\d \d\d:\d\d\] Guard #(\d+) begins shift/ + currentguard = $1.to_i + elsif input[i] =~ /\[1518-(\d\d-\d\d) \d\d:(\d\d)\] falls asleep/ + date = $1 + smin = $2.to_i + input[i+1] =~ /\[1518-\d\d-\d\d \d\d:(\d\d)\] wakes up/ + emin = $1.to_i + (smin...emin).each do |m| + guards[currentguard][date][m] = true + end + else + next + end +end + +sleepiestguard = nil +maxmin = 0 + +guards.each do |id, days| + minutes = Array.new(60, 0) + (0...60).each do |m| + days.each_value do |mins| + minutes[m] += 1 if mins[m] + end + end + if minutes.max > maxmin + maxmin = minutes.max + sleepiestguard = id + end +end + +minutes = Array.new(60, 0) +(0...60).each do |m| + guards[sleepiestguard].each_value do |mins| + minutes[m] += 1 if mins[m] + end +end + +sleepiestminute = minutes.index(minutes.max) + +puts sleepiestguard * sleepiestminute |