Skip to content

Instantly share code, notes, and snippets.

@mattmc3
Last active December 6, 2021 19:54
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 mattmc3/8e603bb77d0b514298fcb2c5fce5a17e to your computer and use it in GitHub Desktop.
Save mattmc3/8e603bb77d0b514298fcb2c5fce5a17e to your computer and use it in GitHub Desktop.
Advent of Code - Day 6
function day6 \
--description "https://adventofcode.com/2021/day/6 - usage: day6 part1 datafile.dat" \
--argument-names part datafile
test -f "$datafile"; or echo >&2 "file expected" && return 1
set part (string match --regex '.$' $part)
set --local total_days (test $part -eq 1 && echo 80 || echo 256)
set --local lantern_fish_timers (string split ',' <$datafile)
set --local new_fish_each_day (string split '' (string repeat -n $total_days 0))
set --local fish_alive_each_day (string split '' (string repeat -n $total_days 0))
# initialize
# we use the fish we know about, and set their breeding schedule
for timer in $lantern_fish_timers
for day in (seq $total_days)
# (day+(7-timer)-1)%7
if test (math \($day + \(7 - $timer\) - 1\) % 7) -eq 0
set new_fish_each_day[$day] (math $new_fish_each_day[$day] + 1)
end
end
end
# then we calculate for each remaining day
for day in (seq $total_days)
set new_fish $new_fish_each_day[$day]
set yesterday (math $day - 1)
if test $yesterday -lt 1
set fish_alive_each_day[$day] (math (count $lantern_fish_timers) + $new_fish)
else
set fish_alive_each_day[$day] (math $fish_alive_each_day[$yesterday] + $new_fish)
end
# new fish breed and produce a new fish after 9 days,
# and then every 7th day after that
set --local nine_days_later (math $day + 9)
if test $nine_days_later -le $total_days
set $new_fish_each_day[$nine_days_later] = (math $new_fish_each_day[$nine_days_later] + $new_fish)
for breeding_day in (seq $nine_days_later $total_days)
if test (math \($breeding_day - $day - 2\) % 7) -eq 0
set new_fish_each_day[$breeding_day] (math $new_fish_each_day[$breeding_day] + $new_fish)
end
end
end
end
echo "The solution is: $fish_alive_each_day[-1]"
end
#!/usr/bin/env python3
def main():
days = 256
lantern_fish_timers = [int(x) for x in "3,4,3,1,2".split(",")]
new_fish_each_day = [0] * (days+1)
fish_alive_each_day = [0] * (days+1)
# initialize
# we use the fish we know about, and set their breeding schedule
fish_alive_each_day[0] = len(lantern_fish_timers)
for timer in lantern_fish_timers:
breeding_days = [x+timer+1
for x in range(days+1)
if x%7 == 0 and x+timer+1 <= days]
for day in breeding_days:
new_fish_each_day[day] += 1
# then we calculate for each remaining day
for day in range(1, days+1):
new_fish = new_fish_each_day[day]
fish_alive_each_day[day] = fish_alive_each_day[day-1] + new_fish
# new fish breed and produce a new fish after 9 days,
# and then every 7th day after that
if day+9 <= days:
new_fish_each_day[day+9] += new_fish
breeding_days = [x+1
for x in range(day+9, days)
if (x-(day+9)+1)%7 == 0]
for breeding_day in breeding_days:
new_fish_each_day[breeding_day] += new_fish
print(f"The solution is: {fish_alive_each_day[-1]}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment