-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23_part01.fs
104 lines (87 loc) · 3.26 KB
/
day23_part01.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
module day23_part01
open AdventOfCode_2022.Modules
type Board = Set<int*int>
let N = (-1,0)
let NW = (-1,-1)
let NE = (-1,1)
let S = (1,0)
let SW = (1,-1)
let SE = (1,1)
let E = (0,1)
let W = (0,-1)
let options = [| [|N;NE;NW|]; [|S;SE;SW|]; [|W;NW;SW|]; [|E;NE;SE|] |]
let splitLine r line =
line |> Seq.mapi(fun c ch -> if ch = '#' then Some(r,c)
else None)
|> Seq.choose id
|> Array.ofSeq
let path = "day23/day23_input.txt"
let lines = LocalHelper.GetLinesFromFile path
let elves = lines |> Array.mapi(fun r line -> splitLine r line)
|> Array.concat
|> Set.ofArray
let boundaries (sqs:Board) =
let rows = sqs |> Seq.map(fun (r,_) -> r)
let cols = sqs |> Seq.map(fun (_,c) -> c)
let minRow = rows |> Seq.min
let maxRow = rows |> Seq.max
let minCol = cols |> Seq.min
let maxCol = cols |> Seq.max
minRow, maxRow, minCol, maxCol
let score (sqs:Board) =
let minRow, maxRow, minCol, maxCol = boundaries sqs
let _,empty = seq { for r in minRow..maxRow do
for c in minCol..maxCol -> r,c }
|> Seq.countBy(fun (r,c) -> sqs.Contains(r,c))
|> Seq.head
empty
let dump (sqs:Board) =
let minRow, maxRow, minCol, maxCol = boundaries sqs
for r in minRow-1 .. maxRow+1 do
let mutable s = ""
for c in minCol-1 .. maxCol+1 do
let ch = if sqs.Contains(r,c) then "#"
else "."
s <- s + ch
printfn $"{s}"
printfn ""
let elf (board:Board) r c option =
options[option]
|> Array.exists(fun (orow, ocol) -> board.Contains(r+orow, c+ocol))
let lonely (board:Board) r c =
not ([ N; S; E; W; NE; NW; SE; SW ]
|> List.exists(fun (dr,dc) -> board.Contains(r+dr,c+dc)))
let pickOpt (board:Board) r c option =
let spaces =
seq { 0.. 3 }
|> Seq.choose(fun x -> let opt = (option + x) % options.Length
if lonely board r c then
Some(r, c)
elif not(elf board r c opt) then
let pr, pc = options[opt][0]
Some(r + pr, c + pc)
else None)
|> List.ofSeq
if spaces.Length = 0 then
(r, c), (r, c)
else
(r, c), spaces |> List.head
let propose state option =
let next = state |> Seq.map(fun (r,c) -> pickOpt state r c option)
// okay, I have the proposals, now I need to check to see if any
// elves want to move to the same location
// So, first group and count the proposed destinations. If the
// count > 1 the elf will remain in their spot
let dest = next |> Seq.map(fun (_,d) -> d)
|> Seq.groupBy(fun d -> d)
|> Seq.map(fun (d, arr) -> d, arr |> List.ofSeq
|> List.length)
|> Map.ofSeq
next |> Seq.map(fun (s,d) -> if dest[d] > 1 then s else d)
|> Set.ofSeq
let part1 =
let rounds = seq { 0..9 }
|> Seq.fold(fun state opt -> propose state (opt % options.Length)) elves
score rounds
let execute =
part1