-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.roc
139 lines (119 loc) · 3.55 KB
/
day08.roc
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
app [part1, part2] {
pf: platform "https://github.com/ostcar/roc-aoc-platform/releases/download/v0.0.8/lhFfiil7mQXDOB6wN-jduJQImoT8qRmoiNHDB4DVF9s.tar.br",
}
example =
"""
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
"""
expect
got = part1 example
expected = Ok "14"
got == expected
part1 = \input ->
{ map, maxCol, maxRow } = parse input
map
|> Dict.walk [] \acc, _, possitions ->
addPairs possitions acc
|> List.walk [] \acc, (pos1, pos2) ->
calcAntinode acc pos1 pos2
|> List.keepIf \pos -> onMap pos maxCol maxRow
|> countUnique
|> Num.toStr
|> Ok
Position : { col : I64, row : I64 }
Map : Dict U8 (List Position)
parse : Str -> { map : Map, maxRow : I64, maxCol : I64 }
parse = \input ->
map =
input
|> Str.splitOn "\n"
|> List.walkWithIndex (Dict.empty {}) \acc, line, row ->
line
|> Str.toUtf8
|> List.walkWithIndex acc \acc2, char, col ->
if char == '.' then
acc2
else
acc2
|> Dict.update char \value ->
value
|> Result.withDefault []
|> List.append { row: row |> Num.toI64, col: col |> Num.toI64 }
|> Ok
maxCol =
input
|> Str.toUtf8
|> List.findFirstIndex \c -> c == '\n'
|> Result.withDefault 0
|> Num.sub 1
|> Num.toI64
maxRow =
input
|> Str.toUtf8
|> List.countIf \c -> c == '\n'
|> Num.toI64
{ map, maxCol, maxRow }
addPairs = \list, result ->
when list is
[] | [_] -> result
[a, .. as rest] ->
newResult =
rest
|> List.walk result \acc, b ->
List.append acc (a, b)
addPairs rest newResult
calcAntinode = \list, { col: colA, row: rowA }, { col: colB, row: rowB } ->
list
|> List.append { col: colA + colA - colB, row: rowA + rowA - rowB }
|> List.append { col: colB + colB - colA, row: rowB + rowB - rowA }
expect
a1 = { col: 4, row: 3 }
a2 = { col: 5, row: 5 }
got = calcAntinode [] a1 a2
expected = [{ col: 3, row: 1 }, { col: 6, row: 7 }]
got == expected
onMap = \{ row, col }, maxCol, maxRow ->
row >= 0 && row <= maxRow && col >= 0 && col <= maxCol
countUnique = \list ->
list
|> Set.fromList
|> Set.len
expect
got = part2 example
expected = Ok "34"
got == expected
part2 = \input ->
{ map, maxCol, maxRow } = parse input
map
|> Dict.walk [] \acc, _, possitions ->
addPairs possitions acc
|> List.walk [] \acc, pair ->
walkOnMap maxCol maxRow pair acc
|> countUnique
|> Num.toStr
|> Ok
walkOnMap = \maxCol, maxRow, ({ col: colA, row: rowA }, { col: colB, row: rowB }), result ->
colDiff = colA - colB
rowDiff = rowA - rowB
result
|> List.append { col: colA, row: rowA }
|> addPosStep maxCol maxRow { col: colA, row: rowA } colDiff rowDiff
|> addPosStep maxCol maxRow { col: colA, row: rowA } -colDiff -rowDiff
addPosStep = \result, maxCol, maxRow, { col, row }, colStep, rowStep ->
newPos = { col: col + colStep, row: row + rowStep }
if onMap newPos maxCol maxRow then
newResult = result |> List.append newPos
addPosStep newResult maxCol maxRow newPos colStep rowStep
else
result