blob: 808d5fbe22161d72e01376963a47ac79b42abe6a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#!/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
times = []
guards.each do |id, days|
sum = 0
days.each_value do |day|
sum += day.count(true)
end
times[id] = sum
end
sleepiestguard = times.index(times.compact.max)
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
|