-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.roc
79 lines (69 loc) · 1.67 KB
/
day02.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
app [part1, part2] {
pf: platform "https://github.com/ostcar/roc-aoc-platform/releases/download/v0.0.8/lhFfiil7mQXDOB6wN-jduJQImoT8qRmoiNHDB4DVF9s.tar.br",
}
examplePart =
"""
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9
"""
expect part1 examplePart == Ok "2"
part1 : Str -> Result Str _
part1 = \input ->
input
|> parse
|> try
|> List.countIf isSave
|> Num.toStr
|> Ok
parse = \input ->
input
|> Str.splitOn "\n"
|> List.mapTry \line ->
line
|> Str.splitOn " "
|> List.mapTry \n ->
n
|> Str.toU64
isSave = \report ->
when report is
[] -> Bool.false
[_] -> Bool.true
[first, second, ..] ->
asc = first < second
report
|> List.dropFirst 1
|> List.walkTry first \before, current ->
if before == current then
Err Unequal
else if asc && before > current || !asc && before < current then
Err Unordered
else if Num.absDiff before current > 3 then
Err BigDiffer
else
Ok current
|> Result.isOk
expect
got = part2 examplePart
expected = Ok "4"
got == expected
part2 = \input ->
input
|> parse
|> try
|> List.countIf \report -> withTolerance report isSave
|> Num.toStr
|> Ok
withTolerance = \list, func ->
if func list then
Bool.true
else
{ start: At 0, end: Before (List.len list) }
|> List.range
|> List.any \index ->
list
|> List.dropAt index
|> func