From 78620c770d9be6d8a7f48707edf2ef273d34de46 Mon Sep 17 00:00:00 2001 From: narimiran Date: Sun, 3 Dec 2023 17:02:28 +0100 Subject: [PATCH] day03.clj --- README.md | 2 +- clojure/day03.clj | 73 ++++++++++++++++ clojure/tests/solutions_tests.clj | 3 +- inputs/03.txt | 140 ++++++++++++++++++++++++++++++ inputs/03_test.txt | 10 +++ 5 files changed, 226 insertions(+), 2 deletions(-) create mode 100644 clojure/day03.clj create mode 100644 inputs/03.txt create mode 100644 inputs/03_test.txt diff --git a/README.md b/README.md index b031ccc..3a641c7 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ Task | Clojure | Day 00: Helper file | [aoc.clj](clojure/aoc.clj) | Utilities I use to solve the tasks. [Day 01](http://adventofcode.com/2023/day/1) | [day01.clj](clojure/day01.clj) | Tougher than expected for day 1. [Day 02](http://adventofcode.com/2023/day/2) | [day02.clj](clojure/day02.clj) | Immediately calculate maximums. - +[Day 03](http://adventofcode.com/2023/day/3) | [day03.clj](clojure/day03.clj) | Tedious parsing. diff --git a/clojure/day03.clj b/clojure/day03.clj new file mode 100644 index 0000000..341765b --- /dev/null +++ b/clojure/day03.clj @@ -0,0 +1,73 @@ +(ns day03 + (:require aoc + [clojure.set :as set])) + + +(def digits (set (apply str (range 10)))) + +(defn symbol-coords [grid] + (set (keys (remove #(digits (val %)) grid)))) + +(defn adjacent-symbols [pt symbs] + (set/intersection symbs (set (aoc/neighbours pt 8)))) + +(defn gear-ratios [gear-values] + (keep + #(when (= (count %) 2) + (reduce * %)) + gear-values)) + + +(defn find-solution [lines symbs gears] + (let [h (count lines) + w (count (first lines)) + zero-state {:pt [0 0] + :curr "" + :near-symbol? false + :numbers [] + :current-gears #{} + :gear-values {}}] + (loop [{:keys [pt curr near-symbol? numbers current-gears gear-values] + :as state} zero-state] + (let [[x y] pt] + (cond + (= y h) + {:nums numbers + :gear-ratios (gear-ratios (vals gear-values))} + + (or (= x w) + (= \. ((lines y) x)) + (symbs pt)) + (if (and near-symbol? (seq curr)) + (let [number (parse-long curr)] + (recur (assoc zero-state + :pt (if (= x w) [0 (inc y)] [(inc x) y]) + :numbers (conj numbers number) + :gear-values (reduce (fn [gvals gear] + (update gvals gear conj number)) + gear-values + current-gears)))) + (recur (assoc zero-state + :pt (if (= x w) [0 (inc y)] [(inc x) y]) + :numbers numbers + :gear-values gear-values))) + + :else + (recur (assoc state + :pt [(inc x) y] + :curr (str curr ((lines y) x)) + :near-symbol? (or near-symbol? (seq (adjacent-symbols pt symbs))) + :current-gears (set/union current-gears (adjacent-symbols pt gears))))))))) + + +(defn solve [input-file] + (let [lines (aoc/read-input input-file :chars) + points (aoc/grid->points lines #(not= % \.)) + symbs (symbol-coords points) + gears (set (filter #(= \* (points %)) symbs)) + solution (find-solution lines symbs gears)] + [(reduce + (:nums solution)) + (reduce + (:gear-ratios solution))])) + + +(solve 3) diff --git a/clojure/tests/solutions_tests.clj b/clojure/tests/solutions_tests.clj index 6d32158..b47983f 100644 --- a/clojure/tests/solutions_tests.clj +++ b/clojure/tests/solutions_tests.clj @@ -1,6 +1,6 @@ (ns solutions-tests (:require - day01 day02 ; day03 day04 day05 + day01 day02 day03 ; day04 day05 ; day06 day07 day08 day09 day10 ; day11 day12 day13 day14 day15 ; day16 day17 day18 day19 day20 @@ -26,6 +26,7 @@ (check-day 1 nil [56049 54530]) (check-day 2 [8 2286] [2239 83435]) +(check-day 3 [4361 467835] [528799 84907174]) (let [summary (run-tests)] diff --git a/inputs/03.txt b/inputs/03.txt new file mode 100644 index 0000000..6a07033 --- /dev/null +++ b/inputs/03.txt @@ -0,0 +1,140 @@ +.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/inputs/03_test.txt b/inputs/03_test.txt new file mode 100644 index 0000000..624ea4f --- /dev/null +++ b/inputs/03_test.txt @@ -0,0 +1,10 @@ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. \ No newline at end of file