10000 AoC 2021, day 9 · tobiasvl/adventofcode@9a1d972 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9a1d972

Browse files
committed
AoC 2021, day 9
1 parent 87e521c commit 9a1d972

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

2021/09/basin.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/python3
2+
from functools import reduce
3+
adjacent = [
4+
lambda y, x: (y - 1, x),
5+
lambda y, x: (y + 1, x),
6+
lambda y, x: (y, x - 1),
7+
lambda y, x: (y, x + 1),
8+
]
9+
10+
def is_low(depths, y, x):
11+
10000 point = depths[y][x]
12+
for adj in adjacent:
13+
_y, _x = adj(y, x)
14+
if _y < 0 or _x < 0 or _y >= len(depths) or _x >= len(depths[0]):
15+
continue
16+
if point >= depths[_y][_x]:
17+
return False
18+
return True
19+
20+
def recurse_basin(depths, y, x, basin):
21+
if y < 0 or x < 0 or y >= len(depths) or x >= len(depths[0]):
22+
return basin
23+
for adj in adjacent:
24+
_y, _x = adj(y, x)
25+
if _y < 0 or _x < 0 or _y >= len(depths) or _x >= len(depths[0]):
26+
continue
27+
if depths[y][x] >= depths[_y][_x] or depths[_y][_x] == 9:
28+
continue
29+
basin.add((_y, _x))
30+
basin |= recurse_basin(depths, _y, _x, basin)
31+
return basin | {(y, x)}
32+
33+
def main():
34+
with open('input') as f:
35+
depths = [[int(height) for height in line.strip()] for line in f]
36+
37+
low_points = []
38+
width = len(depths[0])
39+
for y in range(len(depths)):
40+
for x in range(width):
41+
if is_low(depths, y, x):
42+
low_points.append((y, x))
43+
44+
print(sum(depths[y][x] + 1 for y, x in low_points))
45+
46+
print(reduce(lambda x, y: x * y, (sorted(len(recurse_basin(depths, y, x, set())) for y, x in low_points))[-3:]))
47+
48+
if __name__ == "__main__":
49+
main()

2021/09/example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2199943210
2+
3987894921
3+
9856789892
4+
8767896789
5+
9899965678

2021/09/input

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
7988912498775677899976544676789212346793989896459876543679986778999879896543234965421014567898756757
2+
6777924987654686789987632345678954569894978789398765432456895569998965799765459876592923458987541246
3+
5456896987823545569876543566789995778999765678979986544578923459987894678976599999989899567987620134
4+
4345699876710123499987994677897989989998754567954397755989013498765953566989989998766678978998321345
5+
2235689765324648578999789989976768999987643678964219876789129579654212345898767987654568989876543459
6+
1024569879534787689998689898765656789643212567895324989899298998732101256789959876552456898998765668
7+
2137978998755698798987596789843245698775103456789765693998987899899912487899849865431545587899878979
8+
4346899659876899987656434695432167789874312569899876964987876789987894568989723987420123456789999989
9+
5657896542987899998543326479943278993965643678956989899876755799876795779876545696541344678996989999
10+
6788976321299967987632101267895389012396864989643498798765434598765689889987656987655456789865467899
11+
7899985410123459998843212348996498923987878995432987659878321987654898992398997898769767897654357898
12+
9979876653237598999954423456789987899798989789544598434987210398765987821039998929879878986543234986
13+
5668987954546987899985534668899976587679998678995679645696531239878976532129999212989999775444129875
14+
3456799875659876899876645789989897434599902456789798956987642449989876545298789423495497654321012954
15+
4677999996767985689987766899876795323989893487899987897898743658999988656987679934984398767443199743
16+
5789989987979876789299898999975689919979789678999876789909869767919899767987567899876209976564987621
17+
6899979999989998992199979898754599898667699789989545678919998978909765979876456789998212987676798510
18+
7998769897598679999988466789876698786544578999875533569398987899998954599765347899989323598787987421
19+
9878656789434567898976365679988987695433489998764321239497676899987895679874267999879946679899876532
20+
7765347996545698987654254598999976543212568969976443998974545798946796799842157898768899789944987543
21+
6673234789956799798762123767899989874323678957988769887563234587897897987652046989857678996432498667
22+
5410123567897895699543013459989998765435989348999898765432123456789979876543135678943589789321298798
23+
4321244789998934987676434678979999896546990245986987654321096567898765997765647899432423578932349899
24+
6532345998999326799898945889768799987667899366965398965439989778987754598978956895321012456944569935
25+
7643496987894315698949767999654678998789978979893219878998878999996432989989767976532123467895678923
26+
8789989996789504987656989798773239999993467898799101989997656789987599778997978976546234678987799212
27+
9899879985679423498767894659982099987812456987678919999986547892198987666896989987664345799999989903
28+
2998769864568945589978953545894989876543667898789998899876535989019896545799993499875456896789967894
29+
4987654953456896678989542123799876997864789999898976789987323569198765434878932346987667985699856789
30+
9876543212349789789199965345698965698965678999987965679893210378999767323567891234598778974598768999
31+
9987764101234678991019896459987654569876789788965754598765321289898653210379990123479899653459879998
32+
9999853213345789432199789767899932157989893567954323459898535696799864321245689294567939778567989986
33+
7998765435456898953987678998999893249897902467893212699987679895999978442467890989878949889679895354
34+
5679979545567987894596568989998789998776212379989301989699789924889989766579931979989999998798754213
35+
4343498758799876789987879878987656889654323569979999876529893212679999878689999868999989989899842101
36+
32 57AE 32349767987915679999998769898645778965434598765789765436989101567998989789987657899878978921943213
37+
4101234989875423467898764546789434569197545698764678976545678912345897694999996645678967967939876454
38+
3213445999995434898998753437678928789019678989912467998657799424556789543239865434567956756899989876
39+
4324557899976545699999542123569219678998789974326579349779897435678897654345986323478943445689391987
40+
5436898967987676789987541012789901567899898765987891298989986545679998767459875212357921534789210198
41+
6545989978999887899987432124567892457892949976798932397997897656792129879679954323569210123494321249
42+
7679978989989998999876573336678953767901234987899793976896999877891099989898765654678921234989934956
43+
9798867899978969899989876457789764889212346799978689875699899998932989999999878965699632349877899897
44+
8987656989767456789593987567999875999329987989767598986789678999545978929876989977896543659766789798
45+
7757645779854345995432097678999989998998998976553467987894579899999769019865699988987654599854347689
46+
6545434567943257896543998789998999997987899965432438998913689789887658998764569999998765987843235568
47+
7432123688954346789669899899876899875676789876510129999524598598766546789543488957899899876452123467
48+
6543334799765457899998789998965698763245678987542247896434597439854235699854567899989943995321036799
49+
8754455999879568999887678987654569654356789876543346789675986429863146798765679998768994987432145678
50+
9965667894998678998756569996543479875456799987654567898796975310954345679897789987656789876544287789
51+
9987878943459989989545467897695689976569898798967679969998976429865766789999898996545789998955398999
B41A
52+
8798989895698799879721256789789896987678999659798789954599986539876988896789956789434589219765456789
53+
7659395689987698765430145698999965499899998734569898893989697656987899965689345678923478909896667998
54+
8543234569876549876981234567998977345998989123456986789876569767898998764578956789014567896998978957
55+
7654109678989656999876545699997894239897878935769975678925459899999987653469987892123478945789989546
56+
8869298999598767975987656789876789129776767949898764599212399956798998532357898943234569435678998734
57+
9998987989439899864398967898985789298545456896999753479901989532987987620578999769357678924579987645
58+
9987675978921999872109879967454599987434345965987654567899878993986598321234899998998799314699698656
59+
8765423567890199943223989954323459886521239894598965679998756789765459543466789897999894323987579868
60+
9754313458921987654434599893012398765430456789969988789893344598764349874678994786898965545999456989
61+
8742102567899899876546999762124679965321345899856799899672223579653298765679563535567976659898968999
62+
7653212348988789997999899854345789986732456789735678987521012699862139979899432123459999798767899109
63+
9854323459976578989789789987467896597653468898623489996432359985989345989987541014598998987656789398
64+
8765434899865459876566678976578999439796589997544568987546598874695456794598672123997987897545678967
65+
9876646789654321995434589987689997929897699989659678998669986763498768893359987439876876989439999856
66+
9997797899995210987657991099999896899998789678998799879878985452349879921267896598965385678998779645
67+
8998998999889921298968993298899765689999894568999893656989876321245989542456987987854234567987656434
68+
7659789988677892989879589987698954569986953467897932347999988454346797656767898976542123457897542126
69+
6545679876566799878989679878567895698765432399976431349899876567857899777879979987643235578998631018
70+
5432398765455989766899789767458999789876591988997653498789998798767979888989765698954545678997632346
71+
4321298774344678954668998654345698999987989876989768987678999899878965999698913999875659789986543457
72+
9543987685123789543459876563234567899899878765878979876556789934989893234567899878986789998997654667
73+
7667898543234696432345998434145678998798765674567897687445789123499799013498998765497891237998876789
74+
8799999657345679321236987621012789987679974553456799543234591013987678934989987654398910956899997895
75+
9988998765456789910449998732334567897598763212345698987445989125696568945978999873219999899943459923
76+
9867899876587997892348999644645688935459754323457987896589978934987479899869345965423988798991292101
77+
9756799987998966789556798767856799322398767434598996987679867895699356798758996987554977657989989212
78+
6845789998989655678967899878997899901459878875699885998798756789898769997646889998699866545976878943
79+
5434677999876543489978978989298998892347989986988674899999348898989898785425677999987654439895657895
80+
1012346897998674568989569592129556789456796799876532789993201987878997654304556789875432129754345699
81+
2129578956798785679392398431012345997579985689965321679987512986767898743212347999986573098975212688
82+
329898924567989878910198754912445989999876456987943359899843 9A89 4975859999856423467898987654987654303567
83+
4987892134598989898924597698939598768899878698998944567899549864348989998644568997898865699965212456
84+
9876989012987665967896698987898698656789989987897956778989997955287678999757689456789976799864343567
85+
9765678924598574456998799756789798638998799986986899889679876543124567898768794347893299899875456878
86+
8954689634999432347899898945899909545999698765455798997598765432013478939989894236789345976996567989
87+
7653499549898921258789987834978919696789569654344567996439896563123599023598943145678967895987698999
88+
6532368998767892345678996521467898989893498743243456789545987879234789146987654235989879954599899549
89+
8743456789759789557899865410356987678954987652102567898756798995365693234598964347899989893434999698
90+
7656587997645678969998765321269876569865698843213678929877899876876895397699895456789398732129888967
91+
8767899987657899878979765433345988778978789956523799545988954987999976989988796568890297643498767456
92+
9878999898767999989568986544578999889989898765434999796999313399987987978979689979991298764989854321
93+
4989398799879889995467897655678933991296929876645678989876501239876899867464578899989999879878965452
94+
5794219654989678976345898766989321390965410989876989667988213398995679954343456789678878998765987674
95+
7984398763494567898656789879995490989894321297987899545599754977894569865212367896564767999954598895
96+
9875459982123456789878895998976989879789432996798978923498769865323498765423498998323556789863989976
97+
6996569971012358997999934987899876565678949875439569212459879973212549876434569657212346999872975498
98+
4987698763123457895432129896798765484579898764323459103456998764301234996545879543101867896989854329
99+
3498987654235767996321098765789994323456789871012348914577899878412346987656989754563778965498754312
100+
2109998767346878976543459434679876107689896532434567899789913987523457899767999986774689764349962101

0 commit comments

Comments
 (0)
0