From 37c783c4eabcfd9746f3fe2561af940b429b486f Mon Sep 17 00:00:00 2001 From: Jake Pullen Date: Mon, 4 Dec 2023 11:09:36 +0000 Subject: [PATCH] day 3 done, couldnt manage this one so got help JP to come back to understand how this works --- 2023/day_3/input.txt | 141 +++++++++++++++++++++++++++++++++- 2023/day_3/part 1 solution.py | 43 +++++++---- 2023/day_3/part 2 solution.py | 75 +++++++++++++++++- 2023/day_3/puzzle_text.md | 30 +++++++- 4 files changed, 270 insertions(+), 19 deletions(-) diff --git a/2023/day_3/input.txt b/2023/day_3/input.txt index b221142..6a07033 100644 --- a/2023/day_3/input.txt +++ b/2023/day_3/input.txt @@ -1 +1,140 @@ -Paste any inputs into this file. \ No newline at end of file +.679.....662....71............................805..........862.680...................................................................687.... +............*....-..811..........846..855......*.............*..$........230.92@............................=.....................92........ +..........360..........#....664.....=.*...881...677...934.780.......426.*..........8......654.....*959.....539..........21.........*........ +.....................+.........*......379..*.........*.........=.........969........*........*.976..............872....*....../....579...... +.......566......652...809....482.394......492..303.650..../...38....%...............106...385..................#.....793..484.865........... +......*...........*................*..347.......*.........220.....349...691...392*..................18..797.......................+......... +.....11......890........870.......156............733.................../..........921.....................*......*.............203.......... +.............#...238.....*...........................188......294./58........408...............677.......778....104.903............%........ +.411+...828.......*.....706.....249..=.....638...848*...........%...............*948............*.....+......+...........917.......817...346 +................69.................*..310.*....@..........118*......428=..785...................931...217.....934....475...@.145........*... +...844.885.........#66...........587.......74...773...........194......../.........&107................................%...............375.. +......*....826.190.......498........................960............24........212............................398...........474*925........... +...........*...*......../......949.-...213./353.................................@...........853......33*164....+....998...............325... +........640..823....957........&...746..*........*777.............134.......264.......904+..=........................*......./........%..... +....43...............*....457............325..390...........618.........265....*670............=...#165........909..634.......810........... +.....................503.*.....438..904*.............+.........*..563..*............345@......597.........758.*...................*154...... +...941...................612...@........691.....&...336.......837..*....489...............................*...462.689....995...106.......... +......*383..824...................911........*..467.................44....../.........&445...$...765.647..464.............&..........226.... +......................151.....116*........249..................929*.......500..140.........18.......*.........823.....454............*...... +..................968..@..........................118*.....300.................*....762.........541......59...*....35....*..........8....... +.....304.........*.......620..@...573.878................................@...@.273.*......572...............661........527.971*.......259... +....*.............83.....%...983.*......*.32..603.....201..............492.166.....339.....*...978$..............373...........340.....*.... +.761..758...934..................550.148.................................................25......................*.....*236.............624. +........&......*...393..62*.................167/..716........$........910.767.......380............873*812.362.115..537......48............. +.....#........697..*.......159.....................*.........522..............41.....*.........+............*...........812......13......... +....529.............374......../...%.............................143...........*......109.......365..........952...........*....*........... +...........122*...............555.937......674....................../...529.774..371.................#.38.........../.......304.750....715.. +..................873*....819..........165....*..928*345........................*..........77......478..*...440...743...................*... +964...........417.....802..*......230..-....509..................573...........219...675....-.865.......13............25.62*4...980..145.... +....*556........&.............278*.................957.....85#.....*................*............=....#.....442........$.......*............ +.972.....................541...........91*614..485...............680..792...+........541.325..........486......&..653....#.....247..778..... +.......680.................*.980*710............*...494...............*......88...........................825........#.629.............$.... +.399...*......439.........89...........430*....401.....*660....693......407&...........#883....#265......*.....438.......................... +........390....*.....388.........21........861............................................................798.-....#........................ +..417&.......551.102.$..............332............/804.....463*649..188.........992.....754.203....912..........732.490.................... +...................*................*......214.134......................*805.%.....*.....*....*.....#.....*..........*....302.....934..=.... +....835.........622..............8...541...*....*..861....442....874..........458...955...985..172.....497.310........341......$....*...644. +....*......246......909....320.............893.150....*.....*....*........717................................................833.178........ +...622....*....309.+.........*.........681..........414......612..678....*...........336.................+.....715.......................... +...........407.......611.....42...........*159........................................*..97..............355...*..........*954.............. +.......967.....120..=...............................................................519....+.....382............974...................651... +739.......*.....*........155.....539................132*.....702......@....923...............905*.........................971............... +....453...149...717.400............*.........618........295.....#..644.......-...786....................*...287...........$................. +...#..................*.............810........*.908$............................*......+........168...903.....*.....&.......@.............. +.................306...483..750.108..........570...................934......523..736..#..889......*.........557.......913....107.852..241... +...............................*.......................899............*974..%........42............268...........807..............-...@..... +..958.......610............/......................894.........................242.........300...........$............941..150............... +...........#....131.907.....924...529.267............=.....-./.....692*505...*......265%............$...95.....538........#................. +....137...............$...........*...*.........219.....347..268.............753....................12.....351.................955....875... +......*....258.................854..253...15........................506....#.................559........&................&........*......... +.....376...*.....892.466..160............*.......$................*....*..593.............../............682....721...750..10......187...540 +...........866...=..................$...437....994......730....404...703................507...&...........................*....126......@... +....300...............301....@...315.....................*......................572........-...793.......309.518.437..182.310.*.....&....... +...=.....-176.129*........882............................256...............249..*.....+.....................*.....%..+.........71...76..586. +......54..........910............*......734....*..............-.......445.....@.....807.....75.907....477%.................................. +.....*........................247.855....%...647.....@.....558...407....#....................%...*................................447..129.. +......322..235.......821..............*...........767...............*35..........824.338.55....232...814................546*582.......*..... +..............*........./...........509......270.......-33...673................/.......*..............*......-.....................257..... +....793*831....805.871.....701..........503.....$............%.............710......37..../.........897...-....516.....330...371............ +....................*..324*............$............875........293.........*.........@..510..............332..............*..*....2*558..... +.........../......147........373..........%926.........@......-......426...142...............%.......184.........#.....746.................. +.......589..755.......285............221+.................$.........*.................571.....539......*.....60...644.............333....... +258....................*.....499............$.107......510.......257..........583.......@...........408.............................*....... +.....................396......*.../..../..493.........................489*380....*........908.711............772..........561......24..559.. +.............................548.769.630..........504*........893................605.........*.........*..10................*..653.....*.... +........=418..........*576................-..............815.....*.389......481.........*.......859.180....................137..+....275.... +..77...........551.901.....535............683.............../.638............./......671.908.....@......360.....611....136.................. +...&...........*.......722...........................702.............514............................520.$.........*...&.....$315...#...147.. +.......830............*.............#377........39.....%.......384..*.........748..265......271.............885..882...............45...+... +..329.-...........787.311.........................*........115...@..840.........&...*.......*......513..695..........515.................... +....*......666...*..............256*999......897.651.......*............457.........34..&...933...*......*......................156......... +..627.......*....24...........@.........................177...22........*...775........668......685...947.....110................*.......... +.........172..........295*....540.856..76......................+...337.343.=.............................................18....914..449..... +.............239..........163.........*......770....594.........../..............200.....824*513..........146............*..........-....... +..............#................841...199......=............317..........97.*..........-.............165..................................... +...570...../......................*.......122.......179#..*......138........647......376........65..............735......................... +.....@.221..50..350...@....757.....88........*582.........893.....*.....230.................403...$......................865...927....892... +..........*........#...130..*......................232.........709....&...........105......*..............926=.......966.*.......*...$...... +....200...884..............338....*155....822*609....&..84.........935..............#.....481...968.64................+..337.....152...254.. +.......*.........203............70......................%...................../................*....*........................627............ +.......491........*......321.......$...-.......282............................512.282........564.625........746*825..#..........*361........ +...................908...$........335.332.509$....*..../..........216.....-.......*...................617.............896................... +......946...299............657..................255...724..../......*.....830..122..........487.........*.....645.................56.66..... +..984..@...........@....15*.............*858...............150.....91..................498.........654..969.......+............&.....*...... +.........104.870.281.........$.......796.......................................................................853.............316..984..... +.........*.....#......=...288.............86......376.804...515...............405..265*................................%.................... +.336....152..........671............545.....&......*.....*..............549...*........37.....-....313.#......110.....578..414*............. +..............496.....................*...........536..20........471...*.......397..........437.....*..996...&....18...........200.......... +.................=..........308.....147...319.12................*.....465..............$992........71..............@........................ +............712.......*123.=..............#.....*629.......%..............................................#..........153..945...........36.. +.....+340.....-....850................922...%............812..259*577...#452...-........620.567-.784....985.934.427...@..*.........280...... +216............................394......*..589.........-.....................191....622./..........*........*.............124........*...... +......764......................*......663...............165...318.....................*....200.83...896..856................................ +.....*.......705........751..46...............................-....698....132.......-..81...........................264..................... +...538.......*......170....*..............808........166..........+......$........130........428............852.......*.................902. +..............803...........938..51....66.*...514.......*430..................................*......@..............=............/469....... +.........26@.........90...................473..../..273..............13......656.......242$..829......682...........594...968............... +...180............+.........872..131....................176...781...-.......*.....294.............990.............#..........=.@...../...... +....@..344.........184..........*.............................*.........42..153..#.................*.....577......32...........303..334..... +........&..620..................821....172....32.....470....698..........%....................397..301.......................*..........569. +.../........./.........325.@.............*...*.........*........$.....*....+......124...200......*.......827....*861........857.876......... +..808.................*.....581..............644...3..190.607.175..76.712..724.......$........16.358......*...31.......270.........*915..... +...............894...453...............845..........#.......*.....................#......................793..............*...914*.......... +..................&......................+...............99.596..597*813...958.831......261*95.....922..........&.......537.......302.414... +...931*.................................................+..........................394......................65.679..........%.........*..... +.......839....644......./262......461...424.....682........................954.............43*.............*.......778..*....464......822... +.............#.......................=..*...........706....289*.....459......*..........-.....234........11..$.......@..373................. +.............../620...536+....100*......907.....159..+.........998.*..........832.....806...........695.....516....................126...... +..................................268.............*....893.........12.86.................................................*.................. +......+....355.........998..630..............$.....815...*..............*.....168...554...........735............+......861....490.......... +.....176...*....513....%....*.................120......568....*314.404+.325..........*..662...855.....495.........467..........*............ +..........132......*......677..708.................424.....715.....................520..*..................................36.410........... +..419...........454..............#...730...401&....*.....#............696..............267.=802...................761......*................ +............394.........................*........650...637....695......*.....................................586....-...735..154.....423*344 +........458..%.....*...728..+..........488.435......................241........795.552.....9.&...............................#.............. +...........*....86.981....*..633.........../................762.700.....70*.........../....*.837......-.........20..................%....... +.519...+.652...*........614.......+....................924....*............663..591......192.....952.25.........*....600....#.*239..381..... +...*.366.......845.512......@...200...-....280............*...207............................297...*......828...249.*.....408............... +.189......5........*.....353........245.......#...153..680..............295..50...............+....296.../...........906.............+...... +......741*.........466....................710....=.........561*........*..............831............................................909.... +...........+.................421............*..........+.......661..103...............................922..752..................431......... +181......218.707....*....273....*..130.$273.980.........656..............517..712..578.......&.524.....@....*.....996..............*....#709 +.................442.432..*....64.*......................................../..*....#.......534....*........498.............727.....434...... +233*559....................869.....316../..........&.....502..+...884.731.....503................239...499.....94..........*................ +........352.......231...................340.....177..584.....922.....*....57...........................*.........*349...222...212&...433.520 +...........-.........*981..479..637.....................*969.....*.........=..$.........601...=826...312.................................... +..............205.................*../...@.....213.............41.64..........80....889*.............................@....76.=702........... +....353.......*..................114..97.867...*.....542.........................................721...395............816..............*112. +....*.......367.*............478.............42..../..........947.................................*......@......956...........+...883....... +.....870........460............*...................26...........*...........960......212...479....422...........*.......780..595..*......... +....................978*368....921..760=..................416...665..$.........$.381*.......*............933.744....=..*...........792...... +....52..........300..........................................*........293.162%........................@...*........259..283................. +......%..........%........119.......575.......#..%......./.748......................945...212&.....595.....248.286...........839..106/...... +..940........$...........%............%....880....539.659.............*.........353*.....................=.....*................*.......188. +.....*....254...............794..550...............................701.797............500#................34..475...507..........763........ +..649............58..691&.....%............................................386..............@.$44.101.....................997..*............ +.........255............................376....732....-..............869......$...........473........*376....220.........@.....721...443.... +...........@.913.....168....=909..431......=......@..976.......+.......*..........155............................620.......250......@....... +......806.....*....................*...........@................45.....475...724..*......&45.........+202..-576.....*.........*............. +...............383...........................372..................................474...................................432.471......729.... \ No newline at end of file diff --git a/2023/day_3/part 1 solution.py b/2023/day_3/part 1 solution.py index 3c0ecf6..c825b8e 100644 --- a/2023/day_3/part 1 solution.py +++ b/2023/day_3/part 1 solution.py @@ -1,5 +1,5 @@ # with open(r'advent_of_code\2023\day_3\input.txt', 'r') as file: -# input = file.read() +# input = file.readlines() # print(input) @@ -14,19 +14,36 @@ test_input = '''467..114.. ...$.*.... .664.598..''' +## remove when not test input = test_input +## remove when not test -# find every special character in the input -# at the special character, look at every space around it (including diagonals) -# if there is a number, add it to the list of numbers -# at the end, add up all the numbers in the list +def get_directions(x_offset, y_offset): + for y in range(-1, 2): + for x in range(-1, 2): + yield x + x_offset, y + y_offset -# find every special character in the input -special_characters = ['$', '*', '+', '#'] -special_character_positions = [] -for row_index, row in enumerate(input.split('\n')): - for column_index, character in enumerate(row): - if character in special_characters: - special_character_positions.append((row_index, column_index)) +valid_map = [[0 for _ in range(len(input[0]))] for _ in range(len(input))] -print(special_character_positions) +for i, row in enumerate(input[1:-1]): + for j, char in enumerate(row[1:-1]): + if not (char == "." or char.isnumeric()): + for x, y in get_directions(j + 1, i + 1): + valid_map[y][x] = 1 + +current_number = '0' +num_valid = False +sm = 0 + +for i, row in enumerate(input): + for j, char in enumerate(row): + if char.isnumeric(): + num_valid = num_valid or valid_map[i][j] + current_number += char + else: + if num_valid: + sm += int(current_number) + num_valid = False + current_number = '0' + +print(sm) \ No newline at end of file diff --git a/2023/day_3/part 2 solution.py b/2023/day_3/part 2 solution.py index 6f21f2f..c23ebec 100644 --- a/2023/day_3/part 2 solution.py +++ b/2023/day_3/part 2 solution.py @@ -1,6 +1,73 @@ -import os - with open(r'advent_of_code\2023\day_3\input.txt', 'r') as file: - input = file.read() + input = file.readlines() -print(input) +# print(input) + +test_input = '''467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598..''' + +## remove when not test +## input = test_input +## remove when not test + +def get_directions(x_offset, y_offset): + for y in range(-1, 2): + for x in range(-1, 2): + yield x + x_offset, y + y_offset + +valid_map = [[0 for _ in range(len(input[0]))] for _ in range(len(input))] + +for i, row in enumerate(input[1:-1]): + for j, char in enumerate(row[1:-1]): + if not (char == "." or char.isnumeric()): + for x, y in get_directions(j + 1, i + 1): + valid_map[y][x] = 1 + +current_number = '0' +num_valid = False +sm = 0 + +for i, row in enumerate(input): + for j, char in enumerate(row): + if char.isnumeric(): + num_valid = num_valid or valid_map[i][j] + current_number += char + else: + if num_valid: + sm += int(current_number) + num_valid = False + current_number = '0' + +print(sm) + + +# Part 2 +def h_flood(x: int, y: int, m: int) -> str: + if not input[y][x].isnumeric(): + return "" + flood = h_flood(x + m, y, m) + return f'{flood if m < 0 else ""}{input[y][x]}{flood if m > 0 else ""}' + +def get_surrounding(x: int, y: int) -> list: + return list(map(int, filter(lambda _: _, + {h_flood(_x, _y, -1)[:-1] + h_flood(_x, _y, 1) for _x, _y in get_directions(x, y) if + input[_y][_x] != '*'}))) + + +total = 0 + +for i, row in enumerate(input): + for j, char in enumerate(row): + if char == '*': + if len(nums := get_surrounding(j, i)) == 2: + total += nums[0] * nums[1] + +print(total) \ No newline at end of file diff --git a/2023/day_3/puzzle_text.md b/2023/day_3/puzzle_text.md index 30d56df..78f1eab 100644 --- a/2023/day_3/puzzle_text.md +++ b/2023/day_3/puzzle_text.md @@ -27,4 +27,32 @@ Here is an example engine schematic: ``` 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? \ No newline at end of file +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 --- +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? \ No newline at end of file