-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday04.roc
More file actions
204 lines (181 loc) · 6.23 KB
/
day04.roc
File metadata and controls
204 lines (181 loc) · 6.23 KB
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
app [part1, part2] {
pf: platform "https://github.com/ostcar/roc-aoc-platform/releases/download/v0.0.8/lhFfiil7mQXDOB6wN-jduJQImoT8qRmoiNHDB4DVF9s.tar.br",
parser: "https://github.com/lukewilliamboswell/roc-parser/releases/download/0.9.0/w8YKp2YAgQt5REYk912HfKAHBjcXsrnvtjI0CBzoAT4.tar.br",
}
import parser.Parser exposing [Parser, many, map, oneOf, sepBy]
import parser.String exposing [parseStr, codeunit, codeunitSatisfies]
example : Str
example =
"""
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
"""
expect
got = part1 example
expected = Ok "18"
got == expected
part1 : Str -> Result Str [ParsingFailure Str, ParsingIncomplete Str]
part1 = \rawInput ->
parseStr puzzleParser (rawInput |> Str.trim)
|> Result.map
\input ->
searchXMAS input Part1
|> Num.toStr
Map : List (List Symbol)
Symbol : [X, M, A, S, Other]
puzzleParser : Parser (List U8) Map
puzzleParser =
lineParser |> sepBy (codeunit '\n')
lineParser : Parser (List U8) (List Symbol)
lineParser =
many symbolParser
symbolParser : Parser (List U8) Symbol
symbolParser =
oneOf [
codeunit 'X' |> map \_ -> X,
codeunit 'M' |> map \_ -> M,
codeunit 'A' |> map \_ -> A,
codeunit 'S' |> map \_ -> S,
codeunitSatisfies (\c -> c != '\n') |> map \_ -> Other,
]
PuzzlePart : [Part1, Part2]
Index : U64
Expectation1 : { m : Index, a : Index, s : Index }
Expectation2 : { m : Index, a : Index, s1 : Index, s2 : Index }
searchXMAS : Map, PuzzlePart -> U64
searchXMAS = \m, part ->
lineLength = m |> List.first |> Result.map (\l -> List.len l) |> Result.withDefault 0
maxIndex = (List.len m * lineLength) - 1
longLine =
m
|> List.walk
[]
\state, line ->
state |> List.concat line
when part is
Part1 ->
a = searchForwardHelperPartOne longLine 0 lineLength maxIndex []
b = searchForwardHelperPartOne (List.reverse longLine) 0 lineLength maxIndex []
a + b
Part2 ->
a = searchForwardHelperPartTwo longLine 0 lineLength maxIndex []
b = searchForwardHelperPartTwo (List.reverse longLine) 0 lineLength maxIndex []
a + b
searchForwardHelperPartOne : List Symbol, Index, U64, Index, List Expectation1 -> U64
searchForwardHelperPartOne = \symbols, index, lineLength, maxIndex, expectations ->
when symbols is
[] ->
List.len expectations
[symbol, .. as rest] ->
newExp = checkExpectationsPartOne expectations index symbol lineLength maxIndex
searchForwardHelperPartOne rest (index + 1) lineLength maxIndex newExp
checkExpectationsPartOne : List Expectation1, Index, Symbol, U64, Index -> List Expectation1
checkExpectationsPartOne = \exps, index, symbol, lineLength, maxIndex ->
exps
|> List.dropIf
\exp ->
if exp.m == index then
symbol != M
else if exp.a == index then
symbol != A
else if exp.s == index then
symbol != S
else
Bool.false
|> \e ->
if symbol == X then
e |> List.concat (newExpsPartOne index lineLength maxIndex)
else
e
newExpsPartOne : Index, U64, Index -> List Expectation1
newExpsPartOne = \index, lineLength, maxIndex ->
okToEast = index % lineLength + 4 <= lineLength
okToWest = index % lineLength >= 3
okToSouth = index + (3 * lineLength) <= maxIndex
a =
if okToEast then
[{ m: index + 1, a: index + 2, s: index + 3 }]
else
[]
b =
if okToEast && okToSouth then
a |> List.append { m: index + 1 + lineLength, a: index + 2 + 2 * lineLength, s: index + 3 + 3 * lineLength }
else
a
c =
if okToSouth then
b |> List.append { m: index + lineLength, a: index + 2 * lineLength, s: index + 3 * lineLength }
else
b
if okToWest && okToSouth then
c |> List.append { m: index - 1 + lineLength, a: index - 2 + 2 * lineLength, s: index - 3 + 3 * lineLength }
else
c
expect
got = part2 example
expected = Ok "9"
got == expected
example2 =
"""
S.M.S
.A.A.
S.M.S
"""
expect
got = part2 example2
expected = Ok "2"
got == expected
part2 : Str -> Result Str [ParsingFailure Str, ParsingIncomplete Str]
part2 = \rawInput ->
parseStr puzzleParser (rawInput |> Str.trim)
|> Result.map
\input ->
searchXMAS input Part2
|> Num.toStr
searchForwardHelperPartTwo : List Symbol, Index, U64, Index, List Expectation2 -> U64
searchForwardHelperPartTwo = \symbols, index, lineLength, maxIndex, expectations ->
when symbols is
[] ->
List.len expectations
[symbol, .. as rest] ->
newExp = checkExpectationsPartTwo expectations index symbol lineLength maxIndex
searchForwardHelperPartTwo rest (index + 1) lineLength maxIndex newExp
checkExpectationsPartTwo : List Expectation2, Index, Symbol, U64, Index -> List Expectation2
checkExpectationsPartTwo = \exps, index, symbol, lineLength, maxIndex ->
exps
|> List.dropIf
\exp ->
if exp.m == index then
symbol != M
else if exp.a == index then
symbol != A
else if exp.s1 == index then
symbol != S
else if exp.s2 == index then
symbol != S
else
Bool.false
|> \e ->
if symbol == M then
e |> List.concat (newExpsPartTwo index lineLength maxIndex)
else
e
newExpsPartTwo : Index, U64, Index -> List Expectation2
newExpsPartTwo = \index, lineLength, maxIndex ->
okToEast = index % lineLength + 3 <= lineLength
okToSouth = index + (2 * lineLength) <= maxIndex
if okToEast && okToSouth then
[
{ m: index + 2, a: index + 1 + lineLength, s1: index + 2 * lineLength, s2: index + 2 + 2 * lineLength },
{ m: index + 2 * lineLength, a: index + 1 + lineLength, s1: index + 2, s2: index + 2 + 2 * lineLength },
]
else
[]