Skip to content

Commit 5989037

Browse files
committed
add 2023/03
1 parent f043f37 commit 5989037

File tree

7 files changed

+257
-0
lines changed

7 files changed

+257
-0
lines changed

2023/03/README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# 2023, Day 3: Gear Ratios
2+
3+
You and the Elf eventually reach a [gondola lift](https://en.wikipedia.org/wiki/Gondola_lift) station; he says the gondola lift will take you up to the _water source_, but this is as far as he can bring you. You go inside.
4+
5+
It doesn't take long to find the gondolas, but there seems to be a problem: they're not moving.
6+
7+
"Aaah!"
8+
9+
You turn around to see a slightly-greasy Elf with a wrench and a look of surprise. "Sorry, I wasn't expecting anyone! The gondola lift isn't working right now; it'll still be a while before I can fix it." You offer to help.
10+
11+
The engineer explains that an engine part seems to be missing from the engine, but nobody can figure out which one. If you can _add up all the part numbers_ in the engine schematic, it should be easy to work out which part is missing.
12+
13+
## Part 1
14+
15+
The engine schematic (your puzzle input) consists of a visual representation of the engine. There are lots of numbers and symbols you don't really understand, but apparently _any number adjacent to a symbol_, even diagonally, is a "part number" and should be included in your sum. (Periods (`.`) do not count as a symbol.)
16+
17+
Here is an example engine schematic:
18+
19+
467..114..
20+
...*......
21+
..35..633.
22+
......#...
23+
617*......
24+
.....+.58.
25+
..592.....
26+
......755.
27+
...$.*....
28+
.664.598..
29+
30+
31+
In this schematic, two numbers are _not_ part numbers because they are not adjacent to a symbol: `114` (top right) and `58` (middle right). Every other number is adjacent to a symbol and so _is_ a part number; their sum is _`4361`_.
32+
33+
Of course, the actual engine schematic is much larger. _What is the sum of all of the part numbers in the engine schematic?_
34+
35+
Your puzzle answer was `537732`.
36+
37+
## Part 2
38+
39+
The engineer finds the missing part and installs it in the engine! As the engine springs to life, you jump in the closest gondola, finally ready to ascend to the water source.
40+
41+
You don't seem to be going very fast, though. Maybe something is still wrong? Fortunately, the gondola has a phone labeled "help", so you pick it up and the engineer answers.
42+
43+
Before you can explain the situation, she suggests that you look out the window. There stands the engineer, holding a phone in one hand and waving with the other. You're going so slowly that you haven't even left the station. You exit the gondola.
44+
45+
The missing part wasn't the only issue - one of the gears in the engine is wrong. A _gear_ is any `*` symbol that is adjacent to _exactly two part numbers_. Its _gear ratio_ is the result of multiplying those two numbers together.
46+
47+
This time, you need to find the gear ratio of every gear and add them all up so that the engineer can figure out which gear needs to be replaced.
48+
49+
Consider the same engine schematic again:
50+
51+
467..114..
52+
...*......
53+
..35..633.
54+
......#...
55+
617*......
56+
.....+.58.
57+
..592.....
58+
......755.
59+
...$.*....
60+
.664.598..
61+
62+
63+
In this schematic, there are _two_ gears. The first is in the top left; it has part numbers `467` and `35`, so its gear ratio is `16345`. The second gear is in the lower right; its gear ratio is `451490`. (The `*` adjacent to `617` is _not_ a gear because it is only adjacent to one part number.) Adding up all of the gear ratios produces _`467835`_.
64+
65+
_What is the sum of all of the gear ratios in your engine schematic?_
66+
67+
Your puzzle answer was `84883664`.
68+
69+
## Solution Notes
70+
71+
The tricky part here is coming up with an easy to handle data structure in which to store the input data for easy processing. In the end, what needs to be done is kind of a connected component analysis in horizontal direction, starting at the neighbor positions of the symbols, to retrieve the surrounding numbers.
72+
73+
My initial approach was to use the proven method of a coordinate-as-complex-number to character dictionary, and having a lookup function that "samples" a number at a specified position, if there is one. This does the job well; it even reduces the actual evaluations for both parts to one (lengthly) line each, albeit with lots of nested generator expressions containing nested loops. Still, the "sampling" function stood out as being quite long ... there has to be a better way?
74+
75+
There is another approach, after all: Parse numbers as such right at the beginning, and store them as _(row, start column, end column, value)_ tuples. This indeed simplifies the sampling function a lot (it's a one-liner now), but it makes the input parser more complex (it's no longer a one-liner) and complex numbers are out of the question, because we need to compare coordinates against intervals. In the end, this approach turned out to be significantly worse in terms of code size and, surprisingly, execution time too.
76+
77+
* Part 1, Python (complex number dictionary): 294 bytes, <100 ms
78+
* Part 2, Python (complex number dictionary): 332 bytes, <100 ms
79+
* Part 1, Python (list of spans): 355 bytes, ~800 ms
80+
* Part 2, Python (list of spans): 399 bytes, ~400 ms

2023/03/aoc2023_03_part1_try1.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
E=enumerate
2+
F={1j*y+x:c for y,l in E(open("input.txt"))for x,c in E(l.strip())if'.'!=c}
3+
D=lambda p:F.get(p,'').isdigit()
4+
def V(p):
5+
s='0'
6+
while D(p)*D(p-1):p-=1
7+
while D(p):s+=F[p];p+=1
8+
return(p,int(s))
9+
print(sum(b for a,b in{V(p+u+v)for p in F for u in(-1,0,1)for v in(-1j,0,1j)if D(p)-1}))

2023/03/aoc2023_03_part1_try2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import re
2+
N,O=[],[]
3+
for y,l in enumerate(open("input.txt")):
4+
for m in re.finditer('\d+|[^.]',l.strip()):
5+
s,e,c=m.start(0),m.end(0),m.group(0)
6+
if'0'<c<':':N+=[(y,s,e,int(c))]
7+
else:O+=[(y,s)]
8+
V=lambda v,u:([(y,e,n)for y,s,e,n in N if(y==v)*(s<=u<e)]or[(0,0,0)]).pop()
9+
print(sum(z[2]for z in{V(y+v,x+u)for y,x in O for u in(-1,0,1)for v in(-1,0,1)}))

2023/03/aoc2023_03_part2_try1.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
E=enumerate
2+
F={1j*y+x:c for y,l in E(open("input.txt"))for x,c in E(l.strip())if'.'!=c}
3+
D=lambda p:F.get(p,'').isdigit()
4+
def V(p):
5+
s='0'
6+
while D(p)*D(p-1):p-=1
7+
while D(p):s+=F[p];p+=1
8+
return(p,int(s))
9+
print(sum(g[0]*g[1]for g in([b for a,b in{V(p+u+v)for u in(-1,0,1)for v in(-1j,0,1j)if'*'==F[p]}if b]for p in F)if len(g)==2))

2023/03/aoc2023_03_part2_try2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import re
2+
N,O=[],[]
3+
for y,l in enumerate(open("input.txt")):
4+
for m in re.finditer('\d+|[^.]',l.strip()):
5+
s,e,c=m.start(0),m.end(0),m.group(0)
6+
if'0'<c<':':N+=[(y,s,e,int(c))]
7+
elif'*'==c:O+=[(y,s)]
8+
V=lambda v,u:([(y,e,n)for y,s,e,n in N if(y==v)*(s<=u<e)]or[(0,0,0)]).pop()
9+
print(sum(g[0]*g[1]for g in([z for _,_,z in{V(y+v,x+u)for u in(-1,0,1)for v in(-1,0,1)}if z]for y,x in O)if len(g)==2))

0 commit comments

Comments
 (0)