Skip to content

Commit 1a93146

Browse files
committed
✅ Adding Day 15 OG & One-line Solutions!
1 parent 09d1c59 commit 1a93146

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

Diff for: 2023/day-15.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'''
2+
2023 Advent of Code - Day 15 (https://adventofcode.com/2023/day/15)
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+
# Input files not included in repo per copyright & Eric Wastl's request.
9+
# https://www.reddit.com/r/adventofcode/wiki/faqs/copyright/inputs/
10+
# Replace with path to your personal input file if you would like to run the code.
11+
input_file = '2023/day-15.txt'
12+
13+
# To match the format of input files for the Basilisk.
14+
q = { 15: open(input_file).read().strip() }
15+
16+
17+
######################### PART 1: MULTI-LINE SOLUTION #########################
18+
steps = q[15].split(',')
19+
values = []
20+
for s in steps:
21+
value = 0
22+
for c in s:
23+
value += ord(c)
24+
value *= 17
25+
value %= 256
26+
values.append(value)
27+
28+
print('Day 15 Part 1:',sum(values))
29+
30+
########################## PART 1: ONE-LINE SOLUTION ##########################
31+
print('Day 15 Part 1:',sum([(v:=0) or [v:=(v+ord(c))*17%256 for c in s] and v for s in q[15].split(',')]))
32+
33+
34+
######################## PART 2: MULTI-LINE SOLUTION ##########################
35+
steps = q[15].split(',')
36+
boxes = [[] for _ in range(256)]
37+
for s in steps:
38+
label = ''.join([c for c in s if c.isalpha()])
39+
operation = ''.join([c for c in s if not c.isalnum()])
40+
focal = ''.join([c for c in s if c.isdigit()])
41+
lens = (label, focal)
42+
box = 0
43+
for c in label:
44+
box += ord(c)
45+
box *= 17
46+
box %= 256
47+
if operation == '=':
48+
if label not in [x[0] for x in boxes[box]]:
49+
boxes[box].append(lens)
50+
else:
51+
index = [x[0] for x in boxes[box]].index(label)
52+
boxes[box].pop(index)
53+
boxes[box].insert(index, lens)
54+
elif operation == '-':
55+
if label in [x[0] for x in boxes[box]]:
56+
boxes[box].pop([x[0] for x in boxes[box]].index(label))
57+
58+
score = 0
59+
for i,box in enumerate(boxes):
60+
for j,lens in enumerate(box):
61+
score += (i+1)*(j+1)*int(lens[1])
62+
63+
print('Day 15 Part 2:',score)
64+
65+
########################## PART 2: ONE-LINE SOLUTION ##########################
66+
print('Day 15 Part 2:',sum((b:=[[] for _ in range(256)]) and [((l:=''.join([c for c in s if c.isalpha()])) and (o:=''.join([c for c in s if not c.isalnum()])) and (f:=''.join([c for c in s if c.isdigit()])) and (d:=(l,f)) and (a:=0) or 1) and not (a:=0) and [a:=(a+ord(c))*17%256 for c in l] and ((b[a].append(d) if l not in [x[0] for x in b[a]] else ((e:=[x[0] for x in b[a]].index(l)) or 1) and b[a].pop(e) and b[a].insert(e,d)) if o=='=' else b[a].pop([x[0] for x in b[a]].index(l)) if l in [x[0] for x in b[a]] else 0) for s in q[15].split(',')] and [(i+1)*(j+1)*int(n[1]) for i,p in enumerate(b) for j,n in enumerate(p)]))

0 commit comments

Comments
 (0)