From: Neil Smith Date: Thu, 9 Dec 2021 15:26:27 +0000 (+0000) Subject: Done day 9 X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=150f4ed9cd6cf5a7a29de631eb9b6ff9802dc88d;p=advent-of-code-21.git Done day 9 --- diff --git a/advent-of-code21.cabal b/advent-of-code21.cabal index c3b8977..9e50851 100644 --- a/advent-of-code21.cabal +++ b/advent-of-code21.cabal @@ -124,3 +124,8 @@ executable advent08a import: common-extensions, build-directives main-is: advent08/Main-longwinded.hs build-depends: text, attoparsec, containers + +executable advent09 + import: common-extensions, build-directives + main-is: advent09/Main.hs + build-depends: array, containers diff --git a/advent09/Main.hs b/advent09/Main.hs new file mode 100644 index 0000000..4cd269e --- /dev/null +++ b/advent09/Main.hs @@ -0,0 +1,85 @@ +-- Writeup at https://work.njae.me.uk/2021/12/09/advent-of-code-2021-day-8/ + + +import Data.Array +import Data.Char +-- import Data.Maybe +import Data.List hiding ((\\)) +import qualified Data.Set as S +import Data.Set ((\\)) + + +type Coord = (Int, Int) -- (row, column) +type Grid = Array Coord Int +type Basin = S.Set Coord + +main :: IO () +main = + do text <- readFile "data/advent09.txt" + let grid = mkGrid text + print $ bounds grid + print $ part1 grid + -- print $ lowPoints grid + -- print $ breadthFirstSearch grid (S.singleton (4, 6)) S.empty + print $ part2 grid + +mkGrid :: String -> Grid +mkGrid text = listArray ((0, 0), (r, c)) $ map digitToInt $ concat rows + where rows = lines text + r = length rows - 1 + c = (length $ head rows) - 1 + +part1 :: Grid -> Int +part1 grid = sum $ map (riskLevel grid) $ lowPoints grid + +part2 :: Grid -> Int +part2 grid = product $ take 3 ordSizes + where lows = lowPoints grid + sizes = map (basinSize grid) lows + ordSizes = reverse $ sort sizes + +riskLevel :: Grid -> Coord -> Int +riskLevel grid here = grid ! here + 1 + +lowPoints :: Grid -> [Coord] +lowPoints grid = filter (isLow grid) $ indices grid + +isLow :: Grid -> Coord -> Bool +isLow grid here = all (> this) nbrs + where nbrs = map (grid ! ) $ neighbours grid here + this = grid ! here + +higherNeighbours :: Grid -> Coord -> [Coord] +higherNeighbours grid here = filter isHigher $ neighbours grid here + where this = grid ! here + isHigher there = (grid ! there) > this + +basinSize :: Grid -> Coord -> Int +basinSize grid basinSeed = S.size $ breadthFirstSearch grid (S.singleton basinSeed) S.empty + +breadthFirstSearch :: Grid -> Basin -> Basin -> Basin +breadthFirstSearch grid agenda basin + | S.null agenda = basin + | otherwise = breadthFirstSearch grid agenda' basin' + where here = S.findMin agenda + candidates = (S.fromList $ higherNeighbours grid here) \\ basin + basin' = if (grid ! here) == 9 + then basin + else S.insert here basin + agenda' = S.union candidates $ S.delete here agenda + + +neighbours :: Grid -> Coord -> [Coord] +neighbours grid (r, c) = filter (gValid grid) + [ (r + dr, c + dc) + | (dr, dc) <- [(-1, 0), (1, 0), (0, -1), (0, 1)] + ] + +gValid :: Grid -> Coord -> Bool +gValid grid (r, c) + | r < minR = False + | c < minC = False + | r > maxR = False + | c > maxC = False + | otherwise = True + where ((minR, minC), (maxR, maxC)) = bounds grid diff --git a/data/advent09.txt b/data/advent09.txt new file mode 100644 index 0000000..b943c4c --- /dev/null +++ b/data/advent09.txt @@ -0,0 +1,100 @@ +9987675345698765453987654321234589999899878923493212345678999998656782467898999899878301234578998787 +9876543234789765322398743210123567898789767899986101239899789876543101567897898798763212355989987656 +3987784014897654310987654331235679965697644998765232345997654989765212799956999679854343466799976545 +2199875323998765421298776542346789434595433498754345456789543499876353489549876542975858977899876439 +1012965439899896432359987667457896546789322349987656987898932445987454578932987621986767898943989598 +2125976598798989943467898876589999858899910959898767898976101234598966689431298320987898999432399987 +3234987987656579894578989987679998769999899898769879999965422345679298995420139434598999896543459875 +4346799876545456789679877898789769878989798799656989899876563496799109989591298548999689789656598764 +5956998765434345698989765679895456989678679678943496789988689989898997979989987657987565698787989543 +6899898764321234567999876989954345696534598789432134679999799976987656765778998769766464989999865432 +7987679653210155789999997899875456789323999899521012345899899765698943254667999898954353578999654321 +9696598654323234589788998998986567898919894958933423456789999753459892123458998987893212467898765432 +8543498765434545679567899987987679987898743147894934567898989432398789334767987876789101298989876543 +7656789878545656789338989876598789986797654235679895678997878941987678975879896545698914345678987654 +9868998989656877892129678965439899975498754346998796789986867932954569986798765434567895458989998765 +8979987798787898921012567894321999874329985459876589899985457899873458987987654323469986567899869876 +7899876549898959999923456789432398765909876567987678999875322987654567898998773212378997698998754987 +6534997831999943987894567897543999899899987878998789789964201298765678919679654323456789799987632399 +5424598942689899876789678987659899988788999989019895699865362349986989103498795434567899899996549989 +4212349653498789765699989798798789877697898999999924789976457899798993212379986747698946999897998878 +5334569878989678934998995679987678954545987899789935689987567987649898763467897858789235798789887867 +6446878989876569976897893459876567893239876797689898789397679893234789654598998979999546999698785458 +7557989199987457898956789998984348994124965323486789895498798789123569875989109989898969898598654345 +8998994398796346899545699876543236789549876412345678979569987679023456989878992398767898767469643234 +9999689987654235695434689987652125678956987954456789768979876568994568999867989987659999955398759345 +7887578976543123489545679698983234789768998765677993457899765456789878997654878998767899843229898956 +6543469998768245678956789529865445899879999896798912349998987347899989989653467899879998732019987897 +7672398999875456989767895439876556789989989999899102356987798456789899878932356789989987643498976789 +8954987899986567899878976546989667993498979978976212469996549567895798969991245699899998754987685698 +9769876989987898901989997656898789101997768769895433498765439878934987656789346789788999869876564567 +9898765778998979312397899767999893249876753656789656569976524989423976546789498997697986998765473456 +3987654569789765434456976978999974756965432345678987678987734694319887635689989998456894219874321567 +2198733477678978645797895989989865669876543766799998789599848789998765212678979999367975323985432378 +3989821234568999756898934599878976798998754567898999897679959899899874323599867895459876434596543458 +9876432347679549887969423498969987897989865698997899979898767998798765435679756899567987546987654567 +9876543489789832998953210987658998996579878789876588965939989987659987546789645688979987657898785678 +3987654579898721239998723976547899987467989999995477894321098765545698687895436577898998898999896789 +2398766789989654359899644597656999876345697679654356965434197543434989798954323456987899939998987893 +1239887894678965498788987698987999765234789598765467896545976532129879899996534567896789123976798912 +0945998923567896997687898999298987653123696439876588987679876549098765945987965778945693239875459901 +9896789434678999876575999899399199762064594321987678998789989698987654326599878989238789459954345899 +6797898645789998767434598798989349872165689410198989769899898997898543212367989994345678998986254678 +5789949756894987654323497687878959989278796521239797653998767876799654302456999965456899987432123689 +4569439887893998799934598576967898995478897432345698992989545665698775212567899876697899876543234567 +5678921998969899987899974475459997898567998945456789989878932124569854323498976989989965987654345678 +6799990199656789876798863212349976987678999896897899879767893234698765434578965699878974598785458789 +9899989989543298765987652101267895999799998789998998968456789345999879545679854598767943459987569893 +3998979678932129894598543323458934889934987698999987654345895469876997656789765987659894567998678942 +1987667569892099989679656445667895679899876597896596543256896567989998789899876798547689978999789531 +9876553456789989878998767986898996798788995456789987752125789678998999897999987899534567899989898940 +8765432345679878967999899597959789987576789347899876543234589789987899975689999987656789929876967891 +8764321234698766556899995329345678987455678956789989684348678999876798764567898998769893212975456932 +9863210123459654345688989910258789876323589969894398765457789998765679923459987869878999109864349894 +9854525234598743234567979891345897995474567898989239878767899997654567895678996555989998919753239789 +9765434545987655455679865789457896986567678967878946989878959876543456789789789434599987898954398679 +9896565656898766767999954567978965497678989656567898998989245988652346799894698765679876767895976532 +9987879768999879879878323489989654398789893432349899987690159899721278967923989876798965456987987699 +8998989879988989998965412397898765219899762101234789996521998798754367899999878987987654347898998987 +7659395989876897987654323456789898323998753242345678989439887699865456899989769999876543236789019986 +8543214598765456798987654589896987536789894355456989879598754589876967999877658999997655345678998965 +7654323679876578969999865678945698545678976566567898769987653479989898998766546898998786457799987654 +8795498793997989657899976789234987657799987677678965657898794567998789987653234567899898568899998723 +9989989932398996546789989890123498968989998788789654346789887679899678996442123458901987678999899812 +9878879993989875237899999989254989979878999899898766869895998997676567894321015667892398789999798923 +7767767989878943123978999878969978998967899922939878998953219876565456976534124578943469999988697654 +6553459876767894234568986767898767987656789210125989987994101985432389997645234567894590129876598785 +5432345985458995679799875456989659876545456991234899876789919996676569987656545678965989239988439986 +4321359894346789789987654345678943965434345789546799765679898987787878998787656989879879998895321298 +5430198765497899894298763234567899876521234899987987654598787999898989109898767899998967897654210129 +6541679876989945999987654345678989987752345678999999766987656579999793212999878999987656789775323234 +6432589989877899998998765476989978998843459789998939878996545468989654329989999998798787894989876545 +7543458998965778987679976687898767898754678999987923989876432345678976498978989987659898943497987676 +8654567987854567896563987798998656899865789019876894599987321238789098987967878998743989932346798888 +9766779876543488997432398899987745679877892198765789998765450139989129876754568999974567895459999999 +9877898765632356789543459921096434567988943989654567899986521234678998765323467899865679976598897912 +9988987654521237989699567899987558978999659876543457899987634656899986544212456789877889987987656893 +9999996543210357678987998998998667989431968997652346998998785667901997432101348995998994398996545789 +8932987987631234589476789987689978999599878994321345997899996778929899546712467894239987469997434679 +7891098876546349679345678965578899998989989989935459876979897889298778994324578943190296599989323567 +6989129989865478993234567893456799987878998776896567965468789990199656989435678954989987989978934569 +5678949999976569310123689932345689876569899565789778984345688999987645678945789769979999878767897698 +4567898999987679453294594321017992985498765434698999993234567898765434899656899998768999765457898987 +7698987898799789564989695532399891996309899323456789832125678919876325999797999887657987654346789876 +8999876789654998679879989649989799876212987437899898761012348901985476789898998765431298743235689985 +9899965689543249998765679998767678965429876556945987652125667899876787899979459963210139852123798954 +8788754799932129879654587789656567896578987987899876543234799998989898998764349854321298764234567893 +7656543458794298965443445678932456789789598998901997654345678997693939789943298765432359879876899952 +6543252345679987654352237899751577899895459789212398897656889986542124599892109887545456989987896541 +8432101489989999875210128932967698998901345699924569998767994987431023999789213998676569995699965430 +7654312678999998996321239999878789767892396989895678989879993976545129897678954598789678934567976521 +8976433589998767987542356789999891557789989978789789467998989897685398789569899989899789325679897432 +9987544567899655698653468999987910345679878765678992345987878799896987667348798976949895434599789545 +9999655778999543219766567899876621234598765454567891249876567689929876543237667895435976565987679656 +8798789989988959309877678999765432545699654323479910134995456578912997632123456789523498878996598977 +6549891299876898912988989789876547686987543212567891239984323469329876321014567898654569989985456799 +1234932997764667893499995678987658798998654403459932398765012478998765432125678979866789699876345678 +0356799876543456994567894567898967899019873212378993987654139989219876843336789565977896579843234589 +1235789987532345789978943456899878978929764323456789398543248892101998765459895434598965498765445678 +2356897698421234567899432346789989767899875435668891239654356789432789887567976323699654329876786789 +3567896543210246788987541457892499848921986546879910129767568996545699998678988434789543210989897899 \ No newline at end of file diff --git a/data/advent09a.txt b/data/advent09a.txt new file mode 100644 index 0000000..610bad9 --- /dev/null +++ b/data/advent09a.txt @@ -0,0 +1,5 @@ +2199943210 +3987894921 +9856789892 +8767896789 +9899965678 \ No newline at end of file