53 lines
1.5 KiB
Crystal
53 lines
1.5 KiB
Crystal
require "./common.cr"
|
|
|
|
lines = File.read("day4_input").split("\n")
|
|
lines.pop
|
|
lines.sort!
|
|
|
|
REGEX = /\[(\d+)-(\d+)-(\d+) (\d+):(\d+)\].+/
|
|
GUARD_REGEX = /.+#(\d+).+/
|
|
|
|
active = 0
|
|
last_time = {0, 1, 0, 0, 0}
|
|
minutes = {} of Int32 => Array(Int32)
|
|
|
|
lines.each do |line|
|
|
match = line.match(REGEX).not_nil!
|
|
year = match[1].to_i32
|
|
month = match[2].to_i32
|
|
day = match[3].to_i32
|
|
hour = match[4].to_i32
|
|
minute = match[5].to_i32
|
|
new_time = {year, month, day, hour, minute}
|
|
|
|
if line.includes? "Guard"
|
|
active = line.match(GUARD_REGEX).not_nil![1].to_i32
|
|
elsif line.includes? "wakes"
|
|
old_year, old_month, old_day, old_hour, old_minute = last_time
|
|
difference = (day - old_day) * 24 * 60 +
|
|
(hour - old_hour) * 60 +
|
|
(minute - old_minute)
|
|
|
|
array = minutes[active]? || Array.new(60, 0)
|
|
60.times do |m|
|
|
array[m] += difference / 60
|
|
end
|
|
(difference % 60).times do |m|
|
|
actual_minute = (old_minute + m) % 60
|
|
array[actual_minute] += 1
|
|
end
|
|
minutes[active] = array
|
|
end
|
|
last_time = new_time
|
|
end
|
|
|
|
guard_num, guard_minutes = minutes.max_by &.[1].sum
|
|
puts "Guard num: #{guard_num}"
|
|
puts "Guard minute: #{(guard_minutes.index guard_minutes.max).not_nil!}"
|
|
puts "Answer num: #{guard_num * (guard_minutes.index guard_minutes.max).not_nil!}"
|
|
|
|
guard_num, guard_minutes = minutes.max_by &.[1].max
|
|
puts "Guard num: #{guard_num}"
|
|
puts "Guard minute: #{(guard_minutes.index guard_minutes.max).not_nil!}"
|
|
puts "Answer num: #{guard_num * (guard_minutes.index guard_minutes.max).not_nil!}"
|