-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day10.fs
241 lines (190 loc) · 6.91 KB
/
Day10.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
module aoc23.Day10
open FSharp.Stats
// a pipe is defined by it's entry vectors from both sides, like this: -> PIPE <-
[<Struct>]
type Pipe = Pipe of Vector<float> * Vector<float>
type GridItem =
| PipeItem of Pipe
| StartItem
| EmptyItem
| UnknownItem
type Cursor =
{ direction: Vector<float>
position: Vector<float> }
module Grid =
let findStart (lines: string array) =
let y = lines |> Array.findIndex (_.Contains('S'))
let x = lines[y].IndexOf('S')
vector [| float x; float y |]
let tryCharAt (point: Vector<float>) (lines: string array) =
match point |> Vector.raw with
| [| x; y |] ->
if y >= 0 && y < lines.Length && x >= 0 && x < lines[int y].Length then
Some(lines[int y][int x])
else
None
| _ -> failwith "vector length is not equal to 2"
let tryItemAt point lines =
lines
|> tryCharAt point
|> Option.map (function
| '|' -> Pipe(vector [ 0.0; 1.0 ], vector [ 0.0; -1.0 ]) |> PipeItem
| '-' -> Pipe(vector [ 1.0; 0.0 ], vector [ -1.0; 0.0 ]) |> PipeItem
| 'J' -> Pipe(vector [ 1.0; 0.0 ], vector [ 0.0; 1.0 ]) |> PipeItem
| '7' -> Pipe(vector [ 1.0; 0.0 ], vector [ 0.0; -1.0 ]) |> PipeItem
| 'F' -> Pipe(vector [ -1.0; 0.0 ], vector [ 0.0; -1.0 ]) |> PipeItem
| 'L' -> Pipe(vector [ -1.0; 0.0 ], vector [ 0.0; 1.0 ]) |> PipeItem
| 'S' -> StartItem
| '.' -> EmptyItem
| _ -> UnknownItem)
let itemAt point lines = tryItemAt point lines |> Option.get
let charAt point lines = tryCharAt point lines |> Option.get
let allDirections =
((1.0, 0.0), Seq.replicate 4 ())
||> Seq.scan (fun (x, y) _ -> (-y, x))
|> Seq.map (fun (x, y) -> vector [| x; y |])
module Cursor =
let forward cursor =
{ cursor with
position = cursor.position + cursor.direction }
let changeDirection newDirection cursor =
{ cursor with direction = newDirection }
let leftRightPositions cursor =
match cursor.direction |> Vector.raw with
| [| x; y |] -> (vector [| y; -x |], vector [| -y; x |]) |> TupleEx.map ((+) cursor.position)
| _ -> failwith "vector length is not equal to 2"
let forwardPosition cursor = cursor.position + cursor.direction
module Pipe =
let canEnter direction (Pipe(a, b)) = direction = a || direction = b
let findOutDirection inDirection =
function
| Pipe(i, other)
| Pipe(other, i) when i = inDirection -> other |> Vector.neg
| _ -> failwith "can't traverse pipe in this direction"
let walkLoop lines =
let initialLocation = lines |> Grid.findStart
let initialDirection =
Grid.allDirections
|> Seq.find (fun direction ->
lines
|> Grid.tryItemAt (initialLocation + direction)
|> Option.bind (function
| PipeItem p -> Some p
| _ -> None)
|> Option.map (Pipe.canEnter direction)
|> Option.defaultValue false)
let initialCursor =
{ position = initialLocation
direction = initialDirection }
let nextCursors =
initialCursor
|> List.unfold (fun cursor ->
let nextPosition = cursor |> Cursor.forwardPosition
match lines |> Grid.itemAt nextPosition with
| StartItem -> None
| PipeItem pipe ->
let nextCursor =
{ position = nextPosition
direction = pipe |> Pipe.findOutDirection cursor.direction }
Some(nextCursor, nextCursor)
| _ -> failwith "unexpected break in loop")
initialCursor :: nextCursors
let part1 lines =
let allCursors = walkLoop lines
(allCursors |> List.length) / 2
let part2 lines =
let allCursors = walkLoop lines
let loopPositions = allCursors |> Seq.map _.position |> Set.ofSeq
let rec expand allTiles newLocations =
let newTiles =
newLocations
|> Set.ofSeq
|> Set.filter (fun loc -> (lines |> Grid.tryCharAt loc).IsSome)
|> (fun x -> Set.difference x loopPositions)
|> (fun x -> Set.difference x allTiles)
if (newTiles.Count = 0) then
allTiles
else
expand
(allTiles |> Set.union newTiles)
((Grid.allDirections, newTiles)
||> Seq.allPairs
|> Seq.map (fun (direction, newTile) -> newTile + direction))
let cursorsWithPreviousDirections =
allCursors
|> Seq.pairwise
|> Seq.map (fun (a, b) -> { b with direction = a.direction })
|> Seq.toList
let leftPos, rightPos =
(allCursors @ cursorsWithPreviousDirections)
|> List.map Cursor.leftRightPositions
|> List.unzip
|> TupleEx.map (fun list -> expand Set.empty list)
(*
lines
|> Array.iteri (fun y line ->
line
|> Seq.iteri (fun x char ->
let pos = vector [| x; y |]
let char2 =
match char with
| 'J' -> '╯'
| 'L' -> '╰'
| '7' -> '╮'
| 'F' -> '╭'
| '-' -> '─'
| '|' -> '│'
| c -> c
Spectre.Console.AnsiConsole.Markup(
if (leftPos |> Set.contains pos) then
$"[red]{char2}[/]"
elif (rightPos |> Set.contains pos) then
$"[lime]{char2}[/]"
elif (loopPositions |> Set.contains pos) then
$"[yellow on blue]{char2}[/]"
else
$"[black on pink1]{char2}[/]"
))
printfn "")
*)
let inner =
[ leftPos; rightPos ]
|> Seq.filter (fun set ->
set
|> Set.exists (fun vec -> (Vector.get vec 0) = 0.0 || (Vector.get vec 1) = 0.0)
|> not)
|> Seq.exactlyOne
inner.Count
let run = runReadAllLines part1 part2
module Tests =
open Xunit
open Swensen.Unquote
let example1 =
[| "-L|F7" //
"7S-7|"
"L|7||"
"-L-J|"
"L|-JF" |]
[<Fact>]
let ``part 1 example 1`` () = part1 example1 =! 4
let example2 =
[| "7-F7-" //
".FJ|7"
"SJLL7"
"|F--J"
"LJ.LJ" |]
[<Fact>]
let ``part 1 example 2`` () = part1 example2 =! 8
let example3 =
[| "FF7FSF7F7F7F7F7F---7"
"L|LJ||||||||||||F--J"
"FL-7LJLJ||||||LJL-77"
"F--JF--7||LJLJ7F7FJ-"
"L---JF-JLJ.||-FJLJJ7"
"|F|F-JF---7F7-L7L|7|"
"|FFJF7L7F-JF7|JL---7"
"7-L-JL7||F7|L7F-7F7|"
"L.L7LFJ|||||FJL7||LJ"
"L7JLJL-JLJLJL--JLJ.L" |]
[<Fact>]
let ``part 2 example 2`` () = part2 example3 =! 10