|
| 1 | +from collections import defaultdict |
| 2 | +from functools import reduce |
| 3 | +import os |
| 4 | +import re |
| 5 | + |
| 6 | + |
| 7 | +def column(matrix, i): |
| 8 | + return [row[i] for row in matrix] |
| 9 | + |
| 10 | + |
| 11 | +def part1(lines: list[str]): |
| 12 | + grid = defaultdict() |
| 13 | + pos_head = (0, 0) |
| 14 | + pos_tail = (0, 0) |
| 15 | + |
| 16 | + grid[pos_head] = True |
| 17 | + for line in lines: |
| 18 | + parts = line.split() |
| 19 | + instruction = (parts[0], int(parts[1])) |
| 20 | + |
| 21 | + for i in range(instruction[1]): |
| 22 | + if instruction[0] == "R": |
| 23 | + pos_head = (pos_head[0] + 1, pos_head[1]) |
| 24 | + delta = [x - y for x, y in zip(pos_head, pos_tail)] |
| 25 | + if abs(delta[0]) > 1 or abs(delta[1]) > 1: |
| 26 | + pos_tail = (pos_tail[0] + 1, pos_tail[1] + delta[1]) |
| 27 | + if instruction[0] == "L": |
| 28 | + pos_head = (pos_head[0] - 1, pos_head[1]) |
| 29 | + delta = [x - y for x, y in zip(pos_head, pos_tail)] |
| 30 | + if abs(delta[0]) > 1 or abs(delta[1]) > 1: |
| 31 | + pos_tail = (pos_tail[0] - 1, pos_tail[1] + delta[1]) |
| 32 | + if instruction[0] == "U": |
| 33 | + pos_head = (pos_head[0], pos_head[1] + 1) |
| 34 | + delta = [x - y for x, y in zip(pos_head, pos_tail)] |
| 35 | + if abs(delta[0]) > 1 or abs(delta[1]) > 1: |
| 36 | + pos_tail = (pos_tail[0] + delta[0], pos_tail[1] + 1) |
| 37 | + if instruction[0] == "D": |
| 38 | + pos_head = (pos_head[0], pos_head[1] - 1) |
| 39 | + delta = [x - y for x, y in zip(pos_head, pos_tail)] |
| 40 | + if abs(delta[0]) > 1 or abs(delta[1]) > 1: |
| 41 | + pos_tail = (pos_tail[0] + delta[0], pos_tail[1] - 1) |
| 42 | + grid[pos_tail] = True |
| 43 | + pass |
| 44 | + |
| 45 | + return len(grid) |
| 46 | + |
| 47 | + |
| 48 | +def part2(lines): |
| 49 | + pass |
| 50 | + |
| 51 | + |
| 52 | +dirname = os.path.dirname(__file__) |
| 53 | +filename = os.path.join(dirname, '../input.txt') |
| 54 | +with open(filename) as f: |
| 55 | + lines = f.readlines() |
| 56 | + |
| 57 | +print(part1(lines)) |
| 58 | +print(part2(lines)) |
0 commit comments