123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- from util import get_input, transpose
- input = get_input("13.input")
- folds = []
- numbers = []
- for line in input:
- if line.startswith("fold along"):
- [d, i] = line.split()[2].split("=")
- folds.append((d, int(i)))
- elif line != "":
- [x, y] = line.split(",")
- numbers.append((int(x), int(y)))
- grid = [['.' for _ in range((max(x for (x, y) in numbers) + 1))] for _ in range(max(y for (x, y) in numbers) + 1)]
- for (x, y) in numbers:
- grid[y][x] = '#'
- def print_grid(grid):
- print("\n".join(["".join(row) for row in grid]))
- def fold_y(grid, i):
- y = i - 1
- for dy, row in enumerate(grid[i + 1:]):
- grid[y - dy] = ['#' if row[x] == '#' or grid[y - dy][x] == '#' else '.' for x in range(len(grid[0]))]
- grid = grid[:i]
- return grid
- def fold(grid, d, i):
- if d == 'y':
- grid = fold_y(grid, i)
- else:
- grid = transpose(grid)
- grid = fold_y(grid, i)
- grid = transpose(grid)
- return grid
- for (d, i) in folds:
- grid = fold(grid, d, i)
- break
- print("Task 1:", sum(sum(1 if c == '#' else 0 for c in row) for row in grid))
- for (d, i) in folds[1:]:
- grid = fold(grid, d, i)
- print("Task 2:")
- print_grid(grid)
|