-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.fs
80 lines (65 loc) · 2.12 KB
/
Program.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
module Day11
open Common.Navigation
open System.IO
type Tile = Floor | FreeSeat | OccupiedSeat
let parse (row, line) =
let parseChar c =
match c with
| '.' -> Floor
| 'L' -> FreeSeat
line
|> Seq.indexed
|> Seq.map (fun (col, c) -> ((row, col), parseChar c))
let rec evolve tiles =
let getNewTile coordinate tile =
let surroundingOccupied =
neighbours8 coordinate
|> List.choose (fun c -> Map.tryFind c tiles)
|> List.sumBy (fun t -> if t = OccupiedSeat then 1 else 0)
match tile with
| FreeSeat when surroundingOccupied = 0 -> OccupiedSeat
| OccupiedSeat when surroundingOccupied >= 4 -> FreeSeat
| _ -> tile
let newTiles = tiles |> Map.map getNewTile
if newTiles = tiles then
newTiles
else
evolve newTiles
let rec evolve2 tiles =
let getNewTile coordinate tile =
let firstSeat line =
line
|> Seq.skipWhile (fun c -> Map.containsKey c tiles && tiles.[c] = Floor)
|> Seq.map (fun c -> Map.tryFind c tiles)
|> Seq.head
let surroundingOccupied =
lineOfSight coordinate
|> List.choose firstSeat
|> List.sumBy (fun t -> if t = OccupiedSeat then 1 else 0)
match tile with
| FreeSeat when surroundingOccupied = 0 -> OccupiedSeat
| OccupiedSeat when surroundingOccupied >= 5 -> FreeSeat
| _ -> tile
let newTiles = tiles |> Map.map getNewTile
if newTiles = tiles then
newTiles
else
evolve2 newTiles
let partOne input =
evolve input
|> Map.filter (fun _ tile -> tile = OccupiedSeat)
|> Map.count
let partTwo input =
evolve2 input
|> Map.filter (fun _ tile -> tile = OccupiedSeat)
|> Map.count
[<EntryPoint>]
let main argv =
let input =
File.ReadLines("Input.txt")
|> Seq.indexed
|> Seq.collect parse
|> Map.ofSeq
partOne input |> printfn "Part one: %d"
partTwo input |> printfn "Part two: %d"
0