-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay25.roc
78 lines (57 loc) · 1.76 KB
/
Day25.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
interface Day25 exposes [ output ] imports [ TestUtil ]
output : List I64 -> List (List I64)
output = \puzzleInput ->
testData = parseData testInput
puzzleData = parseData puzzleInput
[ TestUtil.verify 25 1 1 (firstResult testData ) 14897079
, TestUtil.show 25 1 (firstResult puzzleData)
]
# first part
firstResult : [ Pair I64 I64 ] -> I64
firstResult = \data ->
when data is
Pair key1 key2 ->
size = loopSize 7 key1 1 0
transform key2 1 size
loopSize : I64, I64, I64, I64 -> I64
loopSize = \subject, publicKey, value, size ->
if value == publicKey then
size
else
when (value * subject) % 20201227 is
Ok newValue ->
loopSize subject publicKey newValue (size + 1)
_ ->
0
transform : I64, I64, I64 -> I64
transform = \subject, value, size ->
if size > 0 then
when (value * subject) % 20201227 is
Ok newValue ->
transform subject newValue (size - 1)
_ ->
0
else
value
# parser
parseData : List I64 -> [ Pair I64 I64 ]
parseData = \input ->
parseDataHelper input 0 0 -1
parseDataHelper : List I64, I64, I64, I64 -> [ Pair I64 I64 ]
parseDataHelper = \input, pos, num1, num2 ->
when List.get input pos is
Ok 10 ->
parseDataHelper input (pos + 1) num1 0
Ok val ->
if num2 < 0 then
parseDataHelper input (pos + 1) (num1 * 10 + val - 48) num2
else
parseDataHelper input (pos + 1) num1 (num2 * 10 + val - 48)
_ ->
Pair num1 num2
# test data
testInput : List I64
testInput =
[ 53, 55, 54, 52, 56, 48, 49, 10
, 49, 55, 56, 48, 55, 55, 50, 52
]