Comment by WJW

Comment by WJW 3 days ago

6 replies

I like to use Haskell, because parser combinators usually make the input parsing aspect of the puzzles extremely straightforward. In addition, the focus of the language on laziness and recursion can lead to some very concise yet idiomatic solutions.

Example: find the first example for when this "game of life" variant has more than 1000 cells in the "alive" state.

Solution: generate infinite list of all states and iterate over them until you find one with >= 1000 alive cells.

    let allStates = iterate nextState beginState # infinite list of consecutive solutions
    let solution = head $ dropWhile (\currentState -> numAliveCells currentState < 1000) allStates
lkuty 3 days ago

Do you plan to share your solutions on Github or something similar ?

  • WJW 3 days ago

    I actually plan on doing this year in Gleam, because I did the last 5 years in Haskell and want to learn a new language this year. My solutions for last year are on github at https://github.com/WJWH/aoc2024 though, if you're interested.

jvuygbbkuurx 3 days ago

Does this solution copy the state on each iteration?

  • WJW 3 days ago

    Haskell values are immutable, so it creates a new state on each iteration. Since most of these "game of life" type problems need to touch every cell in the simulation multiple times anyway, building a new value is not really that much more expensive than mutating in place. The Haskell GC is heavily optimized for quickly allocating and collecting short-lived objects anyway.

    But yeah, if you're looking to solve the puzzle in under a microsecond you probably want something like Rust or C and keep all the data in L1 cache like some people do. If solving it in under a millisecond is still good enough, Haskell is fine.

    • sltkr 3 days ago

      Fun fact about Game of Life is that the leading algorithm, HashLife[1], uses immutable data structures. It's quite well suited to functional languages, and was in fact originally implemented in Lisp by Bill Gosper.

      1. https://en.wikipedia.org/wiki/Hashlife