-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9.py
137 lines (113 loc) Β· 3.9 KB
/
day9.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from dataclasses import dataclass
from enum import Enum
from aocd import get_data
from parse import parse
class Move(Enum):
Left: str = 'L'
Right: str = 'R'
Up: str = 'U'
Down: str = 'D'
@dataclass
class Position:
X: int
Y: int
def move_head(start: Position, direction: Move) -> Position:
match direction:
case Move.Left:
start.X -= 1
case Move.Right:
start.X += 1
case Move.Up:
start.Y += 1
case Move.Down:
start.Y -= 1
return Position(start.X, start.Y)
def expand_moves(input_data: list[str]):
all_moves = []
for moves in input_data:
compact_move = parse('{} {:d}', moves).fixed
all_moves.extend([compact_move[0] for index in range(0, compact_move[1])])
return all_moves
def move_tail(head: Position, tail: Position) -> Position:
# print(head)
if tail.X == head.X and tail.Y == head.Y:
return Position(tail.X, tail.Y)
if tail.X + 1 == head.X or tail.X - 1 == head.X:
if tail.Y == head.Y or tail.Y + 1 == head.Y or tail.Y - 1 == head.Y:
return Position(tail.X, tail.Y)
if tail.Y + 1 == head.Y or tail.Y - 1 == head.Y:
if tail.X == head.X or tail.X + 1 == head.X or tail.X - 1 == head.X:
return Position(tail.X, tail.Y)
if tail.X == head.X:
if tail.Y < head.Y and head.Y - tail.Y == 2: # up
return Position(tail.X, tail.Y + 1)
if tail.Y > head.Y and tail.Y - head.Y == 2: # down
return Position(tail.X, tail.Y - 1)
if tail.Y == head.Y:
if tail.X < head.X and head.X - tail.X == 2: # right
return Position(tail.X + 1, tail.Y)
if tail.X > head.X and tail.X - head.X == 2: # left
return Position(tail.X - 1, tail.Y)
if (tail.X + 1 == head.X or tail.X - 1 == head.X) and (tail.Y + 1 == head.Y or tail.Y - 1 == head.Y):
return Position(tail.X, tail.Y)
if tail.X < head.X: # move east
if tail.Y < head.Y: # move north
return Position(tail.X + 1, tail.Y + 1)
if tail.Y > head.Y: # move south
return Position(tail.X + 1, tail.Y - 1)
if tail.X > head.X: # move west
if tail.Y < head.Y: # move north
return Position(tail.X - 1, tail.Y + 1)
if tail.Y > head.Y: # move south
return Position(tail.X - 1, tail.Y - 1)
def part1(input_moves: list[str]):
head = Position(0, 0)
tail = Position(0, 0)
head_visits = [head]
tail_visits = [f'{tail.X},{tail.Y}']
for move in expand_moves(input_moves):
head = move_head(head, Move(move))
head_visits.append(head)
tail = move_tail(head, tail)
tail_visits.append(f'{tail.X},{tail.Y}')
return len(set(tail_visits))
def part2(input_moves: list[str]):
head = Position(0, 0)
head_visits = [head]
tails = [Position(0, 0) for x in range(0, 9)]
end_tail_visits = [f'{tails[-1].X},{tails[-1].Y}']
for move in expand_moves(input_moves):
head = move_head(head, Move(move))
head_visits.append(head)
for tail_index, tail in enumerate(tails):
if tail_index == 0:
tails[tail_index] = move_tail(head, tail)
else:
tails[tail_index] = move_tail(tails[tail_index - 1], tail)
if tail_index == len(tails) - 1:
end_tail_visits.append(f'{tails[tail_index].X},{tails[tail_index].Y}')
return len(set(end_tail_visits))
if __name__ == '__main__':
# data = [
# 'R 4',
# 'U 4',
# 'L 3',
# 'D 1',
# 'R 4',
# 'D 1',
# 'L 5',
# 'R 2'
# ]
data = [
'R 5',
'U 8',
'L 8',
'D 3',
'R 17',
'D 10',
'L 25',
'U 20'
]
data = get_data(day=9, year=2022).splitlines()
print(f'Part 1: {part1(data)}')
print(f'Part 2: {part2(data)}')