Advent of Code F# – Day 18

Yan Cui

I help clients go faster for less using serverless technologies.

ps. look out for all my other solutions for Advent of Code challenges here.

 

Day 18

See details of the challenge here.

open System
let previousTiles (row : string) =
seq {
yield [| '.'; row.[0]; row.[1] |]
yield! row |> Seq.windowed 3
yield [| row.[row.Length-2]; row.[row.Length-1]; '.' |]
}
let genRows input =
let nextRow = function
| [| '^'; '^'; '.' |]
| [| '^'; '.'; '.' |]
| [| '.'; '^'; '^' |]
| [| '.'; '.'; '^' |] -> '^'
| _ -> '.'
seq {
yield input
yield! input |> Seq.unfold (fun row ->
let nextRow =
row
|> previousTiles
|> Seq.map nextRow
|> Seq.toArray
|> fun chars -> new String(chars)
Some(nextRow, nextRow))
}
let solve input n =
genRows input
|> Seq.take n
|> Seq.sumBy (fun row -> row |> Seq.filter ((=) '.') |> Seq.length)

To solve both part 1 and 2:

let input = "^.^^^..^^...^.^..^^^^^.....^...^^^..^^^^.^^.^^^^^^^^.^^.^^^^...^^...^^^^.^.^..^^..^..^.^^.^.^......."
let part1 = solve input 40
let part2 = solve input 400000

 

Links

Whenever you’re ready, here are 3 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
  2. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
  3. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

1 thought on “Advent of Code F# – Day 18”

  1. Pingback: Advent of Code F# – Day 19 | theburningmonk.com

Comments are closed.