
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let input = "^.^^^..^^...^.^..^^^^^.....^...^^^..^^^^.^^.^^^^^^^^.^^.^^^^...^^...^^^^.^.^..^^..^..^.^^.^.^......." | |
let part1 = solve input 40 | |
let part2 = solve input 400000 |
Links
- Day 18 challenge description
- Advent of Code 2015
- Solution for Day 17
- All my F# solutions for Advent of Code
- Github repo
Whenever you’re ready, here are 3 ways I can help you:
- 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.
- 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.
- Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.
Pingback: Advent of Code F# – Day 19 | theburningmonk.com