solve_day10 :: (test: bool) {
contents := read_entire_file(ifx test then "inputs/day10_test.txt" else "inputs/day10.txt");
lines := split(contents, "\n");
part1 := 0;
cycles := 0;
register := 1;
crt: string = xx NewArray(40 * 6, u8);
memset(crt.data, #char ".", crt.count);
for line: lines {
if begins_with(line, "addx ") {
draw_pixel(cycles, register, crt);
cycles += 1;
part1 += check_signal(register, cycles);
draw_pixel(cycles, register, crt);
cycles += 1;
part1 += check_signal(register, cycles);
register += string_to_int(slice(line, 5, line.count), 10, s64);
} else if begins_with(line, "noop") {
draw_pixel(cycles, register, crt);
cycles += 1;
part1 += check_signal(register, cycles);
}
}
print("Part 1: %\n", part1);
print("Part 2:\n");
for 0..5 {
print("%\n", slice(crt, 40 * it, 40));
}
}
draw_pixel :: (cycle: s64, register: s64, crt: string) {
p := cycle % 40;
if register >= p - 1 && register <= p + 1 {
crt[cycle] = #char "#";
}
}
check_signal :: (register: s64, cycles: s64) -> s64 {
if cycles == {
case 20; #through;
case 60; #through;
case 100; #through;
case 140; #through;
case 180; #through;
case 220;
return cycles * register;
}
return 0;
}