-
Notifications
You must be signed in to change notification settings - Fork 0
/
day 3.dib
158 lines (119 loc) · 4.31 KB
/
day 3.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
#!meta
{"kernelInfo":{"defaultKernelName":"fsharp","items":[{"aliases":[],"languageName":"fsharp","name":"fsharp"}]}}
#!markdown
# --- Day 3: Gear Ratios ---
You and the Elf eventually reach a gondola lift station; he says the gondola lift will take you up to the water source, but this is as far as he can bring you. You go inside.
It doesn't take long to find the gondolas, but there seems to be a problem: they're not moving.
> "Aaah!"
You turn around to see a slightly-greasy Elf with a wrench and a look of surprise. "Sorry, I wasn't expecting anyone! The gondola lift isn't working right now; it'll still be a while before I can fix it." You offer to help.
The engineer explains that an engine part seems to be missing from the engine, but nobody can figure out which one. If you can add up all the part numbers in the engine schematic, it should be easy to work out which part is missing.
The engine schematic (your puzzle input) consists of a visual representation of the engine. There are lots of numbers and symbols you don't really understand, but apparently any number adjacent to a symbol, even diagonally, is a "part number" and should be included in your sum. (Periods (.) do not count as a symbol.)
Here is an example engine schematic:
```
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
```
In this schematic, two numbers are not part numbers because they are not adjacent to a symbol: 114 (top right) and 58 (middle right). Every other number is adjacent to a symbol and so is a part number; their sum is 4361.
Of course, the actual engine schematic is much larger. What is the sum of all of the part numbers in the engine schematic?
#!markdown
Start with type definitions. I think I will keep the data in lines, but each line should list
- components (may or may not be engine parts) - with a start and a length
- symbols (with a location)
#!fsharp
#!import shared.fs
open System.Text.RegularExpressions
open Shared
[<Struct>]
type Component = {
location: Point
length: int
number: int
}
[<Struct>]
type Symbol = {
location: Point
symbol: char
}
type Line = {
y: int
components: Component list
symbols: Symbol list
}
let regex = Regex(@"\d+|[^\.]")
let parse (y: int) (lineStr: string) =
let mutable components = []
let mutable symbols = []
let line = lineStr.AsSpan()
let mutable regexMatches = regex.EnumerateMatches line
while regexMatches.MoveNext() do
let current = regexMatches.Current
let subspan = line.Slice (current.Index, current.Length)
match Int32.TryParse subspan with
| true, int ->
components <- {
location = { x = current.Index; y = y }
length = current.Length
number = int
}::components
| _ ->
symbols <- {
location = { x = current.Index; y = y }
symbol = subspan[0]
}::symbols
{
y = y
components = components
symbols = symbols
}
let sampleInput = [|
"467..114.."
"...*......"
"..35..633."
"......#..."
"617*......"
".....+.58."
"..592....."
"......755."
"...$.*...."
".664.598.."
|]
sampleInput
|> Array.mapi parse
#!markdown
Looks like that's parsing correctly. Let's figure out how to tell if a component is an engine part
#!fsharp
type Engine = Line array
let symbolIsInRange (comp: Component) (symbol: Symbol) =
symbol.location.x >= comp.location.x - 1 && symbol.location.x <= comp.location.x + comp.length
let isEnginePart (engine: Engine) (comp: Component) =
let firstLine = max 0 (comp.location.y - 1)
let lastLine = min (engine.Length - 1) (comp.location.y + 1)
engine[firstLine..lastLine]
|> Seq.collect (fun line -> line.symbols)
|> Seq.exists (symbolIsInRange comp)
let sampleEngine = Array.mapi parse sampleInput
let findAllParts (engine: Engine) =
engine
|> Seq.collect (fun line -> line.components)
|> Seq.filter (isEnginePart engine)
|> Seq.map (fun comp -> comp.number)
sampleEngine
|> findAllParts
|> Seq.reduce (+)
#!fsharp
let inputEngine =
File.ReadAllLinesAsync "day 3 input.txt"
|> Async.AwaitTask
|> Async.RunSynchronously
|> Array.mapi parse
inputEngine
|> findAllParts
|> Seq.reduce (+)