-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9a.py
77 lines (63 loc) · 1.67 KB
/
day9a.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
locs = set()
moves = []
curposx = 0
curposy = 0
headx = 0
heady = 0
tailx = 0
taily = 0
def printpos():
print(f"Head: ({headx},{heady}), Tail: ({tailx},{taily})")
with open('day9a_input.txt', 'r') as fp:
for line in fp:
dir, steps = line.strip().split(" ")
moves.append([dir, int(steps)])
print(moves)
locs.add((curposx,curposy))
printpos()
for dir,steps in moves:
#print(dir,steps)
for m in range(steps):
#first move head
if (dir=='R'):
headx += 1
elif (dir=='L'):
headx -= 1
elif (dir=='U'):
heady += 1
elif (dir=='D'):
heady -= 1
else:
print("BAD DIRECTION")
#printpos()
# Now move tail
xdiff = headx-tailx
ydiff = heady-taily
#print(f"xdiff:{xdiff} ydiff:{ydiff}")
if (((abs(xdiff)>=2) and (ydiff != 0)) or ((abs(ydiff)>=2) and (xdiff != 0))): # Two direction adjustment
if (xdiff>=2):
tailx+=1
taily+=ydiff
elif (xdiff<=-2):
tailx-=1
taily+=ydiff
elif (ydiff>=2):
taily+=1
tailx+=xdiff
elif (ydiff<=-2):
taily-=1
tailx+=xdiff
else: # One direction adjustment
if (xdiff>=2):
tailx+=1
elif (xdiff<=-2):
tailx-=1
elif (ydiff>=2):
taily+=1
elif (ydiff<=-2):
taily-=1
#printpos()
# Now log current position
locs.add((tailx, taily))
print(locs)
print(f"Num locations: {len(locs)}")