-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.py
65 lines (55 loc) · 2.27 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
#!/usr/bin/env python
import numpy as np
f = open("ressources/day9.txt", "r")
lines = f.readlines()
HeadPosition = [0, 0]
TailPosition = [0, 0]
listOfTailPositions = []
for line in lines:
instructions = line.replace("\n", "").split(" ")
direction = instructions[0]
numberOfSteps = int(instructions[1])
for i in range(numberOfSteps):
if direction == "D":
HeadPosition[1] += -1
if direction == "U":
HeadPosition[1] += +1
if direction == "L":
HeadPosition[0] += -1
if direction == "R":
HeadPosition[0] += +1
positionDifference = [HeadPosition[0]-TailPosition[0], HeadPosition[1]-TailPosition[1]]
if abs(positionDifference[0]) > 1 or abs(positionDifference[1]) > 1:
TailPosition[0] += np.sign(positionDifference[0])
TailPosition[1] += np.sign(positionDifference[1])
listOfTailPositions.append([TailPosition[0], TailPosition[1]])
listOfTailPositions = np.unique(listOfTailPositions, axis=0)
print("Number of visited positions is: {}".format(len(listOfTailPositions)))
ropeLength = 10
ropeList = []
listOfTailPositions = []
for i in range(ropeLength):
ropeList.append([0,0])
for line in lines:
instructions = line.replace("\n", "").split(" ")
direction = instructions[0]
numberOfSteps = int(instructions[1])
for i in range(numberOfSteps):
if direction == "D":
ropeList[0][1] += -1
if direction == "U":
ropeList[0][1] += +1
if direction == "L":
ropeList[0][0] += -1
if direction == "R":
ropeList[0][0] += +1
for i in range(1,ropeLength):
HeadPosition = ropeList[i-1]
TailPosition = ropeList[i]
positionDifference = [HeadPosition[0]-TailPosition[0], HeadPosition[1]-TailPosition[1]]
if abs(positionDifference[0]) > 1 or abs(positionDifference[1]) > 1:
ropeList[i][0] += np.sign(positionDifference[0])
ropeList[i][1] += np.sign(positionDifference[1])
listOfTailPositions.append([TailPosition[0], TailPosition[1]])
listOfTailPositions = np.unique(listOfTailPositions, axis=0)
print("Number of visited positions is: {}".format(len(listOfTailPositions)))