diff --git a/code.txt b/code.txt new file mode 100644 index 0000000..f7e3cf8 --- /dev/null +++ b/code.txt @@ -0,0 +1,138 @@ +addx 1 +addx 5 +noop +addx -1 +noop +addx 3 +addx 29 +addx -1 +addx -21 +addx 5 +noop +addx -20 +addx 21 +addx 2 +addx 8 +addx -1 +noop +noop +noop +noop +addx 6 +addx -1 +addx -37 +addx 40 +addx -10 +addx -25 +addx 5 +addx 2 +addx 5 +noop +noop +noop +addx 21 +addx -20 +addx 2 +noop +addx 3 +addx 2 +addx -5 +addx 12 +addx 3 +noop +addx 2 +addx 3 +addx -2 +addx -37 +addx 1 +addx 5 +addx 3 +addx -2 +addx 2 +addx 29 +addx -22 +addx 13 +noop +addx -8 +addx -6 +addx 7 +addx 2 +noop +addx 7 +addx -2 +addx 5 +addx 2 +addx -26 +addx -11 +noop +noop +addx 6 +addx 1 +addx 1 +noop +addx 4 +addx 5 +noop +noop +addx -2 +addx 3 +noop +addx 2 +addx 5 +addx 2 +addx -22 +addx 27 +addx -1 +addx 1 +addx 5 +addx 2 +noop +addx -39 +addx 22 +noop +addx -15 +addx 3 +addx -2 +addx 2 +addx -2 +addx 9 +addx 3 +noop +addx 2 +addx 3 +addx -2 +addx 2 +noop +noop +noop +addx 5 +addx -17 +addx 24 +addx -7 +addx 8 +addx -36 +addx 2 +addx 3 +addx 33 +addx -32 +addx 4 +addx 1 +noop +addx 5 +noop +noop +addx 20 +addx -15 +addx 4 +noop +addx 1 +noop +addx 4 +addx 6 +addx -30 +addx 30 +noop +noop +noop +noop +noop \ No newline at end of file diff --git a/day10.c3 b/day10.c3 new file mode 100644 index 0000000..4e4fdfc --- /dev/null +++ b/day10.c3 @@ -0,0 +1,105 @@ +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::println(); + } +} + +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; + f.open("code.txt", "rb")!!; + defer catch(f.close()); + bool[40][6] crt; + int cycle = 0; + int x = 1; + while (!f.eof()) + { + @pool() + { + char[] line = f.tgetline(); + char[][] commands = str::tsplit(line, " "); + 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 += str::to_int(commands[1])!!; + default: + unreachable("Invalid input"); + } + }; + } + draw_crt(&crt); +} +fn void part1() +{ + File f; + f.open("code.txt", "rb")!!; + defer catch(f.close()); + int cycle = 0; + int x = 1; + int signal_strength = 0; + while (!f.eof()) + { + @pool() + { + char[] line = f.tgetline(); + char[][] commands = str::tsplit(line, " "); + 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 += str::to_int(commands[1])!!; + default: + unreachable("Invalid input"); + } + if (cycle > 222) break; + }; + } + io::printfln("Signal strength: %d", signal_strength); +} + +fn void main() +{ + part1(); + part2(); +} \ No newline at end of file