-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdayP.toit
39 lines (35 loc) · 789 Bytes
/
dayP.toit
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
import .aoc
import .resources
// First install the host package with `jag pkg install`.
// Run with `jag run -d host template.toit`
TR ::= {
'0': 0,
'1': 1,
'2': 2,
'-': -1,
'=': -2,
}
CHARS := "=-012"
main:
total :=
sum (INPUTP.trim.split "\n"): | txt/string |
l /List := List txt.size: TR[txt[it]]
l.reduce: | t c | t * 5 + c
base5 := total.stringify 5
print base5
l := List base5.size: base5[it] - '0'
adjust := 0
for i := l.size - 1; i >= 0; i--:
value := l[i] + adjust
adjust = 0
while value < -2:
value += 5
adjust--
while value > 2:
value -= 5
adjust++
l[i] = CHARS[value + 2]
if adjust != 0:
l = [CHARS[adjust + 2]] + l
ba := ByteArray l.size: l[it]
print ba.to_string