diff options
Diffstat (limited to 'day04.rb')
-rw-r--r-- | day04.rb | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/day04.rb b/day04.rb new file mode 100644 index 0000000..b9335bf --- /dev/null +++ b/day04.rb @@ -0,0 +1,32 @@ +d = [] +IO.foreach("day04") { |line| + d << line.split(/[\[\]:# ]/) +} + +d.sort! + +g = nil +s = nil +zg = Hash.new 0 +zgt = Hash.new 0 +d.each { |l| + case l[5] + when "Guard" + g = l[7].to_i + when "falls" + s = l[3].to_i + when "wakes" + (s...l[3].to_i).each { |t| + zg[g] += 1 + zgt[[g,t]] += 1 + } + end +} + +a = zg.max_by { |k,v| v }.first +b = zgt.max_by { |(g,t),v| g == a ? v : 0 }[0][1] +p a * b +# => 131469 + +p zgt.max_by { |k,v| v }[0].inject(:*) +# => 96951 |