diff options
-rw-r--r-- | 10.py | 42 | ||||
-rw-r--r-- | 8.py | 64 |
2 files changed, 106 insertions, 0 deletions
@@ -0,0 +1,42 @@ +from dataclasses import dataclass + +@dataclass +class Cycle: + pass + +@dataclass +class Change: + diff: int + +instrs = [] +with open('10.in') as f: + for line in f: + match line.rstrip().split(): + case ['addx', n]: + instrs.append(Cycle()) + instrs.append(Cycle()) + instrs.append(Change(int(n))) + case ['noop']: + instrs.append(Cycle()) + +cycle = 0 +sigstr = 0 +X = 1 +screen = [0 for _ in range(240)] +for instr in instrs: + match instr: + case Cycle(): + cycle += 1 + if (cycle - 20) % 40 == 0: + sigstr += cycle * X + xpos = (cycle - 1) % 40 + if xpos - 1 <= X <= xpos + 1: + screen[cycle - 1] = 1 + case Change(diff): + X += diff +print(sigstr) +for y in range(6): + line = [] + for x in range(40): + line.append('#' if screen[x + 40 * y] else ' ') + print(''.join(line)) @@ -0,0 +1,64 @@ +from functools import reduce + +with open('8.in') as f: + grid = [[int(c) for c in line.rstrip()] for line in f] + +visible = set() + +for y, row in enumerate(grid): + height = -1 + for x, t in enumerate(row): + if t > height: + visible.add((x, y)) + height = t + height = -1 + for x, t in reversed(list(enumerate(row))): + if t > height: + visible.add((x, y)) + height = t + +for x in range(len(grid[0])): + height = -1 + for y, row in enumerate(grid): + t = row[x] + if t > height: + visible.add((x, y)) + height = t + height = -1 + for y, row in reversed(list(enumerate(grid))): + t = row[x] + if t > height: + visible.add((x, y)) + height = t + +print(len(visible)) + +def calc_scenic(grid, x, y): + height = grid[y][x] + scores = [0 for _ in range(4)] + for nx in range(x+1, len(grid[y])): + # print('right', nx, y) + scores[0] += 1 + if grid[y][nx] >= height: + break + for nx in range(x-1, -1, -1): + # print('left', nx, y) + scores[1] += 1 + if grid[y][nx] >= height: + break + for ny in range(y+1, len(grid)): + # print('down', x, ny) + scores[2] += 1 + if grid[ny][x] >= height: + break + for ny in range(y-1, -1, -1): + # print('up', x, ny) + scores[3] += 1 + if grid[ny][x] >= height: + break + # print(scores) + return reduce(lambda x, y: x * y, scores, 1) + +maxscore = 0 +# print(calc_scenic(grid, 2, 3)) +print(max(calc_scenic(grid, x, y) for x in range(len(grid[0])) for y in range(len(grid)))) |