Skip to content

Instantly share code, notes, and snippets.

@Papierkorb
Created December 6, 2017 14:01
Show Gist options
  • Save Papierkorb/1369ba4dafcf8262e0e54dbd736ebec4 to your computer and use it in GitHub Desktop.
Save Papierkorb/1369ba4dafcf8262e0e54dbd736ebec4 to your computer and use it in GitHub Desktop.
def max_index(enum : Enumerable(Int32)) : Int32?
max = Int32::MIN
index = nil
enum.each_with_index do |el, idx|
if el > max
max = el
index = idx
end
end
index
end
def part1(banks = [] of Int32)
seen = Set(Array(Int32)).new{ banks }
cycles = (0..Int32::MAX).find do |cycles|
break cycles if seen.includes? banks
idx_bank = max_index(banks).not_nil!
blocks = banks[idx_bank]
banks[idx_bank] = 0
blocks.downto(1) do |offset|
banks[idx_bank - offset] += 1
end
seen << banks.dup
nil
end
{ cycles.not_nil!, banks, seen }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment