-
Notifications
You must be signed in to change notification settings - Fork 0
/
day25.c3
75 lines (70 loc) · 1.13 KB
/
day25.c3
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
module day25;
import std::io;
int[256] values = { ['2'] = 2, ['1'] = 1, ['0'] = 0, ['-'] = -1, ['='] = -2 };
fn String snafu_from_long(String buffer, long l)
{
char[100] temp;
isz idx = 0;
if (l == 0)
{
return "0";
}
while (l)
{
long val = l % 5;
l /= 5;
switch (val)
{
case 0:
case 1:
case 2:
temp[idx++] = (char)(val + '0');
case 3:
l++;
temp[idx++] = '=';
case 4:
l++;
temp[idx++] = '-';
default:
unreachable();
}
}
isz len = idx;
for (int i = 0; i < len; i++)
{
buffer[len - i - 1] = temp[i];
}
buffer[idx] = 0;
return (String)buffer.ptr[:idx];
}
fn long snafu_to_long(String num)
{
long value = 0;
foreach (long i, c : num)
{
value *= 5;
value += values[c];
}
return value;
}
fn void part1()
{
File f = file::open("snafu.txt", "rb")!!;
defer (void)f.close();
long total = 0;
while (!f.eof())
{
@pool()
{
String line = io::treadline(&f)!!;
total += snafu_to_long(line);
};
}
char[100] buffer;
String res = snafu_from_long((String)&buffer, total);
io::printfn("%s", res);
}
fn void main()
{
part1();
}