diff --git a/2023/day3/day3/.gitignore b/2023/day3/day3/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/2023/day3/day3/.gitignore @@ -0,0 +1 @@ +/target diff --git a/2023/day3/day3/Cargo.lock b/2023/day3/day3/Cargo.lock new file mode 100644 index 0000000..c149834 --- /dev/null +++ b/2023/day3/day3/Cargo.lock @@ -0,0 +1,54 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + +[[package]] +name = "day3" +version = "0.1.0" +dependencies = [ + "regex", +] + +[[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + +[[package]] +name = "regex" +version = "1.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" diff --git a/2023/day3/day3/Cargo.toml b/2023/day3/day3/Cargo.toml new file mode 100644 index 0000000..3c9ca88 --- /dev/null +++ b/2023/day3/day3/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day3" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +regex = "1.10.2" diff --git a/2023/day3/day3/input.txt b/2023/day3/day3/input.txt new file mode 100644 index 0000000..cab8523 --- /dev/null +++ b/2023/day3/day3/input.txt @@ -0,0 +1,141 @@ +....................................18..........889.270.....748.280...997.................617..............622........763............476.... +...529......434.....191..489...717...@.....................&....................939*7.....*....................606............760....*...... +....*...473....*221................$........182......812........493.84....793..........794.......589..407..41...*.....................68.... +..380.................553..559..105.....749.............*..........*.........*.............-968....@.....*..*....908..603................... +.........................................#...600.........441..#833...490....366.........................225..488........&...456.........224. +........288=......896*469..........831..........*....................*..............#...@.146.......*.............790#......%...194....@.... +..730.........................250+......./......359....=.462..............138...#.49..713.-........342...604*676.........=.....*.........418 +....*....907.......526................274...........346.....*.876..........-..725.............376......................541...600.....366*... +.....747...*..........*......56$...10........422........-.86.....*.......................728./............*................................. +.........486.........349............*..........=......696....$.............850&......906*......831.....539.195.........202.................. +..............592.............................................592....+.847.......676......365..=......................*........469.%........ +.542............*...............385....60.................329......657..@......=..*....&...........355.........=....52.........*....352..... +.............994......937...................45.....48........*..............835...791..654....................75.............523............ +...................&..............269.486.....+...*......&....954.........#........................246&...&..............203......693....... +.....262..........920...248.454......*...........289...822.........530...569..............................256.......929....*......*......... +........%............../.........................................../..............+..57.@............616=...........*...462...198.757....... +...............65........=.........764*.....$581.727........62.......4.....796..280......444.807...............174@.20................512... +.........877....*...687..187.522.......853..........*321.....*........*345...........513.....=.......849..........................@....*.... +.........*...635......*........=..............................380...........77...44*..*................#......*......484.....65.886......... +.....153.........@.&.....320........96..619....510......221.............139..*........957..977......20.....232.20......&.....*.............. +......*...397*.610..479...*....=....%.....*.......*.............839.......*................*............*.................144........#554... +..510...................927.....384........714...286....................799.....733.........832......815.187.....*....470................... +........892..................................................645.427.............%......562...................133.442....*906............... +.........=..........803............577.......%.....447..185.........*957.637*97..............132...............................83.258@..$... +...........=55......+....+...435.......930...143.............475................./..............#......................=..675...%......709.. +777....................841..*...........*......................*........*.........859...../...............640%..955..903.*.................. +..............639/...........803.829.....877........#.........716....873....387.........857..837.................*.......390................ +.................................*...............994......................%....*.............&.......749.........718.174.....138..697...463. +.....................697...873#.435......*141...........914*656....@......414..981.....541.......%....*...+.#.........*......&....+......... +.....................*................544.......524.............446.......................+.397..486....603.463......497.................... +.........161........412....273..218..............*..80............................708.......=....................899........+......-........ +............*..435*.........-..$.........666..401.....*897.214..472..499............*...955......................@...867....607.491..43@.... +666%.851..653......44...............152....................*....@.....*..........294.....*...313...237....580............................... +............................989....*.....954.....69.....488../.......90.495*..........697....*.......*.....*..686.......=.......594*981..... +.....104..#...................=.....773...$../.....*396.....959.............272...........433........889.821.....*759.469................... +.......*..865.318.@812........................267...............969.............................758........................913........646... +......132......*.........*699......+...............+.......126......468.....651............424........270.765.....53...965.*...19........... +..............725.....449.......337..187........862.............553*........*....=282.28....*........*....*........*.....%.779..&....218*... +.....744...36.....203.......452.......%..846........&......................................811..501..342...891.....776...................395 +.................$.....567&...@..689.......%.......619..392.........847#..#.....236*833............*.........................408............ +............./.....732............*........................*..............568................375..561..-270..111....=..565.................. +...........801............221....336...493....263..81*.....981...........................449*....................809......*239...........686 +...................+.........*87......*..........#....510...........20*684.../..387.....................................*...........132..... +.......674......&..364...........734.141.854*290...........................572.*......878...929.141.#.......526...919.646..*..........*..... +..............980...............*...............................500...229......876....*..............220.....*.....*........809......522.... +394....374......................194......660.....172..............&.....*...........774...................367......452..952................. +.......*...................918/............*.....*.......220........776.542..............843..857....560.................................... +........728.......@951.668.........651......589.857.........../.....*.........354....509.......@........*.................+.....151......... +......-......262.........#.....*95...................878./...563.642.............#.......................586........638..933....=........... +504.454.......*............540..........524..........*...531...........942...................632@.830.........302..-................%....... +.........498..357..........*....&..........%.......340.........925.......*....623.707.479............*36....%........+.....659*149.401.117.. +34..................608.....156.747...602@...............................748..*....*..$............*.........565.....93................*.... +........426.................................60.938...........................615.328.....818....143.118..................*.@...........916.. +...............547..............25*32.......*..*.......64..-813..........405.........802...*............188............246.236.............. +652................*426...................74.....822...*.................*...533....*....764..............*....................%............ +....920.64......437......598....738..#..............+..44...625...*....879......*..879.......668.#.......509...385..123....677..553.62...... +....*....+.783..........*..........*.552...534...............*.....312........=.........@...-....819............#..-..........*............. +....779.......&....21..582......264.........*.............564...............561........811...........................943....68..$........... +.....................&.....................915.472*918.................848.......943.........803....................*..........386.......... +...........................62........668..........................*801.*.........*...............878.......212....613....................... +.......208.......302..313*...*........*...........&.717........237.....11.........551.569.................*...............717............... +...742*......886*.........15.402....806...84...527.....*....=................650......*.....56.....401...276......964......-.....@.......... +..........................................*.........411...277.722.337.........*.....915.252*........*................+..........356....553.. +.......*...2.........406...............922.....=605............*............403......................316..........84.......%..........=..... +.......385.+....%893........200............150...........420.548.......+111...........70............................*675.895................ +...........................+.......198-...@.........532...........23..........349#....*......#...............................920.........844 +.....#.....387/.............................31..638..*....580.....#...........................397..720..........161...+.......*..628....*... +......816..............669....36..#393..848..........884.+...........&676.....876*341...565%.......*..........*....#...914...356..-..210.... +...............646........&..$.........*...................542....................................370......686.567.......................... +............../....94................414......18.............................861....*.....878..........24@.............875=.693*320..864.989 +...................*.........665..............*...595..........201......738..+...784..187....*237..../..............................*....... +......236-.......126.....641*..../............486.*.....638.............................*.%.......242....*153....+.........63.837.723.41.... +...........35....................114...............661....-....534.........626*......796..28..........579......367..78.929...*.............. +......108.........271*144.........................................*............5.....................................*.*.......669.533..704. +.........*635................#..@947.......290.....544.............707...................465..632.....771...181........471.380*......*...... +.....114..........89$.....699...........=....*........=.....416...........................&.....$......$..........365...............131..... +.......-.............................410....64......*.........*.........&....684.....15.....317...340*..............*....................... +.........26.609........152........................94.498.....26......671.......*.221*..........*......718...824.....56...........915........ +..218..............707*.......786.....839................790.....192.........181......836....287..............%.......................-..... +......937.722...............%.........*...378$...........*.........@.449..............%............226...........&...........428.......341.. +.795....*...&..185..405.....60.121....159.......275...996............*....981.....710........363...%...528....-.888....*641...*....880...... +...-..806.......*.....*......................@.....#......545..467...289..*.........*....681..........*.....86......326.......664.&......... +.............221.....142...860+....933..653..291......52&../..*............300.%...811...*.................................................. +......................................-....*.....861.........149...............673......762........./284..588..................%.....316.... +..373......290..............................665.....*................&.............&552.....51.............=.......105.......162.......*.... +...-........*.....388........%...772....................=...57....163..932..41...............*...693.116*......#.....*..............434..... +......*489...248..*........395...=.....................676..#..........*...................835...........570.199..678....................... +780...............713................952...479...379..................83.554.........919.......486..980...............984.$468.....720...... +..........66..-.....................*.......*....*...........................380%.....$.........*..........899*..118..............*......... +............*..694........382....793.........668.92.....747.......49.....&..........#.....284...462....303........*....518......50.....798.. +...*146..642.......@880...*............79=.............*.....93....&...885...785..780.......*..........=...-.....663..................*..... +533...........996.........352..................619%...200......+......................142....408....=....634............379*491.588@..183... +........$127........970...............902..........................943.649............$..........909.........619............................ +................-..*....640............*.......-459..#.....446......&.....*.934.....#....313............/....*........509...473..901....132. +..........*..415..805..*.....786.....12...............863...*...........674....$...296.....&...474......281...326.......&....*......$....... +........909...........136.......@.........645................961................................/...250...............$................*554. +...*.........589....................253..../...166.....&429.........96......568*684.%186...............*958........678..............466..... +.759............*96..........658.......*......*...............105...-....................428.............................=...73............. +......54*...................+......168.592..536.=................*8...671........753+.......*.................74........564....*............ +.304.....728..764................=...*...........804.575....266........*.....................94......27.316....=.-644.*.......209...485..... +.....997.......*.........#....792...855.................#......*169...496./615........683.......225.*..................474............$.395. +............573....206.477...................*158..........702....................298../......*...=..100....................714.........*... +448...............*..................769...27........691..$.......178....675......*........980.28...............*727...%.....@...201.545.... +............223...561..........261.....*.............*................+...........610.250....................496......408..........*........ +....@775.....*............&.....@....195..........303..+.............141..865..............629..+....659...................855*....958...... +...........407........92...22................493......605..................@..........477...*....160...................743.....163.......... +......................../......*......602@......=..+.......*....668$..245................*...731..........69.....213.....*........./....%... +................213*........903.75..........64......945...221.........+.......740........739..........393*...301*......562...../...53...4... +...164&.....145.....580.......................*................425........900......828*............................*........536............. +...............*.....................91....185..................../....................85.................414...890.204..................... +....772............422..*.351...218..................584................263...849...............*...639*.....*.............................. +.......*307...897...*..28.*........*.*49.......300....=../72...........%......*.....544......974..@.....214...55.......347.............914.. +901............*..67......840...343.........................................447....*...............400...................*.................. +.......684..498.........*......................544...347..+........493.............612..........59.........741..........593.........416*.... +..........*........369.577........690...770....*......*..983.*160....#.................623*248.#......437..*........258.................275. +....+...603....+...*............./......*...913....609.................823........................62.%....83...........&.................... +....538.....356..=..117......-.........556................................*..............920.......*...............746...................... +................483.......197...................&....................231...210......92....*.....982..&......199....$........848.$.....$..... +...370..413.........$.............*............556........325...902@....$..........@...182..........933................*.....&...829.402.... +......*..%...........524.131..134..163.....185...............*...............................756........282&.774......822................... +....340....373...*...............*...........*....649.20....289....799..549.939.....120..........61............*..345.......778...550.267... +............&...699......&319..170...492....61.....*.......................*.....+.................*.........345.....@........*...+......... +534.............................................144..849.......174......*........219.....$......140...476..................970.............. +.../....681............444.....819$...................*..=305../.....413.95...............589.................229...235................644.. +...........-.....28.......*403................&....548.............&...........17..........................33..........+........962......... +373..........500....%429.....................291........*....881.296.....163*....*..............796...585....&.............802..*.......138. +................*..................983&................809.....*.............417.999....$.......*...............633.........*...322....*.... +......907..$921.............665...........731...............903.........................638..662..............-..........495.........130.9.. +.........*......168............&..353.....-..........639.........#...........349......................660....914...570...................... +...%......695..*....679...........=..........$...958.+.....494..369..........*...........................*44........*.........964......548.. +...826.........268..*.....675..............136....-...................650.964....728.823.403..................842.983..........*......*..... +...................328................................519.....210.......................*....143.....*46....../.................241..55..... +.......855$...199.......526.....849...764*294..........#.....*.....280..........261.885.........#.434...................610................. +...............=...............+...................498.......936......*..831.......*.......&...........462.939+..439.......*845.....443..... +.....@360.842......986%.902-.....21.307....234*......*...324.......505....*.............315..............*.............................*.... +..........*....................*.*....*..............42..*................474...../919................50.674..713................./..27..... +........249....291...........448.622..228.......308.........189...............629........-...................*............594..127......782. +..................*173..817.....................*.......91.....*..#................*649...492...400.........17...878.....*..........950..... +...32.674......-........%.....402/...........610..%.....*.....549..853...........96.................=.............*....779.......@.......... +.............885.................................600.456...........................................679.............16.........392.....841... + diff --git a/2023/day3/day3/inputTest.txt b/2023/day3/day3/inputTest.txt new file mode 100644 index 0000000..b20187f --- /dev/null +++ b/2023/day3/day3/inputTest.txt @@ -0,0 +1,10 @@ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. diff --git a/2023/day3/day3/script.py b/2023/day3/day3/script.py new file mode 100644 index 0000000..212d84f --- /dev/null +++ b/2023/day3/day3/script.py @@ -0,0 +1,43 @@ +import re +from collections import defaultdict + + +f = open("inputTest.txt", "r") + +lines = f.read().splitlines() +outputSum = 0 +outputSumP2 = 0 + +# Dictionary of adjacent numbers with a list as default value +adjacent = defaultdict(list) + +for i, line in enumerate(lines): + for match in re.finditer(r"\d+", line): + indexes = [(i, match.start() - 1), (i, match.end())] + for k in range(match.start() - 1, match.end() + 1): + indexes.append((i - 1, k)) + indexes.append((i + 1, k)) + + for index1, index2 in indexes: + # Check if the index is within the bounds of the list + if index1 >= 0 and index1 < len(lines): + if index2 >= 0 and index2 < len(lines[index1]): + # Check if the character is not "whitespace" + if lines[index1][index2] != ".": + # Add the number to the output + outputSum += int(match.group()) + + # Part 2 + # Append number to the adjacency list + adjacent[index1, index2].append(int(match.group())) + + break + +# Part 2 +for val in adjacent.values(): + if len(val) == 2: + outputSumP2 += int(val[0]) * int(val[1]) + +print(outputSum) +print(outputSumP2) + diff --git a/2023/day3/day3/src/main.rs b/2023/day3/day3/src/main.rs new file mode 100644 index 0000000..a060050 --- /dev/null +++ b/2023/day3/day3/src/main.rs @@ -0,0 +1,61 @@ +use std::collections::HashMap; +use std::fs; + +fn main() { + let content = fs::read_to_string("inputTest.txt").expect("Could not read file"); + let lines: Vec<&str> = content.lines().collect(); + + let mut output_sum = 0; + let mut output_sum_p2 = 0; + + // Hashmap with a list of adjacent numbers + let mut adjacent: HashMap<(usize, usize), Vec> = HashMap::new(); + + // Create regex automaton outside of the loop for better performance + let regex = regex::Regex::new(r"\d+").unwrap(); + + for (i, line) in lines.iter().enumerate() { + for cap in regex.find_iter(line) { + let match_str = &line[cap.start()..cap.end()]; + let num = match_str.parse::().unwrap(); + let mut indexes = vec![(i, cap.start().saturating_sub(1)), (i, cap.end())]; + + for k in cap.start().saturating_sub(1)..=cap.end() { + indexes.push((i.saturating_sub(1), k)); + indexes.push((i + 1, k)); + } + + for (index1, index2) in indexes { + if let Some(line) = lines.get(index1) { + if index2 < lines[index1].len() { + if let Some(character) = line.chars().nth(index2) { + if character != '.' { + output_sum += num; + + // Part 2 + // Append the number to the adjacency list + // If no element is found in the hashmap, create it first + if let Some(adj) = adjacent.get_mut(&(index1, index2)) { + adj.push(num); + } else { + adjacent.insert((index1, index2), vec![num]); + } + + break; + } + } + } + } + } + } + } + + for val in adjacent.values() { + if val.len() == 2 { + output_sum_p2 += val[0] * val[1]; + } + } + + println!("{}", output_sum); + println!("{}", output_sum_p2); +}