-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay01.roc
85 lines (64 loc) · 2.28 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
interface Day01 exposes [ output ] imports [ ListExtra, ListZip, TestUtil ]
output : List I64 -> List (List I64)
output = \puzzleInput ->
sortedTestInput = testInput |> TestUtil.ints |> ListExtra.quicksort
sortedPuzzleInput = puzzleInput |> TestUtil.ints |> ListExtra.quicksort
[ TestUtil.verify 1 1 1 (solution2 sortedTestInput ) 514579
, TestUtil.show 1 1 (solution2 sortedPuzzleInput)
, TestUtil.verify 1 2 1 (solution3 sortedTestInput ) 241861950
, TestUtil.show 1 2 (solution3 sortedPuzzleInput)
]
solution2 : List I64 -> I64
solution2 = \list ->
lo = ListZip.newAtFirst list 0
hi = ListZip.last lo list
search2Values list lo hi
search2Values : List I64, ListZip.Zip, ListZip.Zip -> I64
search2Values = \list, lo, hi ->
sum = lo.val + hi.val
if sum == 2020 then
lo.val * hi.val
else if sum < 2020 then
nextLo = ListZip.forward lo list
search2Values list nextLo hi
else
nextHi = ListZip.backward hi list
search2Values list lo nextHi
solution3 : List I64 -> I64
solution3 = \list ->
lo = ListZip.newAtFirst list 0
mid = ListZip.forward lo list
hi = ListZip.last lo list
search3Values list lo mid hi
search3Values : List I64, ListZip.Zip, ListZip.Zip, ListZip.Zip -> I64
search3Values = \list, lo, mid, hi ->
sum = lo.val + mid.val + hi.val
if sum == 2020 then
lo.val * mid.val * hi.val
else if sum < 2020 then
if mid.idx + 1 < hi.idx then
nextMid = ListZip.forward mid list
search3Values list lo nextMid hi
else
nextLo = ListZip.forward lo list
nextMid = ListZip.forward nextLo list
nextHi = ListZip.last nextLo list
search3Values list nextLo nextMid nextHi
else
if mid.idx < hi.idx - 1 then
nextHi = ListZip.backward hi list
search3Values list lo mid nextHi
else
nextLo = ListZip.forward lo list
nextMid = ListZip.forward nextLo list
nextHi = ListZip.last nextLo list
search3Values list nextLo nextMid nextHi
testInput : List I64
testInput =
[ 49, 55, 50, 49, 10
, 57, 55, 57, 10
, 51, 54, 54, 10
, 50, 57, 57, 10
, 54, 55, 53, 10
, 49, 52, 53, 54, 10
]