2023 Day 03 Part 1, Part 2 faulty (working with sample, not with actual input data)
This commit is contained in:
parent
1bf7ba1da4
commit
d0667eabd6
67
2023/03/README.md
Normal file
67
2023/03/README.md
Normal file
@ -0,0 +1,67 @@
|
||||
# 2023 Day 03: Gear Ratios
|
||||
Copyright (c) Eric Wastl
|
||||
#### [Direct Link](https://adventofcode.com/2023/day/3)
|
||||
|
||||
## Part 1
|
||||
|
||||
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.
|
||||
|
||||
It doesn't take long to find the gondolas, but there seems to be a problem: they're not moving.
|
||||
|
||||
"Aaah!"
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
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.)
|
||||
|
||||
Here is an example engine schematic:
|
||||
|
||||
```
|
||||
467..114..
|
||||
...*......
|
||||
..35..633.
|
||||
......#...
|
||||
617*......
|
||||
.....+.58.
|
||||
..592.....
|
||||
......755.
|
||||
...$.*....
|
||||
.664.598..
|
||||
```
|
||||
|
||||
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`**.
|
||||
|
||||
Of course, the actual engine schematic is much larger. **What is the sum of all of the part numbers in the engine schematic?**
|
||||
|
||||
## Part 2
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
Consider the same engine schematic again:
|
||||
|
||||
```
|
||||
467..114..
|
||||
...*......
|
||||
..35..633.
|
||||
......#...
|
||||
617*......
|
||||
.....+.58.
|
||||
..592.....
|
||||
......755.
|
||||
...$.*....
|
||||
.664.598..
|
||||
```
|
||||
|
||||
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`**.
|
||||
|
||||
**What is the sum of all of the gear ratios in your engine schematic?**
|
91
2023/03/code.py
Normal file
91
2023/03/code.py
Normal file
@ -0,0 +1,91 @@
|
||||
# SPDX-License-Identifier: MIT
|
||||
# Copyright (c) 2023 Akumatic
|
||||
#
|
||||
# https://adventofcode.com/2023/day/3
|
||||
|
||||
def read_file(filename: str = "input.txt") -> list:
|
||||
with open(f"{__file__.rstrip('code.py')}{filename}", "r") as f:
|
||||
return f.read().strip().split("\n")
|
||||
|
||||
def check_surrounding_fields(vals: list, x: int, y: int) -> bool:
|
||||
return any(
|
||||
vals[y + j][x + i] != "." and not vals[y + j][x + i].isdigit()
|
||||
for i in range(-1, 2) for j in range(-1, 2)
|
||||
if 0 <= x + i < len(vals[0]) and 0 <= y + j < len(vals)
|
||||
)
|
||||
|
||||
def sum_part_numbers(vals: list) -> int:
|
||||
num_sum = 0
|
||||
for y in range(len(vals)):
|
||||
valid = False
|
||||
num = 0
|
||||
for x in range(len(vals[0])):
|
||||
if not vals[y][x].isdigit():
|
||||
if valid:
|
||||
num_sum += num
|
||||
valid = False
|
||||
num = 0
|
||||
continue
|
||||
num = num * 10 + int(vals[y][x])
|
||||
if not valid:
|
||||
valid = check_surrounding_fields(vals, x, y)
|
||||
if valid:
|
||||
num_sum += num
|
||||
return num_sum
|
||||
|
||||
def find_possible_gears(vals: list) -> list:
|
||||
positions = list()
|
||||
for y in range(len(vals)):
|
||||
positions.extend([(x, y) for x, char in enumerate(vals[y]) if vals[y][x] == "*"])
|
||||
return positions
|
||||
|
||||
def check_left_of_symbol(vals: list, x, y) -> int:
|
||||
if not 0 <= y < len(vals):
|
||||
return -1
|
||||
i, num = 1, 0
|
||||
check = False
|
||||
while (x - i) >= 0 and vals[y][x - i].isdigit():
|
||||
check = True
|
||||
num += int(vals[y][x - i]) * 10**(i - 1)
|
||||
i += 1
|
||||
return num if check else -1
|
||||
|
||||
def check_right_of_symbol(vals:list, x, y) -> int:
|
||||
if not 0 <= y < len(vals):
|
||||
return -1
|
||||
i, num = 1, 0
|
||||
check = False
|
||||
while (x + i) < len(vals[y]) and vals[y][x + i].isdigit():
|
||||
check = True
|
||||
num = num * 10 + int(vals[y][x + i])
|
||||
i += 1
|
||||
return num if check else -1
|
||||
|
||||
def determine_gear_ratios(vals: list, possible_gears: list) -> list:
|
||||
result = list()
|
||||
for x, y in possible_gears:
|
||||
part_numbers = list()
|
||||
for j in (-1, 0, 1):
|
||||
tmp_l = check_left_of_symbol(vals, x, y + j)
|
||||
tmp_r = check_right_of_symbol(vals, x, y + j)
|
||||
tmp_m = vals[y + j][x] if 0 <= y + j < len(vals) and vals[y + j][x].isdigit() else None
|
||||
if tmp_m is not None:
|
||||
nums = [int("".join([str(x) for x in (tmp_l, tmp_m, tmp_r) if x != -1]))]
|
||||
else:
|
||||
nums = [x for x in (tmp_l, tmp_r) if x != -1]
|
||||
part_numbers.extend(nums)
|
||||
if len(part_numbers) == 2:
|
||||
result.append(part_numbers[0] * part_numbers[1])
|
||||
return result
|
||||
|
||||
def part1(vals: list) -> int:
|
||||
return sum_part_numbers(vals)
|
||||
|
||||
def part2(vals: list) -> int:
|
||||
possible_gears = find_possible_gears(vals)
|
||||
return sum(determine_gear_ratios(vals, possible_gears))
|
||||
|
||||
if __name__ == "__main__":
|
||||
vals = read_file()
|
||||
print(f"Part 1: {part1(vals)}")
|
||||
print(f"Part 2: {part2(vals)}")
|
140
2023/03/input.txt
Normal file
140
2023/03/input.txt
Normal file
@ -0,0 +1,140 @@
|
||||
..975..95..................717..........................................747................................................622..............
|
||||
................/47...........@....701...610.........252.660*.............*..236.....323..........108........653............................
|
||||
.......69............313..............$...$...*....@.........640........81.....*................................*332..92....................
|
||||
........................*...210.............767...912...367.......505.......817..................*.........478.........=....223.....568.....
|
||||
..76...968............108...@.....556.....................=..........*...............412..313...575......../...........................*107.
|
||||
............773/..891............*....................744.....805...14................../..../................320&.567..#...................
|
||||
.962..708............&........399....146.....385.................*..........825.......................................-..655....485...-.....
|
||||
...*.........+..........................*76...+..................242....997..*......185..........207.390..870...883............*.......337..
|
||||
....929...299....................*.................249........-...........*.373.....*.............*.....*.*....*.............549............
|
||||
................196.745.996...364..............409...-.926.....273.....580.........31...........545..547...862......810.....................
|
||||
................*.......*..............900+....*..........*........586.........936...................................*.........673..........
|
||||
.../..........864.683.215....*......%.........702..166..46..........&....341..........694..496.......528.%...........839...105..*...887.....
|
||||
....506...617.....*........51.807....942..424...............................*..........@..+............@.84...................*......=......
|
||||
...........@....594.........................*..........*675...........345..52...$..................-..............899.........804.$.........
|
||||
.362*366.................487+.48&.......600..180....845........................535...18...........397.#908....432*........229......346......
|
||||
.................*................835....................578......*502...............*..416.............................................391.
|
||||
....610&...514...501......./........*....*71.........../.......674......925...............*.869.....966........393................/.687.*...
|
||||
............*...............786...248.104........92.633.....%..............*104...&466..138..@.........*.525...+......974......339..=...260.
|
||||
....372/.889..............................55.406........134..250.=228..609...........................69...............*.....................
|
||||
...........................................*........976...*...........=.......357...........................191.....150.....................
|
||||
...-...........595.634.........*396......592..594......*......411.856...760....../..................201.....#...709................168......
|
||||
.936..236.........*....@....436.....100....../......35...../.....*........*....................917%............*.....................*..#63.
|
||||
.......*..............542..........................*........689.........26....667.&...................738....831..$.............513.53......
|
||||
........10.....529........*250..........162.......390...........................*..70.............................701..........*............
|
||||
..355.........&........394......301@..........542..............................142..........%..............%..........*.....507.............
|
||||
.....*.........................................*.....%..283.....*.........997&.......@670....402..........808......308.459.......712...152..
|
||||
..291......246.@....+986.....336/.......976...468.264...*....739.187.............................................................*.....*....
|
||||
........../....46...................186.$..............674............................227......-............975...............436.......492.
|
||||
.491........................810.......*........................772*.........742........%....198.....*702.......*.596=....64.......=.........
|
||||
...................706......=......415..................=.199......235......*...47........................72.607...................443......
|
||||
.214..............*.....771.............302...........230..+..............810.....*219.606........912.....................=.................
|
||||
......277......320.............937.....*.....+....632...........&374..............................*....................898.....642%.........
|
||||
.........#....................*.......716....229./.........................$................934...233...............98............../.......
|
||||
.....*........-659..457.........964...................659....786....619.666........277...@.....*......930*460..618...........910..265.......
|
||||
..845.877.343......................*537.../120.......*.......*.....*.................&...175..631.............=.................#...........
|
||||
............*....................+................@...35.949..34.381......273...880......................451........326.....................
|
||||
............876..880......827..733.......645.......38......*.........402.....+..........902....459.775........837.....*.185.......813.......
|
||||
........578................/............*...............622..18.........*234..............+....%..........448.......935....#.............350
|
||||
..........*...468.....546................588....146=.........*..142.795..............*424................../.....................=..........
|
||||
........245............@...746.......362..................970......*..............593...............487.......+.............709..593........
|
||||
...990......&415..............*381......*.....................435.......214..908..................*....*.....351.....876.25....*............
|
||||
......*............293*.....%...........271.............135......*......=........540............177....604...........&.....-.46..........166
|
||||
...609.....................910.................@........$.......321..........141....+.45.....................750......................98....
|
||||
.......#....779........944.....653...........797...........788.................*......+....164.724....475&...+.............-.471......&.....
|
||||
..794...487....&..81...........*...................@............666@.....*981.937.............*.....%..........888....+...81................
|
||||
.....*...........+......694.............748...104..859..............................675.434.......290.............*..352........353=..$.....
|
||||
...619.977.........859..#....235....230*......*..............429........335....-.......*......631.......=.......27...................192....
|
||||
.......*............=...........&.........121..680.195.959......*417.....*..938.............../..........739..#..............194.750........
|
||||
....309...439.............14...............*..........*...............723.........163..748...........853.....885.........175*........487....
|
||||
.............*707..-626..........464.....382................356...........868#....*...*.....13......*............450............#.....*.....
|
||||
........216......................*...619......$..270........*.....892............25.494.841*.....971.............*............157.+...119...
|
||||
...#40...../............343..559.425.......536........718....739...#.......284...............726.....985..136...597.621.173.......203.......
|
||||
................383............/......................*...................@....348............*.....*.......*........./...*...811.......*158
|
||||
...=..............$......@....................776.829.85...&........541.......*....*...&....700......341.995.....73......114....*....267....
|
||||
.700.203....591.*.....924..........$217.......@.....*.......590....*.........812.579....517.......................*.............730.........
|
||||
......#......*...586.........312..................806............435...259...........................573........453.282*132.243.............
|
||||
..........224.................#.......468@............-...%.............*..............453...................$.....................-........
|
||||
....................................................227.673....881......828..&.....632*...................589.....................284..-....
|
||||
.....680.........871....17+................117.................*.............506..................#...410...................616.........519.
|
||||
........*..386................78*762.........@........28..557.655.......359........-............124..#......&..........843./................
|
||||
......724...*..584.....552............................*...&.................209..857...633-..............602...932......*........132&.......
|
||||
..........41../.........*..........................#..427.............=370....*................................*...353...902...........529..
|
||||
.....................150.......868.......370....549.........779...................284...........431.....3.....889.............189..103.=....
|
||||
.......530..355..........442...*............*63............+.......852.....935.....*..............%.....*..................@....*.-.........
|
||||
..........*...*...976.....*...500....@...........572...........940....*990..*....627.790.................460..904..601-..821..930....699....
|
||||
..........643.824.......248........358...4.........&..301.........&........538.........*.%436....565..........*......................../....
|
||||
.768..$.....................186..........*...178........*.......................*.&520..............%........782..........497.-......*......
|
||||
......762..828*...972......*..............3.*.......................@....../..836...............278..............666......*...927.413.185...
|
||||
....&..........40.......86.122..........&....450....................1....202............42...51....*..............*.....215.................
|
||||
.353.................94*.................995.....655.....520...828....*...........612..........&.119...........486...............713........
|
||||
.........................255........................*.......+.....-..463..........*........235........836....................668............
|
||||
...................................................286........338........=.393....398........*....199./............631..........*631........
|
||||
................64%....704...928.........470...125............*....708.15...*.............193.......*........641....*......687..............
|
||||
........$..............*...................&.....*..........582......*....652.@358..643..........125.....458........168....$.......$194.....
|
||||
........293.........305......417...............117....................612............*......262.............*903............................
|
||||
.....$...................615*..........581.........335...139..505.419.............656.........+.......506................710.....956........
|
||||
776..110.....+............................-........#..........*....*...447......*......................*...*.....938.366*.......#....376....
|
||||
..........366............844....305..761......780......560........236........725.272.....450.............67.126.....................*.......
|
||||
.................669$.......$.......=............*220./.....+665........495................*.@103....992....................&........600....
|
||||
.......*......................812/.........................................$............940...........+...................146...975.........
|
||||
....410.650.282.....*...............696.833....957...............996...*......772.287...................921..935................%.....$.....
|
||||
............*....413.921...713....................%.....769.........@.390.216..#.....@..........................=...........*.......46......
|
||||
.....798.136..............%....150.....236..............*......31...........*..........@.64..17......616..........901......344..............
|
||||
..............................-........*.....402......873.......*......692...592....390...*.............*277................................
|
||||
......539.....799.../.....272....%.@....694.#.............667...318.......=...............762.......71%.......-153......................531.
|
||||
..........556*......573.......465...916...................*..........602.........................=.......145...........................*....
|
||||
........................451.............../....%.....625.401.........*........428-.....493......479.427....$.........60......951......281...
|
||||
..947+....36....270....*............517....521..258.*............108..812.............*................*........................*372........
|
||||
...........*....*....957.823...290.....*.............415..........*...............908..730..........589......+..............................
|
||||
857.........843............*........794......................677..27........................121...........670.........548.837*....&.....664.
|
||||
......175..........-.....=........................422.......*..................226-..344.......*...................2....%..........474......
|
||||
...................283..190..@...........458........*.162@.666.517....202...............*..@...545.%.........705.............*..............
|
||||
...............*............296.466.....#.........................*..*.....852..208...999.475......256..........=....642..188.386...........
|
||||
............504.888..................52....416......394...349.....53.953...*......*...........@521.........213........*.....................
|
||||
......225...............272*32........*.....*..........*...%..............728.........827.513..........287*.........924..388%.........&.....
|
||||
..453...*..................................71.....*..437.......501...869.......928....%...#....................%225.............734@..157...
|
||||
....*..400...........810=.300@...13.15.537.....997........./......*.....*.........+.........407.758...674............604....500.............
|
||||
..970..........409...............*...*...*................159...788....338..............636*...../....#.......#895....*......./.............
|
||||
..................................52.....872........+...........................-........................524.........297............314.....
|
||||
...........=...469.....408.............&.........641......364%.780...526.........540.865....742............*..............549..505...*......
|
||||
........404.......*...-....58..%....577...............655......*.............613......*.............343..779./854............*....*...567...
|
||||
...103............284......./...999....................=.....640.....284..............566.982..........$...........476...478.793..556.......
|
||||
......*.....436........916.................941.............@.........&...........284.........*183.........857.953...........................
|
||||
...............*.404=.....*910..703@...20......266......815.....201.....54.332...........................@......*...........95..............
|
||||
.............898........................*........*..........13.....*806...*.......280.........................905........63*................
|
||||
........&.........642..271.627..920.+....534...96..780......@....=...........246../......99........................................../......
|
||||
....$....518.......@......*......*..323..............-............477....*4...*...........*....979......810.................366....697......
|
||||
.....439........................364.........161..594.....647..................802.........234..............*17......125..82....*............
|
||||
................187....667....................*..#.......*.......*................604..................946.............*....414.............
|
||||
....585...218.....*...*.............-....536.396........950...200......296*935...*..........#...382..........883......182.......326....*....
|
||||
.....+......-...557.349...........217............................./105..........527..387....394...*...........#..430*.....501.......393.190.
|
||||
.......*474.............16....................675.....999..............362..............*..........576...................*..................
|
||||
....449.....+.296*468.....................607....*...........-..#.................619....177...............31...........194..198..187.......
|
||||
.........156......................330.......+..332...631...936...477................*...........658..........%..845..........*..............
|
||||
....*262.........$......362......*.....429=..............................*324....296...........+.......161.....*....57.....309..............
|
||||
.403............492..97.@.....695..696......$.840..&.................21.....................@.........*.......178...+....$.............364..
|
||||
......178..692*.....%.....82..........=...176.......456.....517........+...238.704.......682......266.................&..222..........*.....
|
||||
......*........993........*....931*...........445..............*.................=...........@...*.......448.......251................222...
|
||||
.......824............467..232.......664..299*.......322*512..850..............@..........585....259........$..................&............
|
||||
...458.....572.................379....*........123.....................-........298..................-................336...908....*........
|
||||
....&......*....&..732...701/....*....440.....$...........739..636.....65..../.......%151.............823.455............&..........119.....
|
||||
........550....391..%.............259....................#....*............834............232.179............*.........4...795.784..........
|
||||
.............%...............729...............568.............936...............952........*..*..............726..........*.........-......
|
||||
...627.....87....@...........-...........6.....%...106.......................458*.....281..811..282..500..............=....833....214..780..
|
||||
..............320................665..............*........584.........................*............................343...............*.....
|
||||
..................997....84.590.....*795.90*487.842..%431....*.......233..692....259....391.113*........*338............./..391.............
|
||||
.....62.....#689.*.........*.................................444.......*...*......*.............445..861...........498/.857..*.....505*.....
|
||||
......*...........436.869.....634@..663...599..901*678.100..........666....301..493.986....174.............838*492..........454........313..
|
||||
.....362.....983....../................+..*.............=.............................*....*.......259.............+....464.........27......
|
||||
...%........................580....68.....45.........................................21..768...267....*.+....661..879..@....&......$........
|
||||
.643....545......./435.....*.........+.............................823....919.......................20..102.*..............267.690.....55...
|
||||
.........*...............557....247.......653....584.5*....430....*........../...529+...........856..........305.254..212.............*.....
|
||||
........945...528................/...775..+............204...-.537....$..528................276*...................%....*....863......612...
|
||||
....572.......*.........614.........+................................810...............842........438.....26...&.....%.........*............
|
||||
.......*....201.@......*....671..$........................../.............................*...832*.......*....214.899..........795.421+.....
|
||||
994..797.........684.390....*....452../..................989..728............685....23+.856...........498.............443...................
|
||||
.....................................424.......%...............@.......228.....+..............+.......................$.....................
|
||||
.....2...812.517.........602*967.........262....953...*.....*....67...*...........+......1.431.....721.....402%..&.......*.....504..........
|
||||
.............*....%..................963*...........96...287.255.*...548.........534....*.............*..........896..531.789.=.......855...
|
||||
...........579....595...........8................................781...............................547......................................
|
2
2023/03/solution.txt
Normal file
2
2023/03/solution.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Part 1: 520019
|
||||
Part 2: 73239378
|
14
2023/03/test_code.py
Normal file
14
2023/03/test_code.py
Normal file
@ -0,0 +1,14 @@
|
||||
# SPDX-License-Identifier: MIT
|
||||
# Copyright (c) 2023 Akumatic
|
||||
|
||||
from code import read_file, part1, part2
|
||||
|
||||
def test():
|
||||
vals = read_file("test_input.txt")
|
||||
assert part1(vals) == 4361
|
||||
print("Passed Part 1")
|
||||
assert part2(vals) == 467835
|
||||
print("Passed Part 2")
|
||||
|
||||
if __name__ == "__main__":
|
||||
test()
|
10
2023/03/test_input.txt
Normal file
10
2023/03/test_input.txt
Normal file
@ -0,0 +1,10 @@
|
||||
467..114..
|
||||
...*......
|
||||
..35..633.
|
||||
......#...
|
||||
617*......
|
||||
.....+.58.
|
||||
..592.....
|
||||
......755.
|
||||
...$.*....
|
||||
.664.598..
|
@ -16,7 +16,7 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day
|
||||
| --- | --- | --- |---| --- |
|
||||
| 01 | :white_check_mark: | :white_check_mark: | [Solution](01/code.py) | [Day 01](https://adventofcode.com/2023/day/1) |
|
||||
| 02 | :white_check_mark: | :white_check_mark: | [Solution](02/code.py) | [Day 02](https://adventofcode.com/2023/day/2) |
|
||||
| 03 | | | | [Day 03](https://adventofcode.com/2023/day/3) |
|
||||
| 03 | :white_check_mark: | :x: | [Solution](03/code.py) | [Day 03](https://adventofcode.com/2023/day/3) |
|
||||
| 04 | | | | [Day 04](https://adventofcode.com/2023/day/4) |
|
||||
| 05 | | | | [Day 05](https://adventofcode.com/2023/day/5) |
|
||||
| 06 | | | | [Day 06](https://adventofcode.com/2023/day/6) |
|
||||
|
Loading…
x
Reference in New Issue
Block a user