43 lines
827 B
Ruby
43 lines
827 B
Ruby
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
|
|
|