Skip to content

Instantly share code, notes, and snippets.

@colintsteele
Last active December 6, 2021 17:25
Show Gist options
  • Save colintsteele/4466f16d6d954564c02ac66e64d257e5 to your computer and use it in GitHub Desktop.
Save colintsteele/4466f16d6d954564c02ac66e64d257e5 to your computer and use it in GitHub Desktop.
lanternfish
# day 6
test_input = [3, 4, 3, 1, 2]
input = test_input # set puzzle input here
@lantern_fish = Hash.new(0)
0.upto(input.max) do |counts|
@lantern_fish[counts] = input.count(counts)
end
## start hatching
def pass_day(days: 1)
days.times do
hatch_count = @lantern_fish[0] # how many lanternfish are going to hatch after the day passes
0.upto(@lantern_fish.keys.max).with_index do |index|
@lantern_fish[index] = @lantern_fish[index + 1] if index !=8
end
@lantern_fish[8] = hatch_count
@lantern_fish[6] += hatch_count #hatch count is the same as 'reset to 6' count
end
end
pass_day(days: 80); @lantern_fish.values.sum # part 1 solution
pass_day(days: 176); @lantern_fish.values.sum # part 2 solution (days: 256 if starting from new data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment