-
Notifications
You must be signed in to change notification settings - Fork 0
/
day 14.dib
206 lines (154 loc) · 6 KB
/
day 14.dib
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
#!meta
{"kernelInfo":{"defaultKernelName":"csharp","items":[{"aliases":[],"name":"csharp"},{"aliases":[],"languageName":"fsharp","name":"fsharp"}]}}
#!markdown
# --- Day 14: Parabolic Reflector Dish ---
You reach the place where all of the mirrors were pointing: a massive parabolic reflector dish attached to the side of another large mountain.
The dish is made up of many small mirrors, but while the mirrors themselves are roughly in the shape of a parabolic reflector dish, each individual mirror seems to be pointing in slightly the wrong direction. If the dish is meant to focus light, all it's doing right now is sending it in a vague direction.
This system must be what provides the energy for the lava! If you focus the reflector dish, maybe you can go where it's pointing and use the light to fix the lava production.
Upon closer inspection, the individual mirrors each appear to be connected via an elaborate system of ropes and pulleys to a large metal platform below the dish. The platform is covered in large rocks of various shapes. Depending on their position, the weight of the rocks deforms the platform, and the shape of the platform controls which ropes move and ultimately the focus of the dish.
In short: if you move the rocks, you can focus the dish. The platform even has a control panel on the side that lets you tilt it in one of four directions! The rounded rocks (O) will roll when the platform is tilted, while the cube-shaped rocks (#) will stay in place. You note the positions of all of the empty spaces (.) and rocks (your puzzle input). For example:
```
O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....
```
Start by tilting the lever so all of the rocks will slide north as far as they will go:
```
OOOO.#.O..
OO..#....#
OO..O##..O
O..#.OO...
........#.
..#....#.#
..O..#.O.O
..O.......
#....###..
#....#....
```
You notice that the support beams along the north side of the platform are damaged; to ensure the platform doesn't collapse, you should calculate the total load on the north support beams.
The amount of load caused by a single rounded rock (O) is equal to the number of rows from the rock to the south edge of the platform, including the row the rock is on. (Cube-shaped rocks (#) don't contribute to load.) So, the amount of load caused by each rock in each row is as follows:
```
OOOO.#.O.. 10
OO..#....# 9
OO..O##..O 8
O..#.OO... 7
........#. 6
..#....#.# 5
..O..#.O.O 4
..O....... 3
#....###.. 2
#....#.... 1
```
The total load is the sum of the load caused by all of the rounded rocks. In this example, the total load is 136.
Tilt the platform so that the rounded rocks all roll north. Afterward, what is the total load on the north support beams?
#!fsharp
#!import shared.fs
let parseGrid (input: string seq) =
array2D input
let sampleInput =
[|
"O....#...."
"O.OO#....#"
".....##..."
"OO.#O....O"
".O.....O#."
"O.#..O.#.#"
"..O..#O..O"
".......O.."
"#....###.."
"#OO..#...."
|]
|> parseGrid
Array2D.renderChars sampleInput
#!fsharp
let rec tilt (direction: char[,]->int->int->char) (grid: char[,]) =
let next = Array2D.init (Array2D.length1 grid) (Array2D.length2 grid) (direction grid)
if Seq.equal (Seq.cast grid) (Seq.cast next)
then grid
else tilt direction next
let direction (next: (int * int)->(int * int)) (previous: (int * int)->(int * int)) (grid: char[,]) (y: int) (x: int) =
match grid[y, x] with
| '#' -> '#'
| '.' ->
let (y', x') = next(y, x)
if y' >= 0 && y' < (Array2D.length1 grid) && x' >= 0 && x' < (Array2D.length2 grid) && grid[y', x'] = 'O'
then 'O'
else '.'
| 'O' ->
let (y', x') = previous(y, x)
if y' >= 0 && y' < (Array2D.length1 grid) && x' >= 0 && x' < (Array2D.length2 grid) && grid[y', x'] = '.'
then '.'
else 'O'
| _ -> raise (InvalidOperationException $"Encountered unexpected character {grid[y,x]}")
let north = direction (fun (y, x) -> (y+1, x)) (fun (y, x) -> (y-1, x))
let south = direction (fun (y, x) -> (y-1, x)) (fun (y, x) -> (y+1, x))
let west = direction (fun (y, x) -> (y, x+1)) (fun (y, x) -> (y, x-1))
let east = direction (fun (y, x) -> (y, x-1)) (fun (y, x) -> (y, x+1))
sampleInput
|> tilt north
|> Array2D.renderChars
#!markdown
Looks good. Second step - calculate the total load
#!fsharp
let totalLoad (grid: char[,]) =
let len1 = Array2D.length1 grid
grid |> Array2D.foldi (fun y _ load -> function | 'O' -> len1 - y + load | _ -> load) 0
sampleInput
|> tilt north
|> totalLoad
#!fsharp
let challengeInput =
File.ReadAllLines "day 14 input.txt"
|> array2D
challengeInput
|> tilt north
|> totalLoad
#!fsharp
let cycle =
tilt north
>> tilt west
>> tilt south
>> tilt east
sampleInput
|> cycle
|> Array2D.renderChars
#!markdown
Looks good. Let's try this for 1000000000 cycles on the sample
#!fsharp
type Cycles = {
start: int
length: int
grids: Map<int, char[,]>
}
let rec findCycles' (memo: Dictionary<char [,], int>) (input: char[,]) (index: int) =
match memo.TryGetValue input with
| (true, i) -> {
start = i
length = (memo.Values |> Seq.max) - i + 1
grids = memo |> Seq.map (fun kvp -> (kvp.Value, kvp.Key)) |> Map
}
| (false, _) ->
memo[input] <- index
findCycles' memo (cycle input) (index+1)
let findCycles (input: char[,]) = findCycles' (Dictionary<char[,], int>(Array2DComparer<char>())) input 0
sampleInput |> findCycles
#!fsharp
let repeat (i: int) (input: char[,]) =
let cycles = findCycles input
if i < cycles.start
then cycles.grids[i]
else cycles.grids[(i - cycles.start) % cycles.length + cycles.start]
sampleInput |> repeat 1_000_000_000 |> totalLoad
#!fsharp
challengeInput |> repeat 1_000_000_000 |> totalLoad
#!fsharp
challengeInput |> findCycles
#!markdown
What I should really do is figure out the "cycle" length, and use that to determine the value at a given point.