-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.c3
103 lines (98 loc) · 2.15 KB
/
day10.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
93
94
95
96
97
98
99
100
101
102
103
module day10;
import std::io;
fn int update_signal_strength(int cycle, int x)
{
if ((cycle + 20) % 40 == 0)
{
return x * cycle;
}
return 0;
}
fn void draw_crt(bool[40][6]* crt)
{
foreach (bool[40]* &row : *crt)
{
foreach(pixel : *row)
{
io::print(pixel ? "#" : " ");
}
io::printn();
}
}
fn void update_crt(bool[40][6]* crt, int cycle, int x)
{
int col = (cycle - 1) % 40;
int row = (cycle - 1) / 40;
if (x - 1 <= col && x + 1 >= col)
{
(*crt)[row][col] = true;
}
}
fn void part2()
{
File f = file::open("code.txt", "rb")!!;
defer (void)f.close();
bool[40][6] crt;
int cycle = 0;
int x = 1;
while (!f.eof())
{
@pool()
{
String line = io::treadline(&f)!!;
String[] commands = line.tsplit(" ");
switch (commands[0])
{
case "noop":
cycle++;
update_crt(&crt, cycle, x);
case "addx":
cycle++;
update_crt(&crt, cycle, x);
cycle++;
update_crt(&crt, cycle, x);
x += commands[1].to_int()!!;
default:
unreachable("Invalid input");
}
};
}
draw_crt(&crt);
}
fn void part1()
{
File f = file::open("code.txt", "rb")!!;
defer (void)f.close();
int cycle = 0;
int x = 1;
int signal_strength = 0;
while (!f.eof())
{
@pool()
{
String line = io::treadline(&f)!!;
String[] commands = line.tsplit(" ");
switch (commands[0])
{
case "noop":
cycle++;
signal_strength += update_signal_strength(cycle, x);
case "addx":
cycle++;
signal_strength += update_signal_strength(cycle, x);
cycle++;
signal_strength += update_signal_strength(cycle, x);
x += commands[1].to_int()!!;
default:
unreachable("Invalid input");
}
if (cycle > 222) break;
};
}
io::printfn("Signal strength: %d", signal_strength);
}
fn void main()
{
part1();
part2();
}