Skip to content

Commit 1abe238

Browse files
committed
Solve day 9
1 parent eb7abea commit 1abe238

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

2021/9/input.txt

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
8754399998765678934569897649898767987432123986543213567997681024589763459854327895454567899979876567
2+
9865989879854578923998789438789656795421013499854101278976542137678954598765456964323456789768995456
3+
1979879965913569899878679626598947987532194987653232389898756398889865679876587953212457899656789345
4+
0999767894301346798767568917467898998999989998754343499769877479996986789987678943103998988545999156
5+
9875658789212457899653498902345789989987978999875454678956998567895397898998789653239899876839878997
6+
2984545678923498987732497893459898679876867899986568789545459789954298967899898764349768995424456989
7+
1973123789434589876521256789767986598765458998797678899632349895432109456789909865997659789312349979
8+
9862012578955678965410345789979895439897346789698989998921234999543212367898919979876545689101298864
9+
7653223467896799876321235899998754320989477897569099987892545898767323498987898998765435679232987743
10+
8868434578987899965434345678959965439876568999432129876989656799875456999456997898765323578949896542
11+
9876545699198978976745676789349878545987679998764598765679767899876567894359876987653214789898785621
12+
5987676789019567898956897899956989787898989999975987654568978999987879945298765698775623456789654310
13+
3298798993123458999498969999899996999929497899986798923589989998798989932129854349986737687899794322
14+
2129899986548899992359356789798765678912345678997976534678999987689999893998743234598898788929876533
15+
3348998997656789989467969899659978799323567789989998645689999874578999789897654345689949899434987545
16+
5656987898767899878979899998743989976539678999978909897898979953489987656799765656792125989695997657
17+
6879876789888989867999789897654599987898989999868912998997567892198986545698976768921014568989898798
18+
8989865678999876548987678789765679099987899898756793569989998989987665434567897979432123459879789899
19+
9498654569643987656996545678978789129876989654345679699879899878998543212367998989543456998767689998
20+
4349865678954598987894323489989899298765678973216789989765679869898664323458999297677679877654568997
21+
3239876789985699898965412345996998989874567892101234978954398756789775434567893139798899965433699976
22+
4109987899876987679876103457895987879983459989235349867895987645678996755678921029929989954321788965
23+
3298998965998976569954323568999876767892598768976798658999876535869899876989432198939767895434567893
24+
4397899893219987698765454567897965656991698657997899545699965423456789987897543987898956789567789912
25+
6976976789909998789896566788945694547789987645789999434589876534787895898998659876787645678998992101
26+
9965865667897899890987989999896983234679876535678998545678987645678974799329969865486536789999543322
27+
8754323458976899921298991556789975135798765323456987678789999876899753689919899986321017899987678433
28+
9843212357894567893989632345899989245899986434567898989899876987976974567898798765432128989999989994
29+
9954301348943656789876546786999895346953497565778949799998764398975965689998659986554534678998998789
30+
9865412367892347998987697997899765456932987678899235678929865459989878789987643987866545799567897678
31+
1996537569901456987898989998939876767891299899910123799219876567897999891987654598977656892456789567
32+
0989987678912367896999878999212987978910123992391234942109998678986579910199799679198787891367993456
33+
9878999899953567895698767898923498989521345679989349899298769999975437921988978989019898942478902567
34+
8767899989879789934989656987994989995432656789778997678965459889876556899876567899999999543589323478
35+
9657789875999892129878549876789879876543467898656789498976797679987987898765456789879987674696534567
36+
9545678954569954298765432345689756997654578909947992347897987568899898949993234898968698985897647688
37+
8434567893498765459986321235799842398767678919899743456999876456789789234989148897657569697898798799
38+
7545798932349876578996410134999921269878789998789654567899765347898698949878956789543454598999899912
39+
8786899543467987889987821235689543459989899886599995698998654236987567896967899898432123999898965301
40+
9897999964878998999998642547898654567993998765457889789879764345897678985756789987645239899767993212
41+
6989989895989979479899753457929865788901989654345679998769865456789799864347999897656798778456789323
42+
4579876789998768366789876569019879899899876545234567899656976577892987643238998789978989563348995434
43+
3398765678929854255899989678923989976789985432125678932987987698921098432159876567899765431237896545
44+
2129654569919867134989999989434599965691096421034789321398999789993298541099965467909876210176989766
45+
1019993457899773235678998996545699894989297533145895435459799899989987693989876378919986321245678997
46+
2298789967899654547999997987699989792165987654256789945997689998878998989878987459998765432346799789
47+
3987679899998765659531986798988777689014598765468899899886599987567899878767898567899878643456789678
48+
9876545788999878967999994339876566579923459876979956789765498765468998764356789679932989987567994599
49+
8765434567894989879878789212965452467894567988989245999854329878312349875457898789641094597679999689
50+
9965325789912399989865678909854321378995678999297457898763219885101256988768949896532123498789878993
51+
9978412892109989998754567898765210135789789980196568999874598764312345999879235987643435679893467892
52+
8989523993298678998653278999879321245678999871987979698765679984323679893989123498754545678942378901
53+
7995436789396545698764345699875432356789498762398989549896893986654598782393234569767657899321999992
54+
6789545678989434579975456789986743456789219654569991239989921098765697654459945679898968998939889989
55+
5678967899678924569996567897399865767898999765678989998878932129976798765667897789959979987998775678
56+
4567898934569319698789978965246978978987878976789679876767893239987899986899939893244598976899654678
57+
2458979320178998987678989954358989989876767987898789765456789398698912399996547992123497865678943789
58+
1278965491389987896567898765467999998965656898959898654345678976569699498989956789019986564989654894
59+
2367896989499896985456789876569878987654548789349999875234567895434578997879899892198765473699879932
60+
3456789878998765674367898988998767996543237678998998692123458954325678986567798943999654312579989321
61+
4767898769987654561234567899987856789542123567897987543254569543212349895454687959876543201467898730
62+
5978997658998963210123679901976545878953234689995498954345678969101998765343456799987875412345679641
63+
6789899867899654721234569219875634767894645789789599898758789998929876543212345689998964323456789432
64+
9896799978939875674348678939994323456789656898678988789869899987999987672101234578998765537577997545
65+
9985989989012987786759789998963213697898767956569876679979989876989899865216795789569876646989798986
66+
9874578992124598999899899987954325989999998943498865567899879765679766994345678995459987658994679987
67+
8763469789235789921932999896899476978999989432976543456799854323456954986776899212398799869543456798
68+
7542345678945699892921298765778999869689976569877331357898763210567893098987942101987689978954569899
69+
9321234567896798759893987654567897654567897698765210237999895432378932199798994299876579989765678912
70+
5456347698997987645789199873456789543456998799854325446789976556489653987649789978765467899879889923
71+
6565456789999765434679297632347997654868949989965436556892989667689769999735699866212345999989999899
72+
7676567997898652123589986544459899765689939878976787667921998789799898989129789754323456789992345798
73+
8798678956789843254678987676598798976789898769987898989939879899899987678998998765458578998921959897
74+
9899989767898764567889998787987687897999797659899969396899965910979876567887999896567699987899899986
75+
1932199878969975678999989899895456798998678645779954245789874321256975453456789987779789876799799875
76+
0193998999456989789998776987678347989997576534667895345699765432346965322345678998989998765678678964
77+
1989897632357899899889645987563239867976434323456987456798976543499876410238789879997897654234567973
78+
2979789543468921998765439765432153458985321012667896567987997654987654321234894569876789543123457899
79+
9865698954599893987656023986549012369876432523678987679876889765698766432457943498765679764034598978
80+
9764567895987759876543234797898137478987675434567898798765679896789879745567892987654567932125899568
81+
8643786789876545987654345698996546789198786545678979898664589987899987656678979699843457897236789346
82+
6532345678985432399895456789987857994019897656799567997543468998998998767799764598764598976347895459
83+
7653567989876531569979967891098968943235998768923456985432377999987549878894643469975689765456789568
84+
9765678998983210998767898962239979654459999879436569876621456789896432989943012979896799876568998779
85+
9876789467974329899879999543478989795998987989999978986532567896795321299932199898789899987879999889
86+
7987994359865439789989397654567899989787895699888899986543478965989432349893989747678989998999899998
87+
6798987212976798698993298776678999878576996898777789987654567894678943498789767634599878999986788976
88+
7999876434987977567979999897899898756455989989665568998765678923499854598678954523989769798765667895
89+
8987987565699765454567896998956796544234769876533457899876789765789767898789763219979945698754656789
90+
9876598978798754323478965449349897932123456965412345678987892976799899989999854998767824598743445678
91+
4989499989899998454589986321298989891015569654323456889798901987899989878899969876543213987652134567
92+
2398989990999997567995975410987677789234698765434568997659213499967879965799989987632101298543245898
93+
1987679891298997678954986829876566689345689876987678989998994569656567894678998996545212349654456789
94+
9876566789987898789993497845985434578956890998999799679886989798743459965699987989694393498766567899
95+
8765465698765699897989598956987316789897921989878954599765678999995998977789896578976989999897878989
96+
9943344987654987945678999767999402356789549976767893987654569899889897989899765467899878789998989879
97+
8432123498743495434389899879873212345898798865656899998543654698767786999987674356898765699019698768
98+
7643234898767894321235789998764325476789897654345788999652123598654645898797543237899654578924569978
99+
9654655679879985410134567899865445567899986545234567898761024696543234789643210123978976689635678989
100+
8765678799989876621245789999876786798998765432123456789432123987654345698775431234569987796545689999

2021/9/main.hs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import Data.List (sort)
2+
3+
parseInput :: String -> [[Int]]
4+
parseInput = map (map (\x -> read [x])) . lines
5+
6+
rowLength = length . head
7+
colLength = length
8+
9+
point m (x, y) = if 0 <= y && y < colLength m && 0 <= x && x < rowLength m
10+
then m !! y !! x
11+
else 9
12+
13+
neighbours (x, y) = [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]
14+
15+
isLocalMin :: (Int, Int) -> [[Int]] -> Bool
16+
isLocalMin p@(x, y) m = all (> point m p) $ map (point m) $ neighbours p
17+
18+
localMinima :: (Int, Int) -> [[Int]] -> [(Int, Int)]
19+
localMinima p@(x, y) m = if x < rowLength m
20+
then if isLocalMin p m
21+
then p : localMinima (x + 1, y) m
22+
else localMinima (x + 1, y) m
23+
else if y < colLength m
24+
then localMinima (0, y + 1) m
25+
else []
26+
27+
riskLevel m p = 1 + point m p
28+
29+
part1 m = sum $ map (riskLevel m) $ localMinima (0, 0) m
30+
31+
contains p l = any (== p) l
32+
33+
collectBasin m s p@(x, y) = if point m p < 9 && not (contains p s)
34+
then let [a,b,c,d] = neighbours p
35+
b' = collectBasin m (p:s) a
36+
c' = collectBasin m b' b
37+
d' = collectBasin m c' c
38+
in collectBasin m d' d
39+
else s
40+
41+
collectBasins m = map (collectBasin m [])
42+
43+
part2 m = let f = product . take 3 . reverse . sort . map (sum . map length)
44+
in f $ collectBasins m $ localMinima (0, 0) m
45+
46+
main = fmap parseInput (readFile "input.txt")
47+
>>= \x -> print (part1 x, part2 x)

2021/9/test.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2199943210
2+
3987894921
3+
9856789892
4+
8767896789
5+
9899965678

0 commit comments

Comments
 (0)