Skip to content

Instantly share code, notes, and snippets.

@randrews
Created December 13, 2017 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randrews/caf405ce76775313da3f3a3637cef249 to your computer and use it in GitHub Desktop.
Save randrews/caf405ce76775313da3f3a3637cef249 to your computer and use it in GitHub Desktop.
layers = {}
File.open('day13.txt') do |f|
f.each do |line|
if line =~ /(\d+): (\d+)/
depth, range = $1.to_i, $2.to_i
layers[depth] = range
end
end
end
def walk(layers, delay = 0)
if delay > 0
(0 .. (layers.keys.max)).each do |loc|
next unless layers[loc]
pos = (delay + loc) % ((layers[loc]-1)*2)
return false if pos == 0
end
return true
else
severity = 0
(0 .. (layers.keys.max)).each do |loc|
next unless layers[loc]
if (delay + loc) % ((layers[loc]-1)*2) == 0
severity += loc * layers[loc]
end
end
severity
end
end
puts("Part 1: #{walk(layers)}")
n = 0
sev = false
until sev
n += 1
sev = walk(layers, n)
end
puts("Part 2: #{n}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment