пс. ищите все другие мои решения проблем Advent of Code здесь.
День 18
Подробности челленджа смотрите здесь.
This file contains 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) |
Чтобы решить обе части 1 и 2:
This file contains 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 |