Skip to content

Commit 17b9bbe

Browse files
committed
✅ Adding Day 8 OG solutions & one-liners & viz!
1 parent 87df297 commit 17b9bbe

File tree

4 files changed

+78
-9
lines changed

4 files changed

+78
-9
lines changed

2023/day-08.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'''
2+
2023 Advent of Code - Day 08 (https://adventofcode.com/2023/day/8)
3+
Solution by Sav Bell (https://github.com/savbell)
4+
Challenge: Solve every day in a single line of Python code.
5+
See full progress at https://github.com/savbell/advent-of-code-one-liners
6+
'''
7+
8+
import itertools as it
9+
import math
10+
import re
11+
12+
# Input files not included in repo per copyright & Eric Wastl's request.
13+
# https://www.reddit.com/r/adventofcode/wiki/faqs/copyright/inputs/
14+
# Replace with path to your personal input file if you would like to run the code.
15+
input_file = '2023/day-08.txt'
16+
17+
# To match the format of input files for the Basilisk.
18+
q = { 8: open(input_file).read().strip() }
19+
20+
######################### PART 1: MULTI-LINE SOLUTION #########################
21+
instructions = q[8].split('\n')[0]
22+
nodes = [re.findall(r'(\w+) = \((\w+), (\w+)\)', line) for line in q[8].split('\n')[2:]]
23+
first_node = nodes[0][0][0]
24+
nodes = {node[0][0]: node[0][1:] for node in nodes}
25+
26+
cur = first_node
27+
steps = 0
28+
while not cur.endswith('Z'):
29+
for d in instructions:
30+
steps += 1
31+
if d == 'R':
32+
cur = nodes[cur][1]
33+
elif d == 'L':
34+
cur = nodes[cur][0]
35+
36+
print('Day 08 Part 1:',steps)
37+
38+
39+
########################## PART 1: ONE-LINE SOLUTION ##########################
40+
print('Day 08 Part 1:',(i:=q[8].split('\n')[0]) and (g:=[re.findall(r'(\w+) = \((\w+), (\w+)\)',l) for l in q[8].split('\n')[2:]]) and (g:=(u:=g[0][0][0]) and {o[0][0]:o[0][1:] for o in g}) and not (k:=0) and [((k:=k+1) and (u:=g[u][1] if d=='R' else g[u][0])) for d in it.takewhile(lambda _:(not u.endswith('Z')),it.cycle(i))] and k)
41+
42+
43+
######################## PART 2: MULTI-LINE SOLUTION ##########################
44+
instructions = q[8].split('\n')[0]
45+
nodes = [re.findall(r'(\w+) = \((\w+), (\w+)\)', line) for line in q[8].split('\n')[2:]]
46+
first_nodes = [n[0][0] for n in nodes if n[0][0].endswith('A')]
47+
nodes = {n[0][0]: n[0][1:] for n in nodes}
48+
49+
all_steps = []
50+
for node in first_nodes:
51+
cur = node
52+
steps = 0
53+
while not cur.endswith('Z'):
54+
for d in instructions:
55+
steps += 1
56+
if d == 'R':
57+
cur = nodes[cur][1]
58+
elif d == 'L':
59+
cur = nodes[cur][0]
60+
all_steps.append(steps)
61+
62+
# 13524038372771
63+
print('Day 08 Part 2:',math.lcm(*all_steps))
64+
65+
########################## PART 2: ONE-LINE SOLUTION ##########################
66+
print('Day 08 Part 2:',(i:=q[8].split('\n')[0]) and (g:=[re.findall(r'(\w+) = \((\w+), (\w+)\)', l) for l in q[8].split('\n')[2:]]) and (f:=[n[0][0] for n in g if n[0][0].endswith('A')]) and (g:={n[0][0]: n[0][1:] for n in g}) and not (a:=[]) and [(u:=o) and not (k:=0) and [((k:=k+1) and (u:=g[u][1] if d == 'R' else g[u][0])) for d in it.takewhile(lambda _:(not u.endswith('Z')),it.cycle(i))] and (a.__iadd__([k])) for o in f] and math.lcm(*a))

2023/the-basilisk.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
05 | ✔ | ✔
1313
06 | ✔ | ✔
1414
07 | ✔ | ✔
15+
08 | ✔ | ✔
1516
See README and explanation of solutions at https://github.com/savbell/advent-of-code-one-liners
1617
'''
1718

@@ -26,7 +27,8 @@
2627
5 : '2023/day-05.txt',
2728
6 : '2023/day-06.txt',
2829
7 : '2023/day-07.txt',
30+
8 : '2023/day-08.txt',
2931
}
3032

3133
################################ THE BASILISK ################################
32-
print((re:=__import__('re')) and (ft:=__import__('functools')) and (it:=__import__('itertools')) and (q:={d:open(p).read() for d,p in f.items()}) and 'Day 01 Part 1:',sum([(d:=re.findall(r'\d',l)) and int(d[0]+d[-1]) for l in q[1].split('\n')]),'\nDay 01 Part 2:',sum([int(''.join([{'one':'1','two':'2','three':'3','four':'4','five':'5','six':'6','seven':'7','eight':'8','nine':'9'}[d] if d.isalpha() else d for d in [n[0],n[-1]]])) for l in q[1].split('\n') for n in [re.findall(r'(?=(\d|one|two|three|four|five|six|seven|eight|nine))',l)]]),'\nDay 02 Part 1:',sum([int(g[0]) for g in re.findall(r'(\d+):((?: *\d+\s+\w+,?;?)+)',q[2])])-sum([int(g[0]) for g in re.findall(r'Game (\d+):((?:\s*\d+\s+\w+,?;?)+)',q[2]) if any([int(d[0])>12 and d[1]=='red' or int(d[0])>13 and d[1]=='green' or int(d[0])>14 and d[1]=='blue' for d in re.findall(r'(\d+)\s+(\w+)',g[1])])]),'\nDay 02 Part 2:',sum([(m:=re.findall(r'(\d+)\s+(\w+)',g[1])) and max([int(d[0]) for d in m if d[1]=='red'])*max([int(d[0]) for d in m if d[1]=='green'])*max([int(d[0]) for d in m if d[1]=='blue']) for g in re.findall(r'(\d+):((?: *\d+\s+\w+,?;?)+)',q[2])]),'\nDay 03 Part 1:',sum([int(n[0]) for n in [[n.group(),[(x,n.start()+i) for i in range(len(n.group()))]] for x,l in enumerate(q[3].split('\n')) for n in re.finditer(r'\d+',l)] if any([abs(c[0]-s[1][0])<=1 and abs(c[1]-s[1][1])<=1 for c in n[1] for s in [[s.group(),(x,s.start())] for x,l in enumerate(q[3].split('\n')) for s in re.finditer(r'[^.\d]',l)]])]),'\nDay 03 Part 2:',(r:=[[s.group(),(x,s.start()),[]] for x,l in enumerate(q[3].split('\n')) for s in re.finditer(r'[*]',l)]) and [s[2].append(n) if n not in s[2] else 0 for n in [[int(n.group()),[(x,n.start()+i) for i in range(len(n.group()))]] for x,l in enumerate(q[3].split('\n')) for n in re.finditer(r'\d+',l)] for c in n[1] for s in r if abs(c[0]-s[1][0])<=1 and abs(c[1]-s[1][1])<=1] and sum([s[2][0][0]*s[2][1][0] for s in r if len(s[2])==2]),'\nDay 04 Part 1:',sum([2**(len(n)-1) if len(n)>0 else 0 for n in [n[0].intersection(n[1]) for n in [(set(map(int,n[0].split())),set(map(int,n[1].split()))) for n in re.findall(r': +((?:\d+\s+)+)\| +((?:\d+\s*)+)',q[4])]]]),'\nDay 04 Part 2:',((c:=[[1,len(n[0].intersection(n[1]))] for n in [(set(map(int,n[0].split())), set(map(int,n[1].split()))) for n in re.findall(r'\d+: +((?:\d+\s+)+)\| +((?:\d+\s*)+)',q[4])]]) and not (t:=0) and [(t:=t+n[0]) and [c.__setitem__(i+j,[c[i+j][0]+1,c[i+j][1]]) for _ in range(n[0]) for j in range(1,n[1]+1)] for i, n in enumerate(c)] and t),'\nDay 05 Part 1:',(v:=[[list(map(int,l.split())) for l in d.split('\n')] for d in [d for _,d in [re.split(r':\n',s) for s in re.split(r'\n\n',q[5].strip())[1:]]]]) and (c:=lambda i,m:(lambda c:c[0]+(i-c[1]) if c[1]+c[2]>i else i)(min([y for y in m if y[1]<=i],default=[0,0,0],key=lambda x:i-x[1]))) and min([c(s,v[6])for s in [c(s,v[5]) for s in [c(s,v[4]) for s in [c(s,v[3]) for s in [c(s,v[2]) for s in [c(s,v[1]) for s in [c(s,v[0]) for s in list(map(int,re.split(r'\n\n',q[5].strip())[0].split()[1:]))]]]]]]]),'\nDay 05 Part 2:',[(v:=re.split(r'\n\n',q[5].strip())) and (s:=list(map(int,v[0].split()[1:]))) and (m:=v[1:]) and (c:=[(a,a+b) for a,b in zip(s[::2],s[1::2])]) and [[(w:=[tuple(map(int,x.split())) for x in g.split('\n')[1:]]) and (u:=[-99]) and (o:=[0]) and (l:=-99) and [(l:=max(l,(p:=r[0] - r[1])+r[1]+r[2])) and [(not u.__setitem__(-1,r[1]) and not o.__setitem__(-1,p)) if u and u[-1]==r[1] else (u.__iadd__([r[1]])) and o.__iadd__([p])] and (u.__iadd__([r[1]+r[2]]) and o.__iadd__([0])) for r in sorted(w,key=lambda x: x[1])] and not (t:=[]) and [(j:=[i[0]]) and not (h:=None) and [(((h:=d-1) if h is None else 0) and (j.__iadd__([p]) if p < i[1] and p != i[1] else 0) if not p <= j[-1] else 0) for d,p in enumerate(u)] and (j.__iadd__([i[1]]) and (h:=h or len(o))) and [(d:=o[min(h or float('inf'),len(o)-1)]) and (h:=h+1) and t.__iadd__([(a+d,b+d)]) for a,b in zip(j,j[1:])] for i in c]] and (c:=t) for g in m]] and min(x[0] for x in c),'\nDay 06 Part 1:',(so:=lambda b,c:[(b+((b**2)-(4*c))**0.5)/2,(b-((b**2)-(4*c))**0.5)/2]) and (h:=lambda i:int(i[1])-int(i[0]) if i[0]<i[1] else int(i[0])-int(i[1]) if i[0]%1!=0 else int(i[0])-int(i[1])-1) and (u:=[int(n) for n in re.findall(r'\d+',q[6])]) and ft.reduce(lambda a,b:a*b,[h(so(r[0], r[1])) for r in [[u[i],u[i+int(len(u)/2)]] for i in range(int(len(u)/2))]]),'\nDay 06 Part 2:',(so:=lambda b,c:[(b+((b**2)-(4*c))**0.5)/2,(b-((b**2)-(4*c))**0.5)/2]) and (h:=lambda i:int(i[1])-int(i[0]) if i[0]<i[1] else int(i[0])-int(i[1]) if i[0]%1!=0 else int(i[0])-int(i[1])-1) and (u:=[n for n in re.findall(r'\d+',q[6])]) and (r:=[int(''.join([u[i] for i in range(int(len(u)/2))])), int(''.join([u[i] for i in range(int(len(u)/2),len(u))]))]) and h(so(r[0], r[1])),'\nDay 07 Part 1:',(y:=[[['AKQJT98765432'.index(c)+1 for c in g.split()[0]],g.split()[1]] for g in q[7].split('\n')]) and [g.__iadd__([(s:=sorted(g[0])) and (7 if len(set(s))==1 else 6 if s[0]==s[1]==s[2]==s[3] or s[1]==s[2]==s[3]==s[4] else 5 if s[0]==s[1]==s[2] and s[3]==s[4] or s[0]==s[1] and s[2]==s[3]==s[4] else 4 if s[0]==s[1]==s[2] or s[1]==s[2]==s[3] or s[2]==s[3]==s[4] else 3 if s[0]==s[1] and s[2]==s[3] or s[0]==s[1] and s[3]==s[4] or s[1]==s[2] and s[3]==s[4] else 2 if len(set(s))==4 else 1)]) for g in y] and sum([int(g[1])*(i+1) for i, g in enumerate([g for g in [list(g) for _,g in it.groupby(sorted(y,key=lambda x:x[2]), lambda x:x[2])] for g in sorted(g,key=lambda x:x[0],reverse=True)])]),'\nDay 07 Part 2:',(y:=[[['AKQT98765432J'.index(c)+1 for c in g.split()[0]],g.split()[1]] for g in q[7].split('\n')]) and (score:=lambda h:(7 if len(set(h)) == 1 or len(set(h)) == 2 and 13 in h else 0) or (s:=sorted([min([c for c in h if h.count(c) == max([h.count(c) for c in h if c != 13])]) if c == 13 else c for c in h])) and (6 if s[0]==s[1]==s[2]==s[3] or s[1]==s[2]==s[3]==s[4] else 5 if s[0]==s[1]==s[2] and s[3]==s[4] or s[0]==s[1] and s[2]==s[3]==s[4] else 4 if s[0]==s[1]==s[2] or s[1]==s[2]==s[3] or s[2]==s[3]==s[4] else 3 if s[0]==s[1] and s[2]==s[3] or s[0]==s[1] and s[3]==s[4] or s[1]==s[2] and s[3]==s[4] else 2 if len(set(s))==4 else 1)) and [g.__iadd__([score(g[0])]) for g in y] and sum([int(g[1])*(i+1) for i, g in enumerate([g for g in [list(g) for _,g in it.groupby(sorted(y,key=lambda x:x[2]), lambda x:x[2])] for g in sorted(g,key=lambda x:x[0],reverse=True)])]))
34+
print((re:=__import__('re') or 1) and (ft:=__import__('functools') or 1) and (it:=__import__('itertools') or 1) and (math:=__import__('math') or 1) and (q:={d:open(p).read().strip() for d,p in f.items()}) and 'Day 01 Part 1:',sum([(d:=re.findall(r'\d',l)) and int(d[0]+d[-1]) for l in q[1].split('\n')]),'\nDay 01 Part 2:',sum([int(''.join([{'one':'1','two':'2','three':'3','four':'4','five':'5','six':'6','seven':'7','eight':'8','nine':'9'}[d] if d.isalpha() else d for d in [n[0],n[-1]]])) for l in q[1].split('\n') for n in [re.findall(r'(?=(\d|one|two|three|four|five|six|seven|eight|nine))',l)]]),'\nDay 02 Part 1:',sum([int(g[0]) for g in re.findall(r'(\d+):((?: *\d+\s+\w+,?;?)+)',q[2])])-sum([int(g[0]) for g in re.findall(r'Game (\d+):((?:\s*\d+\s+\w+,?;?)+)',q[2]) if any([int(d[0])>12 and d[1]=='red' or int(d[0])>13 and d[1]=='green' or int(d[0])>14 and d[1]=='blue' for d in re.findall(r'(\d+)\s+(\w+)',g[1])])]),'\nDay 02 Part 2:',sum([(m:=re.findall(r'(\d+)\s+(\w+)',g[1])) and max([int(d[0]) for d in m if d[1]=='red'])*max([int(d[0]) for d in m if d[1]=='green'])*max([int(d[0]) for d in m if d[1]=='blue']) for g in re.findall(r'(\d+):((?: *\d+\s+\w+,?;?)+)',q[2])]),'\nDay 03 Part 1:',sum([int(n[0]) for n in [[n.group(),[(x,n.start()+i) for i in range(len(n.group()))]] for x,l in enumerate(q[3].split('\n')) for n in re.finditer(r'\d+',l)] if any([abs(c[0]-s[1][0])<=1 and abs(c[1]-s[1][1])<=1 for c in n[1] for s in [[s.group(),(x,s.start())] for x,l in enumerate(q[3].split('\n')) for s in re.finditer(r'[^.\d]',l)]])]),'\nDay 03 Part 2:',(r:=[[s.group(),(x,s.start()),[]] for x,l in enumerate(q[3].split('\n')) for s in re.finditer(r'[*]',l)]) and [s[2].append(n) if n not in s[2] else 0 for n in [[int(n.group()),[(x,n.start()+i) for i in range(len(n.group()))]] for x,l in enumerate(q[3].split('\n')) for n in re.finditer(r'\d+',l)] for c in n[1] for s in r if abs(c[0]-s[1][0])<=1 and abs(c[1]-s[1][1])<=1] and sum([s[2][0][0]*s[2][1][0] for s in r if len(s[2])==2]),'\nDay 04 Part 1:',sum([2**(len(n)-1) if len(n)>0 else 0 for n in [n[0].intersection(n[1]) for n in [(set(map(int,n[0].split())),set(map(int,n[1].split()))) for n in re.findall(r': +((?:\d+\s+)+)\| +((?:\d+\s*)+)',q[4])]]]),'\nDay 04 Part 2:',((c:=[[1,len(n[0].intersection(n[1]))] for n in [(set(map(int,n[0].split())), set(map(int,n[1].split()))) for n in re.findall(r'\d+: +((?:\d+\s+)+)\| +((?:\d+\s*)+)',q[4])]]) and not (t:=0) and [(t:=t+n[0]) and [c.__setitem__(i+j,[c[i+j][0]+1,c[i+j][1]]) for _ in range(n[0]) for j in range(1,n[1]+1)] for i, n in enumerate(c)] and t),'\nDay 05 Part 1:',(v:=[[list(map(int,l.split())) for l in d.split('\n')] for d in [d for _,d in [re.split(r':\n',s) for s in re.split(r'\n\n',q[5])[1:]]]]) and (c:=lambda i,m:(lambda c:c[0]+(i-c[1]) if c[1]+c[2]>i else i)(min([y for y in m if y[1]<=i],default=[0,0,0],key=lambda x:i-x[1]))) and min([c(s,v[6])for s in [c(s,v[5]) for s in [c(s,v[4]) for s in [c(s,v[3]) for s in [c(s,v[2]) for s in [c(s,v[1]) for s in [c(s,v[0]) for s in list(map(int,re.split(r'\n\n',q[5])[0].split()[1:]))]]]]]]]),'\nDay 05 Part 2:',[(v:=re.split(r'\n\n',q[5])) and (s:=list(map(int,v[0].split()[1:]))) and (m:=v[1:]) and (c:=[(a,a+b) for a,b in zip(s[::2],s[1::2])]) and [[(w:=[tuple(map(int,x.split())) for x in g.split('\n')[1:]]) and (u:=[-99]) and (o:=[0]) and (l:=-99) and [(l:=max(l,(p:=r[0] - r[1])+r[1]+r[2])) and [(not u.__setitem__(-1,r[1]) and not o.__setitem__(-1,p)) if u and u[-1]==r[1] else (u.__iadd__([r[1]])) and o.__iadd__([p])] and (u.__iadd__([r[1]+r[2]]) and o.__iadd__([0])) for r in sorted(w,key=lambda x: x[1])] and not (t:=[]) and [(j:=[i[0]]) and not (h:=None) and [(((h:=d-1) if h is None else 0) and (j.__iadd__([p]) if p < i[1] and p != i[1] else 0) if not p <= j[-1] else 0) for d,p in enumerate(u)] and (j.__iadd__([i[1]]) and (h:=h or len(o))) and [(d:=o[min(h or float('inf'),len(o)-1)]) and (h:=h+1) and t.__iadd__([(a+d,b+d)]) for a,b in zip(j,j[1:])] for i in c]] and (c:=t) for g in m]] and min(x[0] for x in c),'\nDay 06 Part 1:',(so:=lambda b,c:[(b+((b**2)-(4*c))**0.5)/2,(b-((b**2)-(4*c))**0.5)/2]) and (h:=lambda i:int(i[1])-int(i[0]) if i[0]<i[1] else int(i[0])-int(i[1]) if i[0]%1!=0 else int(i[0])-int(i[1])-1) and (u:=[int(n) for n in re.findall(r'\d+',q[6])]) and ft.reduce(lambda a,b:a*b,[h(so(r[0], r[1])) for r in [[u[i],u[i+int(len(u)/2)]] for i in range(int(len(u)/2))]]),'\nDay 06 Part 2:',(so:=lambda b,c:[(b+((b**2)-(4*c))**0.5)/2,(b-((b**2)-(4*c))**0.5)/2]) and (h:=lambda i:int(i[1])-int(i[0]) if i[0]<i[1] else int(i[0])-int(i[1]) if i[0]%1!=0 else int(i[0])-int(i[1])-1) and (u:=[n for n in re.findall(r'\d+',q[6])]) and (r:=[int(''.join([u[i] for i in range(int(len(u)/2))])), int(''.join([u[i] for i in range(int(len(u)/2),len(u))]))]) and h(so(r[0], r[1])),'\nDay 07 Part 1:',(y:=[[['AKQJT98765432'.index(c)+1 for c in g.split()[0]],g.split()[1]] for g in q[7].split('\n')]) and [g.__iadd__([(s:=sorted(g[0])) and (7 if len(set(s))==1 else 6 if s[0]==s[1]==s[2]==s[3] or s[1]==s[2]==s[3]==s[4] else 5 if s[0]==s[1]==s[2] and s[3]==s[4] or s[0]==s[1] and s[2]==s[3]==s[4] else 4 if s[0]==s[1]==s[2] or s[1]==s[2]==s[3] or s[2]==s[3]==s[4] else 3 if s[0]==s[1] and s[2]==s[3] or s[0]==s[1] and s[3]==s[4] or s[1]==s[2] and s[3]==s[4] else 2 if len(set(s))==4 else 1)]) for g in y] and sum([int(g[1])*(i+1) for i, g in enumerate([g for g in [list(g) for _,g in it.groupby(sorted(y,key=lambda x:x[2]), lambda x:x[2])] for g in sorted(g,key=lambda x:x[0],reverse=True)])]),'\nDay 07 Part 2:',(y:=[[['AKQT98765432J'.index(c)+1 for c in g.split()[0]],g.split()[1]] for g in q[7].split('\n')]) and (score:=lambda h:(7 if len(set(h)) == 1 or len(set(h)) == 2 and 13 in h else 0) or (s:=sorted([min([c for c in h if h.count(c) == max([h.count(c) for c in h if c != 13])]) if c == 13 else c for c in h])) and (6 if s[0]==s[1]==s[2]==s[3] or s[1]==s[2]==s[3]==s[4] else 5 if s[0]==s[1]==s[2] and s[3]==s[4] or s[0]==s[1] and s[2]==s[3]==s[4] else 4 if s[0]==s[1]==s[2] or s[1]==s[2]==s[3] or s[2]==s[3]==s[4] else 3 if s[0]==s[1] and s[2]==s[3] or s[0]==s[1] and s[3]==s[4] or s[1]==s[2] and s[3]==s[4] else 2 if len(set(s))==4 else 1)) and [g.__iadd__([score(g[0])]) for g in y] and sum([int(g[1])*(i+1) for i, g in enumerate([g for g in [list(g) for _,g in it.groupby(sorted(y,key=lambda x:x[2]),lambda x:x[2])] for g in sorted(g,key=lambda x:x[0],reverse=True)])]),'\nDay 08 Part 1:',(i:=q[8].split('\n')[0]) and (g:=[re.findall(r'(\w+) = \((\w+), (\w+)\)',l) for l in q[8].split('\n')[2:]]) and (g:=(u:=g[0][0][0]) and {o[0][0]:o[0][1:] for o in g}) and not (k:=0) and [((k:=k+1) and (u:=g[u][1] if d=='R' else g[u][0])) for d in it.takewhile(lambda _:(not u.endswith('Z')),it.cycle(i))] and k,'\nDay 08 Part 2:',(i:=q[8].split('\n')[0]) and (g:=[re.findall(r'(\w+) = \((\w+), (\w+)\)', l) for l in q[8].split('\n')[2:]]) and (f:=[n[0][0] for n in g if n[0][0].endswith('A')]) and (g:={n[0][0]: n[0][1:] for n in g}) and not (a:=[]) and [(u:=o) and not (k:=0) and [((k:=k+1) and (u:=g[u][1] if d == 'R' else g[u][0])) for d in it.takewhile(lambda _:(not u.endswith('Z')),it.cycle(i))] and (a.__iadd__([k])) for o in f] and math.lcm(*a))

0 commit comments

Comments
 (0)