From ebc3014c4875670712d64881eda8a739c853c780 Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Tue, 19 Dec 2017 14:29:17 +0100 Subject: [PATCH] 13th done --- 13.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 13.rb diff --git a/13.rb b/13.rb new file mode 100644 index 0000000..251ba2b --- /dev/null +++ b/13.rb @@ -0,0 +1,42 @@ +input = File.read('input_13.txt').gsub(" ","").split("\n").map do |x| x.split(":") end.to_h +#input = {0 => 3, 1 => 2, 4 => 4, 6 => 4} +input = input.map do |k, v| [k.to_i, v.to_i] end.to_h + +def round_trip a + (a*2)-2 +end + +def do_count input + input.keys.reduce(0) do |mem, time| + p "Time: " + time.to_s + p "Range: " + input[time].to_s + p "Calc: " + (time % (round_trip input[time])).to_s + if time % (round_trip input[time]) == 0 then + cost = time * (input[time]) + mem += cost + else mem + end + end +end + +def do_count_with_delay input + i = 0 + while true do + res = 0 + input.keys.each do |time| + if (time+i) % (round_trip input[time]) == 0 then + res = 1 + end + end + if res == 0 then + return i + end + i += 1 + end +end + + + +p do_count input +p do_count_with_delay input +