From 936a55c81448047b8739367d27f7f4856b1d7dc5 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Fri, 10 Dec 2021 10:45:11 -0700 Subject: [PATCH] WIP AOC day 9 pt 1 --- projects/aoc/src/year2021/Solutions2021.kiss | 13 ++- projects/aoc/src/year2021/day9.kiss | 48 +++++++++ .../aoc/src/year2021/inputs/day9-example.txt | 5 + projects/aoc/src/year2021/inputs/day9.txt | 100 ++++++++++++++++++ 4 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 projects/aoc/src/year2021/day9.kiss create mode 100644 projects/aoc/src/year2021/inputs/day9-example.txt create mode 100644 projects/aoc/src/year2021/inputs/day9.txt diff --git a/projects/aoc/src/year2021/Solutions2021.kiss b/projects/aoc/src/year2021/Solutions2021.kiss index f268dea2..a9eec83b 100644 --- a/projects/aoc/src/year2021/Solutions2021.kiss +++ b/projects/aoc/src/year2021/Solutions2021.kiss @@ -6,6 +6,13 @@ (dictSet theMap key (set count (Int64Helper.fromFloat 0)))) (dictSet theMap key #|count + amount|#))) +// Remove and return an arbitrary key-value pair from the given Map +(function :Array dictShift [:Map m] + (let [key (.next (m.keys)) + val (dictGet m key)] + (m.remove key) + [key val])) + (function run [] (day 1 (load "day1.kiss") @@ -45,7 +52,11 @@ (assert (= 356958 (fuelToAlignAt (bestPosToAlignAt positions) positions))) (assert (= 105461913 (fuelToAlignAt (bestPosToAlignAt positions true) positions true))))) (dayTodo 8) - (dayTodo 9) + (day 9 + (load "day9.kiss") + (assert (= 4 (count (lowPoints "src/year2021/inputs/day9-example.txt")))) + (let [lp (lowPoints "src/year2021/inputs/day9.txt")] + (doFor =>key val lp (print "$key : $val")))) (dayTodo 10) (dayTodo 11) (dayTodo 12) diff --git a/projects/aoc/src/year2021/day9.kiss b/projects/aoc/src/year2021/day9.kiss new file mode 100644 index 00000000..d9503013 --- /dev/null +++ b/projects/aoc/src/year2021/day9.kiss @@ -0,0 +1,48 @@ +(function :Map pointMap [file] + (let [:Map m (new Map) + lines (Util.readLines file)] + (for [y line] (enumerate lines) + (for [x tile] (enumerate (line.split "")) + (dictSet m (mapKey [x y]) (Std.parseInt tile)))) + m)) + +(function :String mapKey [:Array coords] + "$(first coords),$(second coords)") + +(function :Array coordinates [:String key] + (map (key.split ",") Std.parseInt)) + +(function :Array adjacentPoints [:String key] + (let [[x y] (coordinates key)] + (map + [ + [(+ x 1) y] + [(- x 1) y] + [x (+ y 1)] + [x (- y 1)] + ] + mapKey))) + +(function :Map adjacentPointMap [:String key :Map allPoints] + (let [:Map m (new Map)] + (doFor point (adjacentPoints key) + (whenLet [h (dictGet allPoints point)] + (dictSet m point h))) + m)) + +(function :Map lowPoints [file] + (let [allPoints (pointMap file) + pointsToCheck (pointMap file) + :Map lowPoints (new Map)] + (while !(= 0 (count pointsToCheck)) + (let [[key height] (dictShift pointsToCheck) + adj (adjacentPointMap key allPoints) + &mut isLow true] + (doFor =>adjKey adjHeight adj + (cond + ((> adjHeight height) + (pointsToCheck.remove adjKey)) + ((< adjHeight height) + (set isLow false)))) + (when isLow (dictSet lowPoints key height)))) + lowPoints)) \ No newline at end of file diff --git a/projects/aoc/src/year2021/inputs/day9-example.txt b/projects/aoc/src/year2021/inputs/day9-example.txt new file mode 100644 index 00000000..610bad94 --- /dev/null +++ b/projects/aoc/src/year2021/inputs/day9-example.txt @@ -0,0 +1,5 @@ +2199943210 +3987894921 +9856789892 +8767896789 +9899965678 \ No newline at end of file diff --git a/projects/aoc/src/year2021/inputs/day9.txt b/projects/aoc/src/year2021/inputs/day9.txt new file mode 100644 index 00000000..c97ee01d --- /dev/null +++ b/projects/aoc/src/year2021/inputs/day9.txt @@ -0,0 +1,100 @@ +7678921234988678901238954323498765432125789999567898765454223789989865432124569899894325678989212965 +6567890139876589992367896512987654321034569988979999879321012569876987573245979767799434599878909893 +5456799298987699989456789401398765732125678967991098988632123458965987684347898955688976988768919789 +4345898987698789878967894323499977543245789456789987999544345567894398789458997544567897976956898678 +3212987656569898767998985435989899764766789366789875987656776678993239896569987632456789865432234567 +4336796543456989658789976549978798765677890155898964298767897899789123987678998874567893984321045698 +5445995432345678945669987698766689976798931234987654109988999935678939899789679965678912995452166789 +6569876521236789123458998987654567899899545356998543212399990126989298759894579878789429876743278995 +7689986432345691012567999098767678976989876877899984323456789237892129642923499989896545987895345954 +8789976545856789134569892199898789895976989988999876567967898356799298731019989997998656798985457893 +9895989686787899246698789989999898769894394399989987678988987577898987656198968956999897999876568954 +4934399787899997657997679879987957656793239498765699789999599988967798868987657849899989879987679765 +3210239898989298799876534568896543234899398997654249899923478999456679879876543434678976756798799876 +4323359999879129987655423456789654455678966789743134998899567964345569998765432323459765433459893987 +5535698998968934599543012348998766566789355678951034997678978954233498999988751012578978712778932398 +6645987897659549698792133959429987679891234589432129865567899543102987899876542123457897653567890129 +9759876789798998999983249898902398789932356796543298754346789643229876553987854348578998765778921234 +9898765990987877698765398767893569898756789897954349853245678994346985432398765467689689978989434378 +9949976891396662549876989756789698998767893959876498432134679989459876654459876567896567989997545456 +9956987932985451234989977645656997649998932345987987543245789879569998786867997678987499797897697967 +7899999549876310125698765432345896432109985456798998676346789567998759897878998789599986656789989898 +6798897656995421234999876501456789543998976567899989785456894456789543998989559895498764345899878789 +5676789799876532459899997632567897669887987678989976998767943345678932109995446996987653234998765678 +4565692987987683769789876543478998798756799799878995439889321234589543319865325789876543129987644569 +3234891996598954598698998674989439979545678998956989421995495345678954429876434567998652098999432878 +4545689879459765986567899876896529865436567987545678939876989459789867834987545699899754987898541245 +7666799767249899865456778987898698965323457976434567891989879567899989655698956989798999876987672396 +8787898654123999794325767998999987979412399854323457892499968998998998776789769876587989765698983989 +9898987653234598643214656899998876898924987643212356789598956989767899989899889976465678924589999878 +8959199764545689954902345789987765786899876542101245678987547678958899898967999984324569012367899767 +7543019878789899899895496899876643245689987653212357899498636569446798767856789999313458923457998758 +9652123989896998789799989901985432136793298765323768912399923479234987654545998898926567894568989549 +8783435699934987654678978919876543046789109895434879101989812568995698543239896787987678985879876435 +9897545789219876543234567923987652167898912976545689239878601477889789632198785676798799976989989324 +9998676792101997689345698934598743999987893987866789398765412345678998744987674345889899899992398435 +8799787893912398795458789545689899887876689798978996989876323456889898759876543234578999788921987567 +7678998959893459987567899766798998776564599649989565878989554567996789769985432123699998677892398878 +6578999446789567899678998977897899656453878932199434567898765678945698978996543434589987545799469989 +3467891235697679998799567898986798743212367943098923456989976789434987899987655546678998624678978998 +2379910145789798969896466789875669853201458954987897599878999898559876789899897677889359534567899987 +9498921234578967345912345698654356965342367899876798989767899987698865678789998788990297645678999896 +8997945945689653239543456789743239877463478998985679975656789998987654235699869899459987657789998765 +7686899899789764398654678999899345998569989767564578954545689989599875123987654912348999767899979654 +2545798788999875498798789987988957987678997654353989543234569876421986013498763201567899878998765423 +1236789657899989569899899986767998998989987543212895432123698765310197124569654323468998999769876534 +0198996545678997689968909865456789989799898987323789543254789898725239295998767434569987987656998676 +1246789324568998798754219874367899875698769996534679654345679997654356989899896545678976898543459987 +2345994213579329999864346976788987654398657987785789876456789999765459878788987698789875795432346899 +3456943103589439899976487987899398966497546798999897989597899899976598765697898789895934989521235678 +6577893212347998799987898998989209897989434989321956899989999789987699654346889897954329878945656789 +7898954523456895688999999879878998789879219876532348998978987678999987543234678976543219867896867897 +8949765434577934567934698765467899654569998997543467987767896567899995432125698988655398988987898986 +9939876657689810128955987656356789543498766789676579876456789459929876321014567899789997699898939345 +9899989769798752379969876543245678932398754596987689996567992398910987432323458959899878598789320123 +8789999878987643467893985432134699210987643135898792987678931987934598893434567945921965498678921939 +4678999989598654688912396543245964329876532023789910199789549876899799764545678956899876324567939898 +3456789895429765678901987656457895435995432134567891249897698965788989879659789968998765412679898797 +4597998766219876789799999786578987559876543265678943459998987754557678988769899879879876543498776656 +6789109854101989895698999898689499678987665345789759598789876543234567999899901999768987656989654235 +7899298943212399923987997999995398989298887969899898999567987432123459877989219878957898769876543123 +8987987654434459439876786789889987899199999878978997898689989731036769765678998767942109879988654234 +9566798765645798998765345698778656798987878989769876998799876545645998954567896659956212989999986646 +4345899876756987899873234569656546897976967897654645789893987676789987943456964348897323499854598758 +3234998989967986789982126997435135456895456986543234678901998787899876542546893237789434999743219869 +9099767898999865689321019876521012367892356798632126778919879898998997621238964345678956898654309878 +8987652967989754578993998765432763778901256989321034567898765909987898730145695476789867959765412989 +7896543459876543469789879876743454678994349876532123456789973219876799421234689587891979039876567899 +6987699967985432379699867997854575789689467987944354569899894398765987632345898799910989123987679988 +5498987899876541234578956798965676793578989099895465678976789987654598943656789899321995434598989767 +6589876999997672345689545899877989892345893139799876889765698998873459658769898957949876565789397656 +7678965698976543456789323459988999943456789298689989997654567999764569879878997745698989876899298743 +9899894797897765689893312568999598764567899987578993298785979898975678989989986534987899987998999652 +4987643656798976793932101478965329998789999876459999109999898767899789694499997929876799999897898921 +3298432345789989892953212379654219899899876432367898919878798756998999532378999897655678998786987890 +5099321234877895989894324569795499789999987321458987899957654346897898921569998789743235699674876791 +6988430123456794578789456678989987678998765432567896999843210123456987892459976678954346986553285892 +7976651237897943445678967889978976587899876543678965698754942434567935793598765569899457996442154589 +8995432456789321234567978998767894356789997656789214569869893545678923989789874346778967984321023478 +9987659567895432345689989999846789267899898767892103489998789678989019878999995234569979876465434567 +2198968789976563456792199899656895458986789878943212567987659989392198965487893123479989987566545678 +9099879999998654567989299798767976569875897989654343459876548990129987654356989235689495698998676799 +8989999987769765679878987649878987679564456899868764567982137892498975431245678949794323499998787891 +7668899876545989899768999534989998789432345689999879779873256899987653210134567898999467987899898990 +5456789994323499998657898745699979895431956798989989898654568998798974321265678987998979886789979789 +4245790295934589876546789976789865976599897997878998939765978989659765432347889986987898765699865678 +2134891989895678996534578999898654987988789896767897659877899678942986543656798765976789654599654589 +3256789978789989987321367899987843499875699765656998798998936567890197665689897674765678963688912345 +9346789865678999865475456789996512569764598654546789987659123489921298778895999543124789754567901234 +8957898654567921976986667896989423498843699543234678976543234595432569989954398751012678965679892945 +7899987653459892988987778965978934987654985432124599897964545679843456798743297532154567896798789896 +6899999542356789899299989754567899999769876563465789789875657789654577897659349854365789949897679789 +5678987601298997789124596643454598789989997654567898679986768898765689999798998768456895435989567678 +4567998732349765691015975432123987667895329765678976588987878999876998898987649879768929759878454569 +3479876545458954589129896543239865456789439878899643456798989989989876787976534999878909998764343457 +2567987657567895678998789654497654345678945989998532145799098767898765476899745699989698999873232565 +3456798978678976889765678967986643234789656793987643234689198656987654365678956789995466987652101234 +4568999989889987996554779879865432123898767892198754365699987545976543234579987899954345699843232345 +5679987691998998965443456989976321034999878943598767896789876529865432123689298989875677899854345456 +6789996532467899654321269890195432146890989659679889919899965419876543254590129678986789998767957687 +7999986543456798767532345789987543256932399767899995323999876201998754365789234567897890129878968798