-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.c3
92 lines (81 loc) · 1.55 KB
/
day3.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
module day3;
import std::io;
import std::collections::map;
def CharSet = HashMap(<char, char>);
fn int evaluate(char c)
{
int value = c > 'Z' ? c - 'a' + 1 : c - 'A' + 27;
assert(value >= 1 && value <= 52);
return value;
}
fn void part1()
{
File f = file::open("rucksack.txt", "rb")!!;
defer (void)f.close();
int total = 0;
while NEXT: (!f.eof())
{
@pool()
{
CharSet set;
set.temp_init();
set.clear();
String line = io::treadline(&f)!!;
assert(line.len % 2 == 0 && line.len > 0);
String comp1 = line[:line.len / 2];
String comp2 = line[line.len / 2..];
foreach (c : comp1) set.set(c, c);
foreach (c : comp2)
{
if (@ok(set.get(c)))
{
total += evaluate(c);
continue NEXT;
}
}
unreachable();
};
}
io::printfn("The sum of priorities is: %d", total);
}
fn void part2()
{
File f = file::open("rucksack.txt", "rb")!!;
defer (void)f.close();
int total = 0;
while NEXT: (!f.eof())
{
@pool()
{
CharSet set;
CharSet set2;
set.temp_init();
set2.temp_init();
String line1 = io::treadline(&f)!!;
String line2 = io::treadline(&f)!!;
String line3 = io::treadline(&f)!!;
assert(line1.len && line2.len && line3.len);
foreach (c : line1) set.set(c, c);
foreach (c : line2)
{
if (@ok(set.get(c))) set2.set(c, c);
}
foreach (c : line3)
{
if (@ok(set2.get(c)))
{
set2.set(c, c);
total += evaluate(c);
continue NEXT;
}
}
unreachable();
};
}
io::printfn("The sum of badges is: %d", total);
}
fn void main()
{
part1();
part2();
}