Skip to content

Commit e945bd1

Browse files
committed
added 2021/09-15
1 parent 4dd8a0c commit e945bd1

30 files changed

+2607
-10
lines changed

2021/09/README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# 2021, Day 9: Smoke Basin
2+
3+
These caves seem to be [lava tubes](https://en.wikipedia.org/wiki/Lava_tube). Parts are even still volcanically active; small hydrothermal vents release smoke into the caves that slowly settles like rain.
4+
5+
If you can model how the smoke flows through the caves, you might be able to avoid it and be that much safer.
6+
7+
## Part 1
8+
9+
The submarine generates a heightmap of the floor of the nearby caves for you (your puzzle input).
10+
11+
Smoke flows to the lowest point of the area it's in. For example, consider the following heightmap:
12+
13+
2199943210
14+
3987894921
15+
9856789892
16+
8767896789
17+
9899965678
18+
19+
20+
Each number corresponds to the height of a particular location, where `9` is the highest and `0` is the lowest a location can be.
21+
22+
Your first goal is to find the _low points_ - the locations that are lower than any of its adjacent locations. Most locations have four adjacent locations (up, down, left, and right); locations on the edge or corner of the map have three or two adjacent locations, respectively. (Diagonal locations do not count as adjacent.)
23+
24+
In the above example, there are _four_ low points, all highlighted: two are in the first row (a `1` and a `0`), one is in the third row (a `5`), and one is in the bottom row (also a `5`). All other locations on the heightmap have some lower adjacent location, and so are not low points.
25+
26+
The _risk level_ of a low point is _1 plus its height_. In the above example, the risk levels of the low points are `2`, `1`, `6`, and `6`. The sum of the risk levels of all low points in the heightmap is therefore _`15`_.
27+
28+
Find all of the low points on your heightmap. _What is the sum of the risk levels of all low points on your heightmap?_
29+
30+
Your puzzle answer was `577`.
31+
32+
## Part 2
33+
34+
Next, you need to find the largest basins so you know what areas are most important to avoid.
35+
36+
A _basin_ is all locations that eventually flow downward to a single low point. Therefore, every low point has a basin, although some basins are very small. Locations of height `9` do not count as being in any basin, and all other locations will always be part of exactly one basin.
37+
38+
The _size_ of a basin is the number of locations within the basin, including the low point. The example above has four basins.
39+
40+
The top-left basin, size `3`. <br>
41+
The top-right basin, size `9`. <br>
42+
The middle basin, size `14`. <br>
43+
The bottom-right basin, size `9`.
44+
45+
Find the three largest basins and multiply their sizes together. In the above example, this is `9 * 14 * 9 = `_`1134`_.
46+
47+
_What do you get if you multiply together the sizes of the three largest basins?_
48+
49+
Your puzzle answer was `1069200`.
50+
51+
52+
## Solution Notes
53+
54+
For part 1, the task description already contains the implementation approach. For code golf, the usual "dictionary with complex numbers as keys" trick does the job admirably.
55+
56+
Part 2 adds an additional flood fill step to the mix, which I implemented by means of breadth-first search. After implementing that, I noticed that the BFS doesn't need to start at a low point; the whole idea of the flood fill is that the whole basin is filled, so the start location doesn't matter. Thus, instead of identifying low points first, a simple list of all open deeper-than-9 points in the map can be used.
57+
58+
* Part 1, Python: 185 bytes, <100 ms
59+
* Part 2, Python (using low point seeds): 328 bytes, <100 ms
60+
* Part 2, Python (using arbitrary seeds): 311 bytes, ~100 ms

2021/09/aoc2021_09_part1.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
E,M,Q=enumerate,{},lambda q:M.get(q,9)
2+
for y,l in E(open("input.txt")):
3+
for x,i in E(l.strip()):M[x+1j*y]=int(i)
4+
print sum(M[p]+1for p in M if M[p]<min(Q(p+1),Q(p-1),Q(p-1j),Q(p+1j)))

2021/09/aoc2021_09_part2.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
E,M,Q=enumerate,{},lambda q:M.get(q,9)
2+
for y,l in E(open("input.txt")):
3+
for x,i in E(l.strip()):M[x+1j*y]=int(i)
4+
def A(p):
5+
v,q={p},{p}
6+
while q:
7+
v|=q;q=set()
8+
for p in v:q|={x for x in(p+1,p-1,p+1j,p-1j)if Q(x)<9}-v
9+
return len(v)
10+
b=sorted(A(p)for p in M if M[p]<min(Q(p+1),Q(p-1),Q(p-1j),Q(p+1j)))
11+
print b[-3]*b[-2]*b[-1]

2021/09/aoc2021_09_part2_try2.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
E,M,Q=enumerate,{},lambda q:M.get(q,9)<9
2+
for y,l in E(open("input.txt")):
3+
for x,i in E(l.strip()):M[x+1j*y]=int(i)
4+
a,b={p for p in M if Q(p)},[]
5+
while a:
6+
p=a.pop();v,q={p},{p}
7+
while q:
8+
v|=q;q=set()
9+
for p in v:q|={x for x in(p+1,p-1,p+1j,p-1j)if Q(x)}-v
10+
b+=[len(v)];a-=v
11+
b.sort()
12+
print b[-3]*b[-2]*b[-1]

2021/09/input.txt

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
9987653234589654549865432101235789979765459832101234567943986543101239874321012932397319985214896543
2+
9898541015678943239876973876545699868954398765212345678932399654592345965432129891985498764323789432
3+
9765432126789654123998765987897789759893219894333656789543498986989957898543498789976579895454678943
4+
9879656737899797434589986798998897646789398989454789898959987899876899987654989678899699976565789656
5+
8998767945678996545678997999569965435678987678967897947898765698765678998799878466798989988676898777
6+
7679898998799987676789398998439876323999876567898976736569894329954569789987964345987878999788999888
7+
5569959349898799787892139987321987549898765456789765623478989497542345699876543239876567898999998999
8+
4398943245999659898993045996210499698759876345678954212349876989321234589876455123954453567899987897
9+
6997832134678945959889159895432349999898763234567894303456965878943445678954321019843212456789976995
10+
9876543265789434345678999789943498899987654126898985424579854567894578789986532129654325677899854789
11+
9987654378894321256789987687894997789976543245789876539798768978965989897898763298767534798998765699
12+
9998867459943210167899976576789876678898674689894988798999899989876999976987654349879545899999876789
13+
8989978967894345234989895434598985568789788799933299987988989999987899895498765767989656987899987890
14+
7679989878965976349876789323987654345699899899321012976567678998998998789329876878998767896789899951
15+
6567895999876765498765678912396543236899956978932129854323567897899989698934998989679878945698789543
16+
4367896989989896989824567893459874345789543569549299765434579945799877567895789997543989432397678954
17+
6456789678998989873213678994598765456795432678998989879765678936998765456789896789732394321234589899
18+
7867894569987678965302759789679876567894321799997878998976789029876744348978945699901298636346698778
19+
8998923459876567973212345698789997678965433899876768987897899998765432124567934989893397545458975667
20+
9769212598765459895463456789894398789877549998765656346789998769876545635688949879799498656867894356
21+
9854324987654346789654577899943239899988698999854341247898987653987678986799198765678999987978965457
22+
9985459876532235678968688987652124989998796598763210123567897632598789987891019654569998798989976768
23+
9876598754321014567999799999873015678999987679874331234898998543469893498989198543456989659497899879
24+
4987999865432323456789899998754123789687798798765765345789239754567912989678976432345678943236910989
25+
3999998978543476567896978899875234896545679969896889656890198765678909876549876321234567894345899894
26+
9899987987654587678965356789986346789639889345987992987999349989999999995434965430999698995456789763
27+
8789995398765698789643235894398956998798999239899893998978969999898989984329876999878989976568997654
28+
7679876219876789896542129901239899889897778998765789879569998998787878965510999876567678987879998795
29+
6569997901989891987843458913498788778976667897654678965456987997676567894323987955476567898989989989
30+
5498789892399932398954567894987654567895456789642567894235695987543478999499876545313456789899876567
31+
6987678789999543459987678959876543679932378996543456965123984399864569998989985432101239898798765456
32+
9876546679898954567999789546987621767891567789654587893239973298765678987879876543212345995697654367
33+
9965434569767895978999893234598832348932345698765698954398764569976789656568987654323476789987743256
34+
8954323698956789899998942195679953567893478999876799965499865678989896543459999866455678999976551045
35+
6561012987545698788987893989998769878987567896987899896989987889298985432198754986566789678985432136
36+
7432123498699987667896789878899989989987679975699989689678998994367965321096563497678994567986544357
37+
6543434679987893459945698766789994597899789654599876575569899765459876432985432398989323456987655456
38+
7654545689876789967897987655679913456789898765989975433456789876767989949874321019998910127898767667
39+
8965656798765667898998986543489894768998999899879874321387899989898998899865432125987891238989898798
40+
9878969899874356789999997432356789899567899999867965434458989999999987678976743434976989349876999899
41+
4989878998543235678989854321247895912456789998759876545789678949899998789987854589865878998785679978
42+
3497989987654146789679765432346954323457991987643987856894589439789769893498767679764567987654598767
43+
2356799998763259895434996566459899496567890987732298997893499998698954902999898798943458976543987656
44+
1345998759998768999323987878698798989698999896521019498902578896587893219899999987654569998651098945
45+
2349887646789878998909998989789657478989298765432329399312456789476789399789999898765878979732129857
46+
3498765434899989987898999999896545365679109876543498989423969893345995987678989769876989569843598767
47+
4599894323987899876667899899987621234568914997656987878944878932239894598547568978989895479955679878
48+
5689989210196998765456789789876210145679323498979766567895989210198795987832456899998794398766789989
49+
6799878923965449887567895698765421234789459989898753456789994345989689976541368954989689239877899999
50+
7899769999874335998789989899876532365678998876789866567899985459878599987430267893976578999988998989
51+
8998958989910126999895678916998645466989897665789877689939876598766469898521256799865456789799987879
52+
9987847768921237899934689105679656567898786544598999994324987987654349765432345987674347889659876567
53+
9876426457892357998925693223998769779987654323987887889212998998743234978545469876544235678998765458
54+
8765212346895468987998789339899878989876543105986455678929899999832123987676569998432123789989896345
55+
9893101287896778956789895498799989294987654219875334599998798987654016898987978987654234599868987456
56+
7654242398969889545678976987689990123499864329883223678998657998762145679398989398766345678956897678
57+
8765357899954995434569989876578921934987986598761019999999736799763234789239799219875458789345999789
58+
9876459987899894323678998875467899895976797987652128789989945689654756894345678998986567891239899899
59+
9987699896798789434789897654323456789985698976543234567968896798765897895656799997897678910198776999
60+
8998988765667678945896789876212345699996789987654345678959789989878998976767899976798989321987665678
61+
7999876554354588996945679877103456789869899898765458789345678969989329987878999865659796499895434789
62+
6789987432143456789439998765213467899953998769896569994234569658991012998989998764546679987654323890
63+
5679896541012345799598789874327878969892987654987678996545678967989129879099876543234567899795434891
64+
4598765432123569898987678985456789459789898743498789989756789879878998765146998732125698901989545789
65+
3759986543234678987656589876769898698657789992109899978997894998767899943237984321016989992978956897
66+
2349898757655989898743456989899999998745678989234998868789923499656798895349875432234678989867897896
67+
3498769898767995679754687894999899899987789878976987654689934987543236789767976543345689876546789965
68+
5957656999898994889898798943499789789998898769897998743476899876432125698978989954496898765435789954
69+
9843545989919989998989899954987679678969987658789876532545899976521034567899999795987999876323567893
70+
8732135678909878967879999899995564599357897645679987621236798765432145678978988689999899943213679954
71+
7643014789998765456568998788994323788969919876798768730345679896543267989569876578998798654323789875
72+
8654123898999654345457897656789515677896323987898654321656789987655358993459875467987698765434892996
73+
8766234567898743212345789534994323456965434598989765442767895698767467892398764345696549876545993989
74+
9874345678999832101234679423989434699799565989679877653458954349878878921987653234986539987656789878
75+
9965657899298743212345894212878945987678976976547989765669765689989989439876543123497698998767899764
76+
9876798921019654323456982104567899866567899867435699878878998799997699545998653234598987899889998943
77+
3989899892198767434569877323456998753467997654526789989989219989876578976789854447679996867995387891
78+
1298956789299878565678965434567897542346789643214567999894329878964347897899965557798765456894216789
79+
0987645898989989678789876545878965321578998759323456999765498767894256798949876678987654345892105899
80+
2698766987678997989898987856989954320789869898937599878978987856943145789432988989998743236789324678
81+
3499989898598965397997698967898765434899754997999987567899876645894235699921299096999984345679434567
82+
9988998765467893236789579989999887545678975986878976458998765434689346789890932125789965456796545678
83+
8976899754359992125679456799999999698789876795467891367999986323478956896799843234699878767899696899
84+
7895789973298989014568967898989439899994987984356910179898797212667897975689754348789989878998789999
85+
6434890989987878923699878987678921999893499878268899298789598601457898954579765678895699989349899678
86+
5423921298756867895789999896569890198789989862156788965678987542356789343467999789934989993210998569
87+
8737893989943458976799998765476789987679876545034567894589999753478993212979878992129878994921987678
88+
9656794979899569987999899654345679876543987632123478943499898764589432109898769994939769789892998989
89+
8767999868767989799898698765476789999432499843234569654986789878997643498789356989898654698789899194
90+
9879898657657895698795439876987899987654678954545678999875688989019759569653239876789543987698789012
91+
6998775432348894239679921997898978998767989876789789789654567899998967898764699865678952986567678929
92+
5987654321256789197598799998929867899898999987899897698973456789787978979978989976789769878454567898
93+
4598763210368898986487678999213456789949989998921954567892567998656899754989978987899879764323678986
94+
3459875391239897697396568789428589890129878999530123678943688974545799843499867898963989875214567895
95+
2345965485446797543212345678937678932398767985432654789764569763234899754998756789642199932108678914
96+
1459879876568999852101267899548789654987659876543465999875678954356999879877645678921019853219989103
97+
2345989987879998763254345678959898767899536987754576789986789765456895998763234599934329764324598912
98+
3589996598989899854395456789967999878998745699865679896597897987587894309854123578896449895434567893
99+
4678965409599798765986577899878986999899656789986789965498986987698963219767012456789567976545998954
100+
5789654312345679876897689910989754346798787891099899876899765698999654397654323767897689987656899865

0 commit comments

Comments
 (0)