diff --git a/py-ver/10.py b/py-ver/10.py new file mode 100644 index 0000000..1f2e975 --- /dev/null +++ b/py-ver/10.py @@ -0,0 +1,217 @@ +with open("10.txt") as f: + data = f.read() + +ex_data = """noop +addx 3 +addx -5""" + +ex2_data = """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""" + +def solve1(data): + cycle = 1 + cycle_val = dict() + reg = 1 + + for line in data.splitlines(): + parts = line.strip().split(" ") + if parts[0] == "noop": + cycle_val[cycle] = cycle * reg + cycle += 1 + elif parts[0] == "addx": + cycle_val[cycle] = cycle * reg + cycle_val[cycle + 1] = (cycle + 1) * reg + cycle += 2 + dx = int(parts[1]) + reg += dx + + s = 0 + want = [20, 60, 100, 140, 180, 220] + for id in want: + s += cycle_val[id] + print(s) + +def solve2(data): + cycle = 1 + reg = 1 + + crt = dict() + for x in range(40): + for y in range(6): + crt[x, y] = "." + + def topos(n): return (n % 40, n // 40) + + def draw(): + px = cycle - 1 + if (px % 40) in [reg - 1, reg, reg + 1]: + pos = topos(px) + crt[pos] = "#" + + for line in data.splitlines(): + parts = line.strip().split(" ") + if parts[0] == "noop": + draw() + cycle += 1 + elif parts[0] == "addx": + draw() + cycle += 1 + draw() + cycle += 1 + dx = int(parts[1]) + reg += dx + + for y in range(6): + for x in range(40): + print(crt[x, y], end="") + print() + +solve1(ex2_data) +solve1(data) + +solve2(ex2_data) +solve2(data) diff --git a/py-ver/10.txt b/py-ver/10.txt new file mode 100644 index 0000000..eb3d000 --- /dev/null +++ b/py-ver/10.txt @@ -0,0 +1,142 @@ +addx 2 +addx 4 +noop +noop +addx 17 +noop +addx -11 +addx -1 +addx 4 +noop +noop +addx 6 +noop +noop +addx -14 +addx 19 +noop +addx 4 +noop +noop +addx 1 +addx 4 +addx -20 +addx 21 +addx -38 +noop +addx 7 +noop +addx 3 +noop +addx 22 +noop +addx -17 +addx 2 +addx 3 +noop +addx 2 +addx 3 +noop +addx 2 +addx -8 +addx 9 +addx 2 +noop +noop +addx 7 +addx 2 +addx -27 +addx -10 +noop +addx 37 +addx -34 +addx 30 +addx -29 +addx 9 +noop +addx 2 +noop +noop +noop +addx 5 +addx -4 +addx 9 +addx -2 +addx 7 +noop +noop +addx 1 +addx 4 +addx -1 +noop +addx -19 +addx -17 +noop +addx 1 +addx 4 +addx 3 +addx 11 +addx 17 +addx -23 +addx 2 +noop +addx 3 +addx 2 +addx 3 +addx 4 +addx -22 +noop +addx 27 +addx -32 +addx 14 +addx 21 +addx 2 +noop +addx -37 +noop +addx 31 +addx -26 +addx 5 +addx 2 +addx 3 +addx -2 +addx 2 +addx 5 +addx 2 +addx 3 +noop +addx 2 +addx 9 +addx -8 +addx 2 +addx 11 +addx -4 +addx 2 +addx -15 +addx -22 +addx 1 +addx 5 +noop +noop +noop +noop +noop +addx 4 +addx 19 +addx -15 +addx 1 +noop +noop +addx 6 +noop +noop +addx 5 +addx -1 +addx 5 +addx -14 +addx -13 +addx 30 +noop +addx 3 +noop +noop