Compare commits

...

4 Commits

Author SHA1 Message Date
magical a3e4290ccd day 8 python slight cleanup 2022-12-08 22:08:51 -08:00
magical 117d3d58d5 day 9 python simplify 2022-12-08 21:44:02 -08:00
magical 8cb5086749 day 9 python part 2 2022-12-08 21:24:52 -08:00
magical 036a563d8f day 9 python part 1 2022-12-08 21:13:53 -08:00
4 changed files with 2056 additions and 12 deletions

View File

@ -6,36 +6,30 @@ scores = []
for ii in range(len(data)):
for jj in range(len(data[ii])):
top = data[ii][jj]
h = -1
a = 0
i = ii
for j in range(jj+1, len(data[ii])):
a += 1
h = data[ii][j]
if h >= top:
if data[i][j] >= top:
break
b = 0
h = -1
for j in reversed(range(0, jj)):
b += 1
h = data[ii][j]
if h >= top:
if data[i][j] >= top:
break
j = jj
c = 0
h = -1
for i in range(ii+1, len(data)):
c += 1
h = data[i][jj]
if h >= top:
if data[i][j] >= top:
break
d = 0
h = -1
for i in reversed(range(0, ii)):
d += 1
h = data[i][jj]
if h >= top:
if data[i][j] >= top:
break
print(ii,jj,a,b,c,d)

42
day09/day09.py 100644
View File

@ -0,0 +1,42 @@
def go(p, dir):
x,y = p
if dir == "U": y += 1
elif dir == "D": y -= 1
elif dir == "L": x -= 1
elif dir == "R": x += 1
return x,y
def follow(h, t):
if abs(h[0] - t[0]) > 1 or abs(h[1] - t[1]) > 1:
x,y = t
x += sgn(h[0] - t[0])
y += sgn(h[1] - t[1])
t = x,y
return t
def sgn(x):
return (x > 0) - (x < 0)
def solve(n):
R = [(0,0)]*n
visited = set([R[-1]])
for line in open("input"):
dir, count = line.split()
for _ in range(int(count)):
R[0] = go(R[0], dir)
for i in range(1, len(R)):
t = follow(R[i-1], R[i])
if t == R[i]:
# optimization: if a knot doesn't move,
# none of the knots after it will either
break
R[i] = t
visited.add(R[-1])
#print(R)
return len(visited)
A1 = solve(2)
print(A1)
A2 = solve(10)
print(A2)

2000
day09/input 100644

File diff suppressed because it is too large Load Diff

8
day09/sample 100644
View File

@ -0,0 +1,8 @@
R 4
U 4
L 3
D 1
R 4
D 1
L 5
R 2