From ff32e8c336818c91f2f845768c85cf4c7086b7bf Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Sat, 10 Dec 2022 01:01:44 -0500 Subject: [PATCH] day10 full --- 2022/full/day10.txt | 138 ++++++++++++++++++++++++++++++++++++++ 2022/python/anim.py | 1 - 2022/python/day10.py | 76 +++++++++++++++++++++ 2022/samples/day10.txt | 146 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 360 insertions(+), 1 deletion(-) create mode 100644 2022/full/day10.txt create mode 100644 2022/python/day10.py create mode 100644 2022/samples/day10.txt diff --git a/2022/full/day10.txt b/2022/full/day10.txt new file mode 100644 index 0000000..5edd3b8 --- /dev/null +++ b/2022/full/day10.txt @@ -0,0 +1,138 @@ +addx 1 +noop +addx 2 +noop +addx 3 +addx 3 +addx 1 +addx 5 +addx 1 +noop +noop +addx 4 +noop +noop +addx -9 +addx 16 +addx -1 +noop +addx 5 +addx -2 +addx 4 +addx -35 +addx 2 +addx 28 +noop +addx -23 +addx 3 +addx -2 +addx 2 +addx 5 +addx -8 +addx 19 +addx -8 +addx 2 +addx 5 +addx 5 +addx -14 +addx 12 +addx 2 +addx 5 +addx 2 +addx -13 +addx -23 +noop +addx 1 +addx 5 +addx -1 +addx 2 +addx 4 +addx -9 +addx 10 +noop +addx 6 +addx -11 +addx 12 +addx 5 +addx -25 +addx 30 +addx -2 +addx 2 +addx -5 +addx 12 +addx -37 +noop +noop +noop +addx 24 +addx -17 +noop +addx 33 +addx -32 +addx 3 +addx 1 +noop +addx 6 +addx -13 +addx 17 +noop +noop +noop +addx 12 +addx -4 +addx -2 +addx 2 +addx 3 +addx 4 +addx -35 +addx -2 +noop +addx 20 +addx -13 +addx -2 +addx 5 +addx 2 +addx 23 +addx -18 +addx -2 +addx 17 +addx -10 +addx 17 +noop +addx -12 +addx 3 +addx -2 +addx 2 +noop +addx 3 +addx 2 +noop +addx -13 +addx -20 +noop +addx 1 +addx 2 +addx 5 +addx 2 +addx 5 +noop +noop +noop +noop +noop +addx 1 +addx 2 +addx -18 +noop +addx 26 +addx -1 +addx 6 +noop +noop +noop +addx 4 +addx 1 +noop +noop +noop +noop diff --git a/2022/python/anim.py b/2022/python/anim.py index 65d6694..f1cb688 100644 --- a/2022/python/anim.py +++ b/2022/python/anim.py @@ -14,7 +14,6 @@ class Animate: def add_frame(self, frame): self.f_count += 1 - print(self.f_count) self.write_frame(frame) def write_frame(self, frame): diff --git a/2022/python/day10.py b/2022/python/day10.py new file mode 100644 index 0000000..1ed1704 --- /dev/null +++ b/2022/python/day10.py @@ -0,0 +1,76 @@ +import shared +from anim import Animate +import matrix + +cycles = { + "noop": 1, + "addx": 2, + } + +def part1(x): + X = 1 + cycle_count = 0 + signal_strength = [] + for step, instruction in enumerate(x): + val = None + if instruction.startswith("addx"): + opcode, val = instruction.split(" ") + val = int(val) + else: + opcode = instruction + + for x in range(cycles[opcode]): + cycle_count += 1 + + if cycle_count == 20 or ((cycle_count -20) % 40 == 0): + signal_strength.append(cycle_count*X) + + if opcode == "addx": + X += val + print(sum(signal_strength)) + + + + +def part2(x): + def cycle_to_xy(cycle): + x = cycle % 40 + y = cycle // 40 + return x,y + + screen = matrix.matrix_of_size(40,6) + X = 1 + cycle_count = 0 + for step, instruction in enumerate(x): + val = None + if instruction.startswith("addx"): + opcode, val = instruction.split(" ") + val = int(val) + else: + opcode = instruction + + for x in range(cycles[opcode]): + _x,y = cycle_to_xy(cycle_count) + criteria = (X-1, X, X+1) + cycle_count += 1 + if _x in criteria: + #print("draw", _x,y) + screen[y][_x] = 1 + + if opcode == "addx": + X += val + + matrix.ppmx(screen,pad=False, space=False) + anim = Animate(screen, day="10") + anim.add_frame(screen) + print("wrote gif-10/day10.gif") + +def main(): + rows = [row for row in shared.load(10)] + part1(rows) + print("%"*40) + part2(rows) + + +if __name__ == "__main__": + main() diff --git a/2022/samples/day10.txt b/2022/samples/day10.txt new file mode 100644 index 0000000..37ee8ee --- /dev/null +++ b/2022/samples/day10.txt @@ -0,0 +1,146 @@ +addx 15 +addx -11 +addx 6 +addx -3 +addx 5 +addx -1 +addx -8 +addx 13 +addx 4 +noop +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx -35 +addx 1 +addx 24 +addx -19 +addx 1 +addx 16 +addx -11 +noop +noop +addx 21 +addx -15 +noop +noop +addx -3 +addx 9 +addx 1 +addx -3 +addx 8 +addx 1 +addx 5 +noop +noop +noop +noop +noop +addx -36 +noop +addx 1 +addx 7 +noop +noop +noop +addx 2 +addx 6 +noop +noop +noop +noop +noop +addx 1 +noop +noop +addx 7 +addx 1 +noop +addx -13 +addx 13 +addx 7 +noop +addx 1 +addx -33 +noop +noop +noop +addx 2 +noop +noop +noop +addx 8 +noop +addx -1 +addx 2 +addx 1 +noop +addx 17 +addx -9 +addx 1 +addx 1 +addx -3 +addx 11 +noop +noop +addx 1 +noop +addx 1 +noop +noop +addx -13 +addx -19 +addx 1 +addx 3 +addx 26 +addx -30 +addx 12 +addx -1 +addx 3 +addx 1 +noop +noop +noop +addx -9 +addx 18 +addx 1 +addx 2 +noop +noop +addx 9 +noop +noop +noop +addx -1 +addx 2 +addx -37 +addx 1 +addx 3 +noop +addx 15 +addx -21 +addx 22 +addx -6 +addx 1 +noop +addx 2 +addx 1 +noop +addx -10 +noop +noop +addx 20 +addx 1 +addx 2 +addx 2 +addx -6 +addx -11 +noop +noop +noop