2022 day 9

This commit is contained in:
2022-12-09 23:39:01 +00:00
parent 4de4e05050
commit c474e4b25e
4 changed files with 2102 additions and 2 deletions

40
2022/09/part1 Executable file
View File

@@ -0,0 +1,40 @@
#!/usr/bin/env python
import math
MOVES = {
'U': ( 0, -1),
'D': ( 0, +1),
'L': (-1, 0),
'R': (+1, 0),
}
TAIL_POSITIONS = set([])
HEAD = (0, 0)
TAIL = (0, 0)
def point_add(a, b):
return tuple(map(lambda i, j: i + j, a, b))
def move(delta):
global TAIL_POSITIONS, HEAD, TAIL
last_head = HEAD
HEAD = point_add(HEAD, delta)
if ed := math.dist(HEAD, TAIL) < 2.0:
_ # do nothing
elif ed == 2.0:
TAIL = point_add(TAIL, delta)
else:
TAIL = last_head
TAIL_POSITIONS.add(TAIL)
with open('input') as f:
for line in f:
direction, count = line.rstrip().split(" ")
for _ in range(int(count)):
move(MOVES[direction])
print(len(TAIL_POSITIONS))