-
-
Save Papierkorb/1369ba4dafcf8262e0e54dbd736ebec4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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