From 774e0ddf5a96f9957298fdd75db8719896e21b62 Mon Sep 17 00:00:00 2001 From: vlabayen Date: Sun, 3 Dec 2023 09:42:22 +0100 Subject: [PATCH] 2023 d3 --- vLabayen/2023/d3/d3.py | 98 ++++++++++++++++++++++++ vLabayen/2023/d3/example.txt | 10 +++ vLabayen/2023/d3/input.txt | 140 +++++++++++++++++++++++++++++++++++ 3 files changed, 248 insertions(+) create mode 100644 vLabayen/2023/d3/d3.py create mode 100644 vLabayen/2023/d3/example.txt create mode 100644 vLabayen/2023/d3/input.txt diff --git a/vLabayen/2023/d3/d3.py b/vLabayen/2023/d3/d3.py new file mode 100644 index 0000000..0f9e5be --- /dev/null +++ b/vLabayen/2023/d3/d3.py @@ -0,0 +1,98 @@ +import logging +from typing import Tuple, List, Dict, Set, Iterable +from attrs import define, field +import re +from itertools import product + +Coordinate = Tuple[int, int] + +def get_neighbours(pos: Coordinate) -> Iterable[Coordinate]: + x, y = pos + for x_inc, y_inc in product([-1, 0, 1], [-1, 0, 1]): + if x_inc == y_inc == 0: continue + yield (x + x_inc, y + y_inc) + +@define +class Simbol: + position: Coordinate + character: str + +@define +class Number: + positions: List[Coordinate] + number: int + + def __hash__(self): return id(self) + +@define +class Schematic: + simbols: List[Simbol] = field(factory=list) + numbers: List[Number] = field(factory=list) + + +find_numbers_rgx = re.compile(r'([0-9]+)') +find_simbols_rgx = re.compile(r'([^0-9\.])') +def read_file(file: str) -> Schematic: + schematic = Schematic() + + with open(file, 'r') as f: + for y, line in enumerate(l.strip() for l in f): + for number in find_numbers_rgx.finditer(line): + schematic.numbers.append(Number( + positions = [(x, y) for x in range(*number.span())], + number = int(number.group()) + )) + + for simbol in find_simbols_rgx.finditer(line): + schematic.simbols.append(Simbol( + position = (simbol.start(), y), + character = simbol.group() + )) + + return schematic + + +def p1(args): + schematic = read_file(args.file) + numbers: Dict[Coordinate, Number] = { + (x, y): number for number in schematic.numbers for (x, y) in number.positions + } + + part_numbers: Set[Number] = set() + for simbol in schematic.simbols: + for position in get_neighbours(simbol.position): + n = numbers.get(position, None) + if n is not None: part_numbers.add(n) + + print(sum(n.number for n in part_numbers)) + +def p2(args): + schematic = read_file(args.file) + numbers: Dict[Coordinate, Number] = { + (x, y): number for number in schematic.numbers for (x, y) in number.positions + } + + gear_ratios: List[int] = [] + for gear in (simbol for simbol in schematic.simbols if simbol.character == '*'): + part_numbers: Set[Number] = set() + for position in get_neighbours(gear.position): + n = numbers.get(position, None) + if n is not None: part_numbers.add(n) + + if len(part_numbers) == 2: + n1, n2 = part_numbers + gear_ratios.append(n1.number * n2.number) + + print(sum(gear_ratios)) + +if __name__ == '__main__': + import argparse + parser = argparse.ArgumentParser() + parser.add_argument('file', type=str, default='input.txt') + parser.add_argument('-v', '--verbose', type=str, choices={'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'}, default='WARNING') + args = parser.parse_args() + + logging.basicConfig(level=args.verbose) + + p1(args) + p2(args) diff --git a/vLabayen/2023/d3/example.txt b/vLabayen/2023/d3/example.txt new file mode 100644 index 0000000..b20187f --- /dev/null +++ b/vLabayen/2023/d3/example.txt @@ -0,0 +1,10 @@ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. diff --git a/vLabayen/2023/d3/input.txt b/vLabayen/2023/d3/input.txt new file mode 100644 index 0000000..73e32db --- /dev/null +++ b/vLabayen/2023/d3/input.txt @@ -0,0 +1,140 @@ +.........874.772...........787..........556.....292......141................910............54............................................... +.......*..*.......314............308.......*....*..............156.759.....*................*.......408*954.84..55.......................515 +......927.49...........734...............115...738..=....723..........*...599.......+........573.....................324..../508............ +........................*.....222..................298....+........313.............504...../.....375.................................114.... +.962...262............988..........*170.....................543...........61............827..244.&..................610......310....#....... +...*..........129...............243...................809....&...+.........*...742............%....*96..100.....766...*..308....*......37... +..2...295....../........*336..*......#..185...........*.........502......301...*.......................*...........-.770..-...599........... +.......-.............343......750..661....%........+..323.....1..............480.........+..............198.......................533.../764 +..................................................799.....832.......640.................413...392............597.................=.......... +......$795...678....954.....*96...........671.%..........*.......23....@.536..................*...450..........*.....427..............413... +..314...........*.....+.827.......757.550*.....475.148.614.........@......$.......*429.....849....*...%584....706....*..........248...*..... +.......246.......830....*..........................*.......52..................663...............55...............606........%.......72..... +......&.....378.........342....10.849............394.....&...*.....697*............................./629...700........=.......217........... +........208*......*783.........*...*.......496.........108...757..............564..............................904....448................... +..../..........716............428..28..879*..........................*506.823....*690......53...................*.............291......357.. +..920.@172.........869..................................185.......809......../.............*......*821.......296..............*.......*..... +...........965........*...................669.793........................*................238..609......465.........504.....574..970=.575... +...................520.....................*.....&......................2.44..*......*..............@...*.......254*.....17................. +............574../.......*..............894...........939....236...875........92..873....555.825*.543..344..483......677*......123.......... +...257..../.*....301.....83......*929........+.......&.............*...=...#..............#...................................*............. +.......218...648...../.....................533.683*....419........76.461.268......915...............523.....482.............788........845.. +..................846......405.807.................693....%...................668*....193$....*......*.....@.......368.67..........703*..... +................&.............*.........752.988......................967*..................652.746.772...6........*......................... +....638.....769.829....*...97......536.#..........................&......786......@......................*......440......................... +....*........*..........55...*......*...............598*387.......744..........141....254*......157.....977..32...................606.392... +...682........268............134...187.........25.........................................561.....*............*.................*......*... +.......718........773............................*.....781...380......856....445......601.......71..........884........949*13..888.......277 +.......*......487*.......$....$........8..@613....868.....*.....&....*............899*....*994........116............................806.... +....807.............441..848.692.-13...*...................723........646.....525...........................*............289...........*.957 +........-......891.....@.............185........672............................*.....626..937............838............@...........698..... +....634..418.....@.......75.430................*.......588-....815..56*685....714...@....*........+................385*..................... +620..*.......720....580........*521.772...222..763.888...........*........../..........537.818..534....................370..942.384...991... +.....462.......*...&.......118......*....................915.....797......255.....734........*.................*.............=.....-....*... +...............529..........+..701#..56.......@447..@......%..........-..............=.....197..../..........37....223.................186.. +............#......................................702.846...........791..85..698..............755......458........#........847............. +.........993......916...@669.....493.........&...........*.....*..........*..-...........921...........$...............100...*..717.....489. +..................-................*.456..634.........202....=.815.......769................*...................300.....#..649...*......*... +....................762................*........211.......164.........27............817.286.....@493..............*............@..422.837... +......470.............*...............160...........560.......962.....*.....431.909..*..*...712.........852....838.........64.614........... +........*........404...406.=561....98........@........-.195.....*...320..........*..922.777..*.............%.&.........530.................. +.71.....680.-79.....*..............*..859....625...............300..............465....................515...795...111.....646@............. +...................13...545.....386....*...............................865..........537.815........731.*.............*..........=.572*...... +...........173.......................324...............................*...../.......*.....*157......*.480........175...662$..471.....136... +.....443*6...*...........*165....481............211/...373*297........470.....982..402............895........54............................. +...........11...&.....848...........@..........................@769.......................120...................555*........%........734.... +..............827..........723...........178.............308........659............#492......*.............10.......80.......966.....*...... +.....438-........................$.........................$./......*....%196..............299...393......*..............78.........629..... +.......................743....200...............243..702......447....775.........328.......................274.......360*.......459......... +....723..........514..../...........479......76*......*.........../......&....../......861...........$807......................*......+..... +.......*.....987................226....*.........#..867..........481......165......415*...................212...%649.....923-...685....346.. +...+....914...*...........336.....@.278..622....204......269.976........................487.....%...727-.*.................................. +..937.........38..-......$.................-........992.......*................628..740*.....%.445........360..............429.......917.... +..........$.......592........481*285.....=.................126.............................269.......702........................823.#....... +..........593..52........546.............282..*226..............108..412.............=............#..................869....@....&.......... +628............*..768......=.655...................*............*.....*...../..909....855....$....400.......238.........*11..545............ +.............577....................../......147....945.31......979.846....403../.............742............*.......=.............#........ +505+................485.............411......-..........-...........................536...-.........144......885....42...218.147...329...... +......................&.849*714..........94*...856..........89..457.......*.....%...&...180........*......@..............*......*........... +....................-.......................68........651../...........264...&..252...............359...256..669*.......766..668...193...... +.........992....743.860..174....&303............883.....*.....732..........433..........609...125................308.................*...... +..592....#...64..*..........*........656..505..*......546.....&........422.........#............*.613.382..123............%....721...572.... +...*.........*..........839.364.........*....*.549..............336...*...........129.......632....*....*.....#..647..122..25............... +.612...-..%............$..............628..489......................-.850..633.........=......*...972.660........*....*..................... +.....410..765.........................................../102.652..529.....*.............533..788............449&..8..875...846..392..649.... +....................&.......581.....352............107+..................251.....130........................................./.......$...... +.....807.........722...........*....*..........#...............+.116...............+..122.................184*491..&142..................... +.....&....755............894..227.315.........648..968......515....&.................*.....+..62............................................ +............*.............-...........523............*....-...........165......434...106.427....#............241...........461......451..... +...#246...905.297..#668......=.....=....$...........712..778..968........*........*...............592........*.......703#..............*.... +..............*............837....396........898..............+.....232.110....785...............*.........644...803...................178.. +.......&190....69...*665....................*...................161.&.................618.../.........660..........*............850......... +.............$.............896.896.........884.............287...#........568................588.........@.........236....369$.....&........ +.....@........464.........$....*............................*......958....*.......816....................................................... +..385.................877............160........263...&763..634...-........556.....*....658......953.......276........634+..&...........861. +......................*.....914.208.*..............*................472....................@........*937.#....*745..........841..910...-.... +.......851........259..339.....*.....756.........285................*.........-................66+.......111........................@....... +.409....@........*.........................474.........+...197......180.@.....567.......................................850................. +....*......972.739.....*.....831*.............=......489.................611............*246.....................27.......*..631#......+.... +.....314...*........334.968......739......505...............844.......................................732..............322...........33..... +............49...&..........*...............*........791..........152.....................247....166.............243.........826............ +529&..544........275.....140.322..........73......+......435...................942...273....*.......#........749....*.......*............... +........*.......................................665.....*..................562....@........114.170-............/..92.........443..........51 +......534.............212..........385................306.....863.....357....&........717...............570#................................ +..............783.......*...........*..........................@...@.*............593*....*249....*338.............804..#...496............. +...=..........*......373....../570.65................../...836.....1..685....*39.......584................938........&.642....*..865..40#... +.715...909..142...........................136....892..192...@.............121...............629..........$.................651.............. +................147........-....758..........=...*.............+...89.........678...815.997....*.....................990.................... +...........*90.*....777.....868...*.............909.........361.....&..354....*...........+..200.....438.......62...*........521............ +...............840...&..144.....861.....................479.............*...+...........................&.....*.....682......#........950... +....456.237................*................................146.......927..917..858....601..................834........................$.... +.......*.....794...661.....618................369...#.......&....&................*.....*.....753.................381/.....*397..........524 +.415................*.............490+.......*......533...........766..=.......657.......511...=........................766................. +...............539@.50..164...............583...574......409............126........34...................%652........44........%......886.... +...907...................*..................................*346.............359.....*.........&.887...............*........115.837.=....... +......*...............3.666.............................736.........208.226..-......616.....250.......859..628....829.............*....+600. +......153..262.........................947......260......*.........*......*............................*...*..../...............93.../...... +..........*..........421.................$.........%..500..107......402.264...407.....703..49..........54..424..372..684...........147.249.. +......363..942...137*.....197..............*83............*...................*......*....*...158.......................-....73........*.... +.........+...............*..........116..32............*...900..............360....479....54.........435*192.@850...........*...........325. +..390*..............=..431...518......*......436.......892.......787.517................%.......18.....................625.95....582........ +......600...-176..415.......=.........780.......*...........*702..*..*.....699..........914.........186.......49*897..............*..558.... +......................312........................40......481.........872.#....$./198..........764...+............................352........ +.............800.........*.248.............571..................14........845............627.....*.......*...............336.664............ +.........910.......395.506....*.........@..&.............567...*..............573..913....*......334..*..759......29.%.....&....+........... +..328.......*399.....*...........180...936....97$..376...*......251.....#447...............304.......450.....226.=...408..............*..... +......479............259........*...................*....78.........848......568......305..................=.*.............917..542.667..... +......#......332.............785.........422......624.63...........@.....896.@.........+..=770...530.....630..641.............*.........292. +............*..............................-.@..........*...$.............*......&..%..........%............................832..534...#.... +.....560...400......................246*......411.....377...645.........62......620..465....146................#.986..185........*.......... +......*.........761*......323@..........466...........................................................-111..382....*.....*.......408...164.. +.......247.....................575*..........................836.496*380..125...321...............841...........648.....976............@.... +.254.......74-.....+.......926.......-....844*.........224................*......*......562..811../...226................................... +...*..............162.*261.....287....758.....638........+.....479........834...80............=...........................188........287.... +...734....396...................*.............................*.....406.*.............244#..............&....320.............*.*165.$....... +..........*..........31......221.......$384............*115.308.87..../..306....88#....................230...@............768............... +........694......441*............53..............35.814.........-.......................77......379.....................&..........-........ +..................................*............-.+.......527......975...........594*......*........*857.#37..........302...........947..716. +.......104*561......978.226....925...........244.....107.........*..................336...749.............................691...........*... +..654..........561+...*....&.......54....924........=.........385..238........661*...............49......=.......456.859+...$.........479... +...*......392.........587....-....*.........*.965.....863.........&.....801.......19..$......*.....*529.881..670*.................790....... +.615.....-........570.....11.547.166.....347...*..190*.....................=...........302...236...........................&................ +.....................*686.....................43..........79...........475......396.......................................610..#856......... +..................+...............$......................*...592.........*.....*.........804.......@30......629...23........................ +...............202..954..%..958..894....503..579........904..*.....573..603.....450......*...734.........56....&..&.....415......408.511&... +.........750.........*..454....*......%..../..$..747#.......493......=......455.......252...*.....999.....................*........*........ +...239...*........180.........594......280............412*.............255.....*..........17..338...........950..228.......840..809......... +........572.............326......................518.........................700.............*........263......*....@.675*............22.... +...182....................*......................*......85...731...@.........................130.........*........*.......408....727@.+..... +...*....110....$899..+...755...568.=309........192.......*...*...259.847...557*42.......*........304/.....608....351........................ +...823.+.............765......../........-508.........516...412.......*..............704...............................275.........210...... +...........356........................#..........439......*........564....372*...846.............=........+62....-........*..298......*964.. +.......%......*...308.....-4.........420......./..*....905.551..........$..........#.452*........417.195..........276...956.....*...$....... +....996.......413.....569.....#.810..........889..328..........*.........595.............359..........+..%.......................35.416..... +551......$...............*..357....*....627.............94..784.719..........697*76..........135..735....690.........944*177................ +.......711............770...........664...=.......#...../...............730*..........58....*.......*...........182............848.......... +...*........800................#..............#....929......=843............216.........*...412....473...735....*.....610*.....*...505...... +348.5.......*......317......920................224...............503..-158.............463..............*.......865.......674.750.....@..... +.......367.723......*...................42.........636............+............$599..........948.......882..............................173. +.........&........873........=...........*...........*....292..-..............................*......................597*923................ +..............................871.......497..........159........452.................900...116..450...878...........................302...574