about summary refs log tree commit diff
path: root/day04.rb
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2018-12-05 18:14:08 +0100
committerLeah Neukirchen <leah@vuxu.org>2018-12-05 18:14:08 +0100
commit9b9dc4d2fc08c07435d61e34a36b8ce037168a5c (patch)
tree28522c80abb4e76e7b4b783434d4800da5b95d9e /day04.rb
parentd0b988bb748038ec88b63d4d7ba0885be8221bed (diff)
downloadadventofcode2018-9b9dc4d2fc08c07435d61e34a36b8ce037168a5c.tar.gz
adventofcode2018-9b9dc4d2fc08c07435d61e34a36b8ce037168a5c.tar.xz
adventofcode2018-9b9dc4d2fc08c07435d61e34a36b8ce037168a5c.zip
day04
Diffstat (limited to 'day04.rb')
-rw-r--r--day04.rb32
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