Advent of Code 2022 - 10

Day 10: Cathode-Ray Tube

https://adventofcode.com/2022/day/10

Part One

1
2
3
4
5
6
7
with open('10.txt', 'r') as fp:
instrs = fp.read()
instrs = instrs.replace('noop', '0').replace('addx ', '0\n')
instrs = [int(x) for x in instrs.split()]
reg_x = np.cumsum([1]+instrs)
target_idx = np.arange(20, 221, 40)
print(sum([reg_x[i]*i for i in target_idx]))

Answer: 15120

Part Two

1
2
3
4
5
6
7
8
9
10
11
12
with open('10.txt', 'r') as fp:
instrs = fp.read()
instrs = instrs.replace('noop', '0').replace('addx ', '0\n')
instrs = [int(x) for x in instrs.split()]
reg_x = np.cumsum([1]+instrs)
crt_idx = np.arange(len(reg_x)) % 40
visible = (crt_idx >= reg_x - 1) & (crt_idx <= reg_x + 1)
visible = visible[:240].reshape(6, 40).astype(int)
for line in visible:
for v in line:
print('▉' if v else ' ', end='')
print()

Answer: RKPJBPLA

1
2
3
4
5
6
▉▉▉  ▉  ▉ ▉▉▉    ▉▉ ▉▉▉  ▉▉▉  ▉     ▉▉  
▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉
▉ ▉ ▉▉ ▉ ▉ ▉ ▉▉▉ ▉ ▉ ▉ ▉ ▉
▉▉▉ ▉ ▉ ▉▉▉ ▉ ▉ ▉ ▉▉▉ ▉ ▉▉▉▉
▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉
▉ ▉ ▉ ▉ ▉ ▉▉ ▉▉▉ ▉ ▉▉▉▉ ▉ ▉