solve Day03 in python
This commit is contained in:
parent
a10377789a
commit
4d560c4530
60
Day03/README.md
Normal file
60
Day03/README.md
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
# Day 3: Gear Ratios
|
||||||
|
# Part One
|
||||||
|
You and the Elf eventually reach a 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 Two
|
||||||
|
--- Part Two ---
|
||||||
|
|
||||||
|
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?
|
140
Day03/input.txt
Normal file
140
Day03/input.txt
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
...................15....904...........850.................329...................13....................................871....816....697....
|
||||||
|
...........53.497........................%....906...610.......*.............735#..&...*......558...68...............68..*......&....*.......
|
||||||
|
..........*....$....................132.........*..........844....875................350............*...............*..336.364...649........
|
||||||
|
.......726.......341..................*...186...358..................*244........57.......@.........738......*.....663.................584..
|
||||||
|
.............952.*......33......660..704............949......................518*....234.967....551........971..&.......................*...
|
||||||
|
.......738...*....222......................706.......*..825.............474%...........*...........*.405.........779..............542...405.
|
||||||
|
.74.........366....................192..........542.737....*760...................623/..730.....718.../.....................$17......%......
|
||||||
|
...*126.....................%........*.504...=..*.........................................................974...............................
|
||||||
|
........331/..901.........337..........*...461.698...............*461.....814............................*............975...............165.
|
||||||
|
..../.........*.....................262.......................313........*........530.56.....567.......897.....*.........*.9................
|
||||||
|
....953....355...@703..................................609..............462.......%........./...............108.557...501.../...724.........
|
||||||
|
..............................................=.........%.......46......................533....670...............................*..%630....
|
||||||
|
.........................91...382*204.........154..%.............+.524.995..............=.....*...........7..........692.92*56...73.........
|
||||||
|
.685....*....70.189.......*.........................773...830/....../....*........=.........565.............464................$....738..130
|
||||||
|
.........938..*........993..............*.................................243...340...753...........*...882...............108.647....*......
|
||||||
|
..............762..........348.......280...............364*526...........................*........552.....=....25..682....*.........164.....
|
||||||
|
.....948..............813../....644......62........................................#.....657......................*.........................
|
||||||
|
......*...........................*.................$745.......739.....*...399...30............&..@...........924.117..........309..........
|
||||||
|
...883....544.....33.........585=.428.146................288.....*..853....*...........*891.409....429..460...*..........997....*........187
|
||||||
|
.............*818........829..........*....846..............................248.....574..................*....789.......*.....965....-..*...
|
||||||
|
......=.....................*..........370.......850+.............313...........................573....621...............268.......58..800..
|
||||||
|
....67....287.............481..709...............................*.......#74...................*.................965........................
|
||||||
|
............@.291.............*.........607*....950.475..309...66..932........395..374..........155.891....472..*......774.............&....
|
||||||
|
....207..........%..262......543..887/......711....*.......+......*............*...*........823............*...93...........377.....764.....
|
||||||
|
.....*...968.951.......#................................$.........453...366..736.972........*.............697........*618...&...............
|
||||||
|
....984..*...............-.......................884..23......492.....*..*................70..523.....25........$.....................370...
|
||||||
|
..................870...187.......548#.522..458.....*..........*....956......535....../.........*.525*........197....@..*266...123.....*....
|
||||||
|
.........501.....*..........................&.....323........257........956..*........80..946.592.................211.........*.........311.
|
||||||
|
.....380*.....867...653..347....................*........93=...........*......515..........-...................................678..........
|
||||||
|
..........876......&....*.......&631.950.647...631...@...........=...705..914................739..............319......713.........*97.#918.
|
||||||
|
.....805...*..........118................-.........790....559..51........../........*.........*......550......*........./.......647.........
|
||||||
|
......-..428.....599......813................413..................280............840.654.......91.....*...........236.......$...............
|
||||||
|
.............=...%.......*.....77...................#.492............................................96..........@.......175....%...........
|
||||||
|
......*427..569..........47......................999......314.....636*253.................764.................27......-..........415..386...
|
||||||
|
...142................$................@..320........969...................882*311...............#..751...........=....649...........*......
|
||||||
|
.........*723..993.976....638........475................=..800.......................*764.710...574....*964....831..........473......531....
|
||||||
|
...%..486...................*....#..................................831...........375........*........................=824......21..........
|
||||||
|
559.........................441...316................................./....................618.407.......83.......972...............91......
|
||||||
|
......*.....25..741...................%...............*63.546$..905......27........861.756........*.......*..........*...28...57............
|
||||||
|
.......794....*./........782...236....47...........937...........*..755....*505..............23.948.......560.532...421....*..#.........405.
|
||||||
|
............566.................*........-850..872.......90....229...+..........978..684............225..................675.....591...*....
|
||||||
|
.............................296..243...........+....691...................311.........$......321......@......827....$13.......$....&..530..
|
||||||
|
.....664..422$....613.....@.........%.401/.$974.....-.........%.145....729......*........./.......106............%.......=.....611..........
|
||||||
|
.......$.........*.......857...147*.............591........+.46..*.....=........306.67*...818.............................261....../........
|
||||||
|
............823...785..............340.........-....564..777......456.......%....................891.................560...........642..181.
|
||||||
|
.....394.....*................226..........423..............................539.................*............640&.............535...........
|
||||||
|
.......@.....626..317..329....*...92........+..................374....827............*.683+....771....529.........303..........*............
|
||||||
|
.........172.....-........*......*.....332...........288..229..+.........*.786=....468.............*....*...........*....461...249..=.......
|
||||||
|
....974...*............901..@....130..*..............$...*..........4.573................163......429....808.464*47..231....@......964......
|
||||||
|
........183.................582........11...............310.....774.*.....567.............*....36...........................................
|
||||||
|
.......................939.....................*826............@.....892..........481......266.*...-253.........106.830....235......*..841..
|
||||||
|
727.........177#.........&.......507........301..........................550..242*.....+./.....686.......@.446.*.....*.......*....894..*....
|
||||||
|
....67...............468.....&......&..596......................817.600...............85.5.............973...$.805...513......378.......388.
|
||||||
|
...........*247.........*.....974.........*263....329....476....*............721............723.....................................358.....
|
||||||
|
........784.....434.....667.......................*............615.195.612...............................540#........347...........*........
|
||||||
|
...............*.................76......121...659........@555..........-.............201......................866............628.505.509...
|
||||||
|
.............515........../.................#....................281..%...656............*670.419.......799.......*...510.786...*.......&...
|
||||||
|
....993.................540.373....709.............705............*...132..#........298$......*...232....*..@..878.......*....528...#.......
|
||||||
|
....$.........615.............*..........948..........*........565................%..........465..#.....497.67....................631.......
|
||||||
|
.........138....#....417......123...........*.......546...821...................114..=............................258.859=.380-.............
|
||||||
|
.....734*.............../.............833..399..........................+............780...288..............117..@...................925....
|
||||||
|
................*860..........228........*...............................956....390*........=..........%...*..........839...373......*......
|
||||||
|
..802........369........641.............763.....154.24...............302............620..............150.403.......*.........*....647.......
|
||||||
|
...................................&........466*.....@..................*827....530.............................129.628.....923.........=866
|
||||||
|
..........&......&.............824.573.................669.........................$.....374..627...........698.....................759.....
|
||||||
|
.......499....%.812.857*653...................668..........879.......974................../......*926...852.........................-....832
|
||||||
|
...646......284..............708...369...........*808................-.......705.......@...............%.............492.462............*...
|
||||||
|
....................959......#.......*....872..........................102....*.........80..839....996..............*.....%........83...49..
|
||||||
|
.785............363..*.............49.......*.......667.....797....224.......586..............&....*...34..673.......650....................
|
||||||
|
....*..*132....*.....225...................631..350..*.........*....*.............941....=.......52....*.................406...546..........
|
||||||
|
.543............760.........305..325...330.......+.................621...........*.......63..#.......268..239...................#...........
|
||||||
|
........658..................*............*...........735......820.....145....419...........91..489.........*......404.......*....#.........
|
||||||
|
..........=....415+.....38$....844...+..366.............*.......*......*..............................43....945.....#.....563.205..211..183.
|
||||||
|
......267.......................*...519.....730........836..........277...........808...........504...&................................#....
|
||||||
|
.....*..................705/....575..........*....../..........$866.........=.......*...975......#.......183.931........%.......611.........
|
||||||
|
.......135.......284........................246.653.972..............365..191.....376...*.....&.........*......*.........994...@.....11.....
|
||||||
|
...440../.........*.............................*..............415.....................985..838......677.......109...................*......
|
||||||
|
...@............224....399.727$......68#..........................$......432.....214$.....................#.................591....686......
|
||||||
|
..........&.......................................658........143....779.+....942......$.............885....567..........$...................
|
||||||
|
.461.......33....277.407......108..............47.*.............*...........*.......$.811............................390....................
|
||||||
|
..........................907*.....#397..307...*....197.12.....3...738....254.85..393...............713..700..741...........176.....883.....
|
||||||
|
..316........180...........................*.758...................-...........................................*............*...............
|
||||||
|
....*..../........871...338..165.........683............455...701*......................540....255.......$....858........277....28....+.....
|
||||||
|
...660.469..912...#..........%......743.........439.....-.........206......363..........*.......*.......640............................144..
|
||||||
|
..............*.........443...............610.............803.............*.............879...658............$................189...........
|
||||||
|
...........172......794........98..............646......................662..............................*....49.-210.523.257....*..........
|
||||||
|
...87..662.....#.......*667....%..408..........*............220...524............=.....742..39..160....64.636..........*..*....974..378.....
|
||||||
|
...*.....*.....42..&.............=....371*.....804.................*..#....../.240.....*........................395%..461..818.....*........
|
||||||
|
....706.398........452....500.............406..........57*380....365.42...635.......131...........18.....538.....................403........
|
||||||
|
..............143........*.........80............740..........................*245......784........-...........708...................&.297..
|
||||||
|
.230...........*.......373..341...........................$438.......*.................=.............874&..959*...........192......829.#....
|
||||||
|
...#..........476..647.....*......................................431.728..........................................50.....$.................
|
||||||
|
........334...........*731.930......=........966........*323...................301.....%....404...............879....*372.....208.134.......
|
||||||
|
...........*.....................163..........*......509......................%.....&..785.......................*...............*..........
|
||||||
|
...768..329....199........................................797......*368..........873..........26.565....651.....664......111..........217...
|
||||||
|
................*.....26.......705*76....415.............*................................749..........*..................#......545-..%....
|
||||||
|
....-....473..517......*...............5*....449..........68........................751.......*399......401.................................
|
||||||
|
.730......#.........240...&..65.718.87......*.....532..............-....567............*....................675.......67..463...............
|
||||||
|
.....289#....509........200......*...*....852....................885...+....663..72..507......998.&954..213....$.....*.........672..........
|
||||||
|
..............................403.....129........377-....................../....&.................................473...........*...........
|
||||||
|
.........855=.........250...................961........860*916.......981..........326.........633..404................676.%566..120.........
|
||||||
|
892...........532.............754.......866*....790..................#........819*..............*.........%.44..=......*............29......
|
||||||
|
....@527.......*..................600............../...$568.....800.....................179..528........613.....358...949...................
|
||||||
|
................302...807............*..895.................+.....@.....-...........................980...........................736.......
|
||||||
|
........217...=......*............879.....+.-..........%.....793.......442.......422............209*.......&.......969....240.378...*.......
|
||||||
|
....340*.....145...%.596....................23....703...217................372..........610.................129.......*........../.....=....
|
||||||
|
.................790.....&............904............=............795......*.......385=...+.....88*143...............891....406.......79....
|
||||||
|
............*749.........31..............*....318.................+.......317.................................%419.............*392.........
|
||||||
|
....854..797......*..500...............614.1...*..............803.....451............330.159.143.598.......+......./827...............717...
|
||||||
|
...@............924.*........................245............-.&..........*...606................*...........461............604....#....*....
|
||||||
|
....................73....160..406.150...............933.957...................%.......419.........665..*.........-.......*.....721..573....
|
||||||
|
....*915.........40................*...%.............*........584.......929......377....*..404.935....&..870.....93........395..............
|
||||||
|
.781.............@...........632.......710....769.323............$.........*.........904.....*...*.....................859..................
|
||||||
|
.....=.......................*...646............-.........................52................149...414....................*........%.........
|
||||||
|
...51....524.625..........328...*.................................295.734......@..........................856.291.695....367..218..787..8...
|
||||||
|
.........../.*................339........................576/.417*...........445...@........................%....................-..........
|
||||||
|
....869.85...316.....308...............345.......749.............................264.............775..777...............................89..
|
||||||
|
.......*...............#.........$....*..........*....../.....786.713*52.................357.......&./.........#......971....74.305....-....
|
||||||
|
............................325..770..167..@.....265..+..883................................+...57..........312.......*........*............
|
||||||
|
......+.....266.....788.111....*..........342........588.......939..393*717.............488....*....................401....249...255........
|
||||||
|
.......308..*............#.....816....465..........*.....492..@.............581.........-.......397..709*.................$.....*...........
|
||||||
|
.............511.....881...+.........+..........178..524*..............150..../..720.......843...........964....129.../......908............
|
||||||
|
.......749.............*.526.$............800............................*.........*...500....*.../.................487..........200........
|
||||||
|
..668......*639......932......139.726................#...51..436$..................775...*...834..874.......%..............859+......439....
|
||||||
|
........810........................*.......805.......100..*...........999.....743......169............477...961......973............$.......
|
||||||
|
.574...........6*262........398....204......*.....%.......525...........*.........................412..*.........376...*.@43....=......=....
|
||||||
|
....*836....................................25...619............658.....172.......................*...408...........*........%...776..802...
|
||||||
|
.............307.......&........537..988..............128..-.....*..........................118...327......296.......967..991...............
|
||||||
|
.....899...$...*....788.....829......=....................559.722.......519.........385$...+..................*.323*..............*123......
|
||||||
|
........*.930...674...........*..........447.......801..............957....................................111......767..34....556..........
|
||||||
|
..565.644..............18.....472.........*...8........................*........258........901.................296.......*..........*.......
|
||||||
|
....*............995=....*................649.*...151.........437%...445.747......-.........&......#......%.....*.........800....357.240....
|
||||||
|
....476.491/.931.......121..732*940..682......975..$.....*..................+.358.............../.363....152.264........$...................
|
||||||
|
.............=....730*..................*584...........234.........996.................*.....701........................499............649..
|
||||||
|
......................672......@.............................958.....&.+...........983.673........233.@981........760.......................
|
||||||
|
..942.993@......293...........939..&....@.........867..92.....*........679.........................*.............*.......27...998...........
|
||||||
|
....#.......230.$...................36.24....809..#....*...660.....$.......748..199.-.............717...@......................../..........
|
||||||
|
......*95..............647...$..............-.........461..........757...........*...68...............728..210*680..708......$.....246......
|
||||||
|
...355.........*..........*.538...%..............................-...............977....209*.......................*.........141.....*......
|
||||||
|
...............680.....670.........784........171..799.........317..........................844........166........289.................463...
|
77
Day03/python/solution1.py
Normal file
77
Day03/python/solution1.py
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
def parse_schematic(file_path):
|
||||||
|
"""Reads the engine schematic from a file and returns it as a list of strings."""
|
||||||
|
try:
|
||||||
|
with open(file_path, 'r') as file:
|
||||||
|
return [line.strip() for line in file.readlines()]
|
||||||
|
except FileNotFoundError:
|
||||||
|
raise Exception(f"File not found: {file_path}")
|
||||||
|
|
||||||
|
def is_symbol(char):
|
||||||
|
"""Checks if a character is a symbol (not a digit or a period)."""
|
||||||
|
return not char.isdigit() and char != '.'
|
||||||
|
|
||||||
|
def get_adjacent_positions(rows, cols, row, col):
|
||||||
|
"""Generates positions adjacent (including diagonally) to the given coordinates."""
|
||||||
|
for i in range(max(0, row - 1), min(row + 2, rows)):
|
||||||
|
for j in range(max(0, col - 1), min(col + 2, cols)):
|
||||||
|
if i != row or j != col:
|
||||||
|
yield i, j
|
||||||
|
|
||||||
|
def find_start_of_number(schematic, row, col):
|
||||||
|
"""Finds the start position of the number that includes the given digit."""
|
||||||
|
while col > 0 and schematic[row][col - 1].isdigit():
|
||||||
|
col -= 1
|
||||||
|
return row, col
|
||||||
|
|
||||||
|
def extract_full_number(schematic, start_row, start_col):
|
||||||
|
"""Extracts the full number starting from the given digit coordinates."""
|
||||||
|
start_row, start_col = find_start_of_number(schematic, start_row, start_col)
|
||||||
|
number = ''
|
||||||
|
rows, cols = len(schematic), len(schematic[0])
|
||||||
|
col = start_col
|
||||||
|
|
||||||
|
while col < cols and schematic[start_row][col].isdigit():
|
||||||
|
number += schematic[start_row][col]
|
||||||
|
schematic[start_row] = schematic[start_row][:col] + '.' + schematic[start_row][col + 1:]
|
||||||
|
col += 1
|
||||||
|
|
||||||
|
return int(number) if number else 0
|
||||||
|
|
||||||
|
def sum_part_numbers(file_path):
|
||||||
|
"""Calculates the sum of all part numbers in the engine schematic."""
|
||||||
|
schematic = parse_schematic(file_path)
|
||||||
|
total_sum = 0
|
||||||
|
visited = set()
|
||||||
|
|
||||||
|
for row in range(len(schematic)):
|
||||||
|
for col in range(len(schematic[row])):
|
||||||
|
if is_symbol(schematic[row][col]):
|
||||||
|
for i, j in get_adjacent_positions(len(schematic), len(schematic[row]), row, col):
|
||||||
|
if schematic[i][j].isdigit() and (i, j) not in visited:
|
||||||
|
visited.add((i, j))
|
||||||
|
number = extract_full_number(schematic, i, j)
|
||||||
|
print(f"Found number {number} at line {i+1}")
|
||||||
|
total_sum += number
|
||||||
|
|
||||||
|
return total_sum
|
||||||
|
|
||||||
|
def run_tests():
|
||||||
|
"""Runs tests on the test file and prints the results."""
|
||||||
|
test_result = sum_part_numbers("../test.txt")
|
||||||
|
print(f"Test Result: {test_result}")
|
||||||
|
assert test_result == 4361, f"Test failed: Expected 4361, got {test_result}"
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Main function to run the test and then process the input file."""
|
||||||
|
try:
|
||||||
|
run_tests()
|
||||||
|
print("Test passed successfully.")
|
||||||
|
final_result = sum_part_numbers("../input.txt")
|
||||||
|
print(f"Final Result: {final_result}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error: {e}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
83
Day03/python/solution2.py
Normal file
83
Day03/python/solution2.py
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
def parse_schematic(file_path):
|
||||||
|
"""Reads the engine schematic from a file and returns it as a list of strings."""
|
||||||
|
try:
|
||||||
|
with open(file_path, 'r') as file:
|
||||||
|
return [line.strip() for line in file.readlines()]
|
||||||
|
except FileNotFoundError:
|
||||||
|
raise Exception(f"File not found: {file_path}")
|
||||||
|
|
||||||
|
def is_symbol(char):
|
||||||
|
"""Checks if a character is a symbol (not a digit or a period)."""
|
||||||
|
return not char.isdigit() and char != '.'
|
||||||
|
|
||||||
|
def get_adjacent_positions(rows, cols, row, col):
|
||||||
|
"""Generates positions adjacent (including diagonally) to the given coordinates."""
|
||||||
|
for i in range(max(0, row - 1), min(row + 2, rows)):
|
||||||
|
for j in range(max(0, col - 1), min(col + 2, cols)):
|
||||||
|
if i != row or j != col:
|
||||||
|
yield i, j
|
||||||
|
|
||||||
|
def find_start_of_number(schematic, row, col):
|
||||||
|
"""Finds the start position of the number that includes the given digit."""
|
||||||
|
while col > 0 and schematic[row][col - 1].isdigit():
|
||||||
|
col -= 1
|
||||||
|
return row, col
|
||||||
|
|
||||||
|
def extract_full_number(schematic, start_row, start_col):
|
||||||
|
"""Extracts the full number starting from the given digit coordinates."""
|
||||||
|
start_row, start_col = find_start_of_number(schematic, start_row, start_col)
|
||||||
|
number = ''
|
||||||
|
rows, cols = len(schematic), len(schematic[0])
|
||||||
|
col = start_col
|
||||||
|
|
||||||
|
while col < cols and schematic[start_row][col].isdigit():
|
||||||
|
number += schematic[start_row][col]
|
||||||
|
schematic[start_row] = schematic[start_row][:col] + '.' + schematic[start_row][col + 1:]
|
||||||
|
col += 1
|
||||||
|
|
||||||
|
return int(number) if number else 0
|
||||||
|
|
||||||
|
def find_gears_and_calculate_ratios(schematic):
|
||||||
|
"""Finds gears in the schematic and calculates their gear ratios."""
|
||||||
|
total_ratio_sum = 0
|
||||||
|
rows, cols = len(schematic), len(schematic[0])
|
||||||
|
|
||||||
|
for row in range(rows):
|
||||||
|
for col in range(cols):
|
||||||
|
if schematic[row][col] == '*':
|
||||||
|
part_numbers = []
|
||||||
|
for i, j in get_adjacent_positions(rows, cols, row, col):
|
||||||
|
if schematic[i][j].isdigit():
|
||||||
|
part_number = extract_full_number(schematic, i, j)
|
||||||
|
if part_number not in part_numbers:
|
||||||
|
part_numbers.append(part_number)
|
||||||
|
|
||||||
|
if len(part_numbers) == 2:
|
||||||
|
gear_ratio = part_numbers[0] * part_numbers[1]
|
||||||
|
total_ratio_sum += gear_ratio
|
||||||
|
print(f"Found gear at line {row + 1} with ratio {gear_ratio}")
|
||||||
|
|
||||||
|
return total_ratio_sum
|
||||||
|
|
||||||
|
def run_tests():
|
||||||
|
"""Runs tests on the test file and prints the results."""
|
||||||
|
test_schematic = parse_schematic("../test.txt")
|
||||||
|
test_result = find_gears_and_calculate_ratios(test_schematic)
|
||||||
|
print(f"Test Result: {test_result}")
|
||||||
|
assert test_result == 467835, "Test failed: Expected 467835"
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Main function to process the input file and calculate the sum of gear ratios."""
|
||||||
|
try:
|
||||||
|
run_tests()
|
||||||
|
print("Test passed successfully.")
|
||||||
|
schematic = parse_schematic("../input.txt")
|
||||||
|
total_ratio_sum = find_gears_and_calculate_ratios(schematic)
|
||||||
|
print(f"Total sum of gear ratios: {total_ratio_sum}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error: {e}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
10
Day03/test.txt
Normal file
10
Day03/test.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
467..114..
|
||||||
|
...*......
|
||||||
|
..35..633.
|
||||||
|
......#...
|
||||||
|
617*......
|
||||||
|
.....+.58.
|
||||||
|
..592.....
|
||||||
|
......755.
|
||||||
|
...$.*....
|
||||||
|
.664.598..
|
Loading…
Reference in New Issue
Block a user