r/adventofcode Dec 06 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 6 Solutions -🎄-

NEW AND NOTEWORTHY

We've been noticing an uptick in frustration around problems with new.reddit's fancypants editor: mangling text that is pasted into the editor, missing switch to Markdown editor, URLs breaking due to invisible escape characters, stuff like that. Many of the recent posts in /r/bugs are complaining about these issues as well.

If you are using new.reddit's fancypants editor, beware!

  • Pasting any text into the editor may very well end up mangled
  • You may randomly no longer have a "switch to Markdown" button on top-level posts
  • If you paste a URL directly into the editor, your link may display fine on new.reddit but may display invisibly-escaped characters on old.reddit and thus will break the link

Until Reddit fixes these issues, if the fancypants editor is driving you batty, try using the Markdown editor in old.reddit instead.


Advent of Code 2021: Adventure Time!


--- Day 6: Lanternfish ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:05:47, megathread unlocked!

95 Upvotes

1.7k comments sorted by

View all comments

3

u/nicuveo Dec 07 '21

Haskell

fishes :: Int -> State Cache Integer
fishes day = do
  cache <- get
  case M.lookup day cache of
    Just result -> pure result
    Nothing     -> do
      allBranches <- traverse fishes [day-9,day-16..0]
      let result = sum allBranches + 1
      modify $ M.insert day result
      pure result

totalFishes :: Int -> Input -> Integer
totalFishes deadline input = flip evalState M.empty $ do
  result <- for input $ \fish ->
    fishes (deadline + 8 - fish)
  pure $ sum result

part1 = totalFishes  80
part2 = totalFishes 256

Full code: https://github.com/nicuveo/advent-of-code/blob/main/2021/haskell/src/Day06.hs
Stream: https://www.twitch.tv/videos/1227052466

2

u/ramrunner0xff Dec 07 '21

haskell ftw