day10 full

This commit is contained in:
Tyrel Souza 2022-12-10 01:01:44 -05:00
parent e5969b04f8
commit ff32e8c336
4 changed files with 360 additions and 1 deletions

138
2022/full/day10.txt Normal file
View File

@ -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

View File

@ -14,7 +14,6 @@ class Animate:
def add_frame(self, frame): def add_frame(self, frame):
self.f_count += 1 self.f_count += 1
print(self.f_count)
self.write_frame(frame) self.write_frame(frame)
def write_frame(self, frame): def write_frame(self, frame):

76
2022/python/day10.py Normal file
View File

@ -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()

146
2022/samples/day10.txt Normal file
View File

@ -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