aboutsummaryrefslogtreecommitdiff
path: root/2022/09/puzzles.py
blob: 565a39c9b543070e716023f719ebf6b2428f84c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/python3

# START PART 1
SEGMENTS = 2
# END PART 1 START PART 2
SEGMENTS = 10
# END PART 2

position = tuple[int, int]


class Segment:
	def __init__(self):
		self.x = 0
		self.y = 0

	def track(self, pos: position) -> None:
		x, y = pos
		dx = x - self.x
		dy = y - self.y
		nx = 1 if dx > 0 else -1
		ny = 1 if dy > 0 else -1

		match (abs(dx), abs(dy)):
			case (2, 0):
				self.x += nx
			case (0, 2):
				self.y += ny
			case (2, 1) | (2, 2) | (1, 2):
				self.x += nx
				self.y += ny

	@property
	def pos(self) -> position:
		return self.x, self.y


def main() -> None:
	with open("input", "r") as f:
		data = [(xs[0], int(xs[1])) for xs in [l.split() for l in f.readlines()]]

	snake = [Segment() for _ in range(SEGMENTS)]
	head = snake[0]
	tail = snake[-1]
	locs: set[position] = set()

	for d, c in data:
		for _ in range(c):
			match d:
				case "U":
					head.y += 1
				case "D":
					head.y -= 1
				case "R":
					head.x += 1
				case "L":
					head.x -= 1

			for i, segment in enumerate(snake[1:]):
				segment.track(snake[i].pos)

			locs.add(tail.pos)

	print(len(locs))


if __name__ == "__main__":
	main()