-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.roc
71 lines (63 loc) · 1.85 KB
/
day01.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
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, const, keep, many, map, maybe, skip]
import parser.String exposing [parseStr, digits, string]
examplePart1 : Str
examplePart1 =
"""
3 4
4 3
2 5
1 3
3 9
3 3
"""
expect
got = part1 examplePart1
expected = Ok "11"
got == expected
part1 : Str -> Result Str _
part1 = \rawInput ->
parseStr puzzleParser rawInput
|> Result.map
\input ->
left = input.left |> List.sortAsc
right = input.right |> List.sortAsc
List.map2 left right (\a, b -> Num.absDiff a b)
|> List.sum
|> Num.toStr
puzzleParser : Parser (List U8) { left : List U64, right : List U64 }
puzzleParser =
many
(
const (\a -> \b -> (a, b))
|> keep digits
|> skip (string " ")
|> keep digits
|> skip (maybe (string "\n"))
)
|> map \lines ->
lines
|> List.walk
{ left: [], right: [] }
\state, (a, b) ->
{ left: state.left |> List.append a, right: state.right |> List.append b }
examplePart2 = examplePart1
expect
got = part2 examplePart2
expected = Ok "31"
got == expected
part2 : Str -> Result Str _
part2 = \rawInput ->
parseStr puzzleParser rawInput
|> Result.map
\input ->
input.left
|> List.map
\a ->
count = input.right |> List.countIf \b -> a == b
a * count
|> List.sum
|> Num.toStr