Compare commits

...

7 Commits

Author SHA1 Message Date
magical 5a27954c11 day 25 simplify 2022-12-24 22:15:02 -08:00
magical 666aa2bcad day 25 \o/ 2022-12-24 21:16:39 -08:00
magical 460d51db5c day 24 python minor optimization
i can't believe this is faster but it is (slightly)
2022-12-24 11:22:49 -08:00
magical 0d46922957 day 24 python optimization 2022-12-24 11:11:32 -08:00
magical 66f2250b21 day 24 timing 2022-12-24 10:25:43 -08:00
magical 0d50dd1874 day 24 python part 2 2022-12-24 10:07:38 -08:00
magical 5acd7b7812 day 24 python part 1 2022-12-24 09:56:22 -08:00
6 changed files with 337 additions and 0 deletions

22
day24/input 100644
View File

@ -0,0 +1,22 @@
#.######################################################################################################################################################
#.v><><<>^^^<><<>.^v<>v^>v^v^^<..^v>^>^vv.>v^<<>^.v<vvv<.>><v>><^>><>^<><v<>>>><v>v<^<>^.v..v^vv<v<<vv.v>>v<>v.<..^<vv<^v^v.v>>v>><^v>^<.vv^^<<><<>v>><#
#.vv<vv<>.v<<<<<>>vv<>^v^^<^<>><.<^>^<^>^<v<<<<v>v>><.><.^><>vv<^^><<<<>^<.<v^.vv^vv>v>^.<<<><v<.v.<><<v^^v>^^<^>>^.<v^v.v><<v^vv<<v>^v>><^^<^<v>^>><v<#
#<v^>v^vv<><v><<^<>^<^vv>v>>^^><<<vv><v.v.^><<^><>v>^<^v<<^>v<vv><v<<^.vv^>>>.^v>v.><v>v<^<^^<><<>><<^^^>v<^>>^v>>vv.<>^<><<^v>><^.>v^v><<^^^^<v<>^v<v.#
#<<^<vv^<^><>^><>^vv.v^v>><^^<>^<>>^><^.^^<.v...>^^.^<<v<vvv>>>v^..^v.v<^><>.>v^.vv<^^vv>vv^.^.vv<v>>^>v.^^^<v<>><vv^>>^^.v>.v^^<^>vvv^<<v^<v<^><^>v<>>#
#<.<^>>^<v><^>^^.>>v^<v^vvv>>vv.v^<>^^>v<<vv^<>^v.<<<^>>>.^>vvv.^^^<^><<^><v>><^.^>vv^^vv.><<v<^<v^^^.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^^v^>^<>v^<^<>>..>><>v<.^<.>v.>^v.>>^^<^v>>v^.v>^><>>vv.<<<>v>^.>v>..<<<v.<v^<>^>^<>v<^<v<><^>^v><.<<^^^<^.<<<^.^v^^.>^.<<vv>^v><<>^<^v^^>^v><<^>#
######################################################################################################################################################.#

6
day24/sample 100644
View File

@ -0,0 +1,6 @@
#.######
#>>.<^<#
#.<..<<#
#>v.><>#
#<^v^^>#
######.#

135
day24/sol.py 100644
View File

@ -0,0 +1,135 @@
data = []
for line in open("input"):
data.append(line.strip())
import sys, os; sys.path.append(os.path.join(os.path.dirname(__file__), "../lib"))
import astar
#print(*data, sep="\n")
def show(t):
H = len(data)-2
for y in range(len(data)):
s = []
W = len(data[y])-2
for x in range(len(data[y])):
c = data[y][x]
if c == '#':
s.append(c)
else:
bliz = []
u = 1 + ((x - t)-1)%W
if data[y][u] == '>':
bliz.append('>')
u = 1 + ((x + t)-1)%W
if data[y][u] == '<':
bliz.append('<')
v = 1 + ((y - t)-1)%H
if data[v][x] == 'v':
bliz.append('v')
v = 1 + ((y + t)-1)%H
if data[v][x] == '^':
bliz.append('^')
if len(bliz) == 0:
s.append('.')
elif len(bliz) == 1:
s.append(bliz[0])
elif len(bliz) < 10:
s.append(str(len(bliz)))
else:
s.append('*')
print(''.join(s))
def blocked(x,y,t):
if not 0 <= y < len(data):
return True
if not 0 <= x < len(data[y]):
return True
if data[y][x] == '#':
return True
H = len(data)-2
W = len(data[y])-2
u = 1 + ((x - t)-1)%W
if data[y][u] == '>':
return True
u = 1 + ((x + t)-1)%W
if data[y][u] == '<':
return True
v = 1 + ((y - t)-1)%H
if data[v][x] == 'v':
return True
v = 1 + ((y + t)-1)%H
if data[v][x] == '^':
return True
return False
start = (data[0].index('.'), 0)
end = (data[-1].index('.'), len(data)-1)
leg_distance = abs(start[0] - end[0]) + abs(start[1] - end[1])
def is_goal(node, goal=end):
x, y, t = node
return (x,y) == goal
def heuristic(node, goal=end):
x, y, t = node
return abs(goal[0] - x) + abs(goal[1] - y)
def neighbors(node):
x, y, t = node
n = []
def check(dx,dy):
if not blocked(x+dx,y+dy,t+1):
n.append((1, (x+dx, y+dy, t+1)))
check(+1,0)
check(0,+1)
check(-1,0)
check(0,-1)
check(0,0)
return n
show(0)
show(1)
#show((len(data)-2)*(len(data[0])-2))
from functools import partial
import time
t0 = time.time()
# part 1
d1 = astar.search(start+(0,), is_goal, neighbors, heuristic)[0]
t1 = time.time()
# part 2
# we can always take the best path for each leg,
# rather than trying to compute it over the whole trip.
# suppose there is a better overall path B = d1' + d2 + d3
# with d1' > d1. we can "sync up" with this path by simply
# waiting at the end square for d1'-d1 steps at the beginning
# of the next leg. (A* will check this possibility for us.)
# (the start and end squares are never blocked by blizzards.)
# therefore even if we can complete later legs faster by
# starting later, there is no downside to taking the shortest
# path for all the previous legs.
is_start = partial(is_goal, goal=start)
heuristic2 = partial(heuristic, goal=start)
d2 = astar.search(end+(d1,), is_start, neighbors, heuristic2)[0]
d3 = astar.search(start+(d1+d2,), is_goal, neighbors, heuristic)[0]
t2 = time.time()
print("part 1", d1, t1 - t0)
print("part 2", d1+d2+d3, t2 - t0, "(%+f)"%(t2-t1))

125
day25/input 100644
View File

@ -0,0 +1,125 @@
2-=0102020-02
1-02202=-1=-2==
1==00-101-=0-
211012=12222
112-20
11==0020-=20-=0=12=
122-22110
1==00-1201112==--=
1-10==11=
10=0=110-2
1111=000=2001=211=
22-21===0=-
2=022
11=-0100===
10=10--==-0
12=01-1
12-01--20121-2-=0-1
22-222--022-0-=01-
12-1
2-=2=02-1=
2210
1=2=0211
10=01=001-01
2
201=-1
11---10
1=-0=
1=-021
1-2=02012111=01=-100
1101=20=
20-1-
2-1020---
2=121=0=10=
1022-22211112=-1
2-1-
210--==00
12=1122-10
1=2222=2-=21=0
1=021=
1=10-1201
1=1
1-=-==0201-=1=0
22=2=211-0-1=2=1
1=21-=002111
102112=10021
1===-111-=2001122
10--
1-
1-2-2
11-1-1-02-
1=20--=1=0101-1001
12012-
1220-1--00
1=2=1-
12-0=2022=0100-1
12=2-=-=221==2=
2-2-
1=-1-01-0=
12---2=
1=1=1102-==010
10-=--200=
1=2212=-
12-121=-
1===0-=20-=
1=2020-01---112=1-
101=00111=20=
1=2=01=1-2==0-
2=-100---00
1==1=01--===0-10-
1==-=0-20
1=2011121=10--2
2-=0-01-=--12102=
200
2==22=0002-210
102
1-22221
20==00
1-22--2-0=10=11-1
1=12=2
1=-12-=
10-020-100=0=
1-11111
1=02-002220
20==0120210--1
100-121=2-2-=00=
21
11=-=-
2-=1100=10102-
112--0-2=1--=2=
1-1==100
1--02-0=1-
1102-
1-2-21-
22=1=-000=210-12
1=112-12-
1====120==0
20=2=22=22=2-=2=
1=21201=
2202202-20
1=-2=-==--200101
12010
1--22=2--02
102==-
2-==1-10
12-==12-2
22=-11110=2-211
1-0
1=02=20-1=--
1-20
1102---10
11=2-==2=1-
2=2-
2=10=2==221=01
1--==-1==
1011-111
1-2-=---012-21
1=-==0
1=0011=11112102=
10-0-=202
2-----12=
1=-2221020=01-10
12-2201==
1-102
22-100=0-===
100=0-0

13
day25/sample 100644
View File

@ -0,0 +1,13 @@
1=-0-2
12111
2=0=
21
2=01
111
20012
112
1=-1=
1-12
12
1=
122

36
day25/sol.py 100644
View File

@ -0,0 +1,36 @@
def eval(num):
digits = ["=-012".index(c)-2 for c in num]
n = 0
for x in digits:
n = n*5 + x
return n
def tosnafu(n):
orig = n
digits = []
carry = 0
while n != 0:
n, d = divmod(n, 5)
carry, d = divmod(d+carry, 5)
if d > 2:
carry += 1
d -= 5
digits.append(d)
if carry:
digits.append(carry)
digits.reverse()
snafu = "".join("=-012"[d+2] for d in digits)
#print(n, digits, snafu)
assert eval(snafu) == orig
return snafu
t = 0
for line in open("input"):
n = eval(line.strip())
print(n)
t += n
print(t)
print(tosnafu(t))