Merge branch 'master' of ssh://git.elnota.space:10222/daviddoji/Advent_of_code
This commit is contained in:
commit
50fc9acc80
158
src/Year_2021/P9.py
Normal file
158
src/Year_2021/P9.py
Normal file
@ -0,0 +1,158 @@
|
||||
# --- Day 9: Smoke Basin ---
|
||||
|
||||
# These caves seem to be lava tubes. Parts are even still volcanically active;
|
||||
# small hydrothermal vents release smoke into the caves that slowly settles
|
||||
# like rain.
|
||||
|
||||
# If you can model how the smoke flows through the caves, you might be able to
|
||||
# avoid it and be that much safer. The submarine generates a heightmap of the
|
||||
# floor of the nearby caves for you (your puzzle input).
|
||||
|
||||
# Smoke flows to the lowest point of the area it's in. For example, consider
|
||||
# the following heightmap:
|
||||
|
||||
# 2199943210
|
||||
# 3987894921
|
||||
# 9856789892
|
||||
# 8767896789
|
||||
# 9899965678
|
||||
|
||||
# Each number corresponds to the height of a particular location, where 9 is
|
||||
# the highest and 0 is the lowest a location can be.
|
||||
|
||||
# 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.)
|
||||
|
||||
# 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.
|
||||
|
||||
# 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.
|
||||
|
||||
# Find all of the low points on your heightmap. What is the sum of the risk
|
||||
# levels of all low points on your heightmap?
|
||||
|
||||
|
||||
from collections import defaultdict
|
||||
from math import prod
|
||||
|
||||
with open("files/P9.txt") as f:
|
||||
heightmap = [line for line in f.read().strip().split()]
|
||||
|
||||
heightmap_dic = defaultdict(int)
|
||||
for i, line in enumerate(heightmap):
|
||||
for j, number in enumerate(line):
|
||||
if number != "9":
|
||||
heightmap_dic[i, j] = int(number)
|
||||
|
||||
|
||||
def is_low(x, y, value):
|
||||
for neighbor in ((x - 1, y), (x + 1, y), (x, y + 1), (x, y - 1)):
|
||||
if value > heightmap_dic.get(neighbor, 9):
|
||||
break
|
||||
else:
|
||||
return True
|
||||
|
||||
|
||||
def part_1() -> None:
|
||||
res = 0
|
||||
for pos, value in heightmap_dic.items():
|
||||
if not is_low(*pos, value):
|
||||
continue
|
||||
res += value + 1
|
||||
|
||||
print(f"The sum of all low points is {res}")
|
||||
|
||||
|
||||
# --- Part Two ---
|
||||
|
||||
# Next, you need to find the largest basins so you know what areas are most
|
||||
# important to avoid.
|
||||
|
||||
# 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.
|
||||
|
||||
# The size of a basin is the number of locations within the basin, including
|
||||
# the low point. The example above has four basins.
|
||||
|
||||
# The top-left basin, size 3:
|
||||
|
||||
# 2199943210
|
||||
# 3987894921
|
||||
# 9856789892
|
||||
# 8767896789
|
||||
# 9899965678
|
||||
|
||||
# The top-right basin, size 9:
|
||||
|
||||
# 2199943210
|
||||
# 3987894921
|
||||
# 9856789892
|
||||
# 8767896789
|
||||
# 9899965678
|
||||
|
||||
# The middle basin, size 14:
|
||||
|
||||
# 2199943210
|
||||
# 3987894921
|
||||
# 9856789892
|
||||
# 8767896789
|
||||
# 9899965678
|
||||
|
||||
# The bottom-right basin, size 9:
|
||||
|
||||
# 2199943210
|
||||
# 3987894921
|
||||
# 9856789892
|
||||
# 8767896789
|
||||
# 9899965678
|
||||
|
||||
# Find the three largest basins and multiply their sizes together. In the above
|
||||
# example, this is 9 * 14 * 9 = 1134.
|
||||
|
||||
# What do you get if you multiply together the sizes of the three largest
|
||||
# basins?
|
||||
|
||||
|
||||
def bassin(
|
||||
queue: list[tuple[int, int]], is_visited: set[tuple[int, int]]
|
||||
) -> int:
|
||||
while queue:
|
||||
x, y = queue.pop(0)
|
||||
for neighbor in ((x - 1, y), (x + 1, y), (x, y + 1), (x, y - 1)):
|
||||
value = heightmap_dic.get(neighbor, 9)
|
||||
if (
|
||||
value == 9
|
||||
or neighbor in is_visited
|
||||
or heightmap_dic[(x, y)] > value
|
||||
):
|
||||
continue
|
||||
is_visited.add(neighbor)
|
||||
queue.append(neighbor)
|
||||
|
||||
return len(is_visited)
|
||||
|
||||
|
||||
def part_2() -> None:
|
||||
res = []
|
||||
for pos, value in heightmap_dic.items():
|
||||
if not is_low(*pos, value):
|
||||
continue
|
||||
res.append(bassin([pos], {pos}))
|
||||
|
||||
largest_basin = prod(sorted(res)[-3:])
|
||||
|
||||
print(f"The product of the three largest basins is {largest_basin}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
part_1()
|
||||
part_2()
|
100
src/Year_2021/files/P9.txt
Normal file
100
src/Year_2021/files/P9.txt
Normal file
@ -0,0 +1,100 @@
|
||||
9876543234679310943456798433456798998764321357921025689921987899896498799923491297654545679876212347
|
||||
6987675036678939874567987012567897899975532467892334567890996789789989679895989398743236789865101456
|
||||
5598983124589598765689765423678956789876543568943565678999765395679876598789678987654345998764313689
|
||||
4349994235695329976789998634678946789987987679757678989398653234598767459654567998767499899875424568
|
||||
3298987656795419899896459745989134568998998789968789699219772123987654398753456899989989689986576789
|
||||
2126598787896998788902349897991013457899999997989994567909765244598875697542346789798764593987897897
|
||||
1012349898999887567893956999543234967967988656799323459898954355699876989331238996659875691298998956
|
||||
4123467999598765456789897987654347899659876545678912398767896466789987979210349965545996789399659235
|
||||
3234598985439878567896789198795456789898765434189329987546789998994699867991467894534987899985432123
|
||||
4347899876567989678965994349987897899909996521034998675323597899123989659889578943023498999875321014
|
||||
5456987997689299989254789498798998999919989992129876543213456789239877545678989432145999998989432345
|
||||
6567896798792109892123569987679999998898767889234997854324597994399765434689996565236789987698544456
|
||||
7898945689899998763012458976597989987784345678949998985434989219987654323456899854345699996597655678
|
||||
8949239796958899954124567895435678996543234567897889876599878998698765435678998765496978989498966899
|
||||
9932198965346789867258789932124567987655123458976778987988769876569876646889019887989869979329987957
|
||||
9893987896234568954345898743245798998766234567894567899875457987421987756799934999876649868912398946
|
||||
8789876789195689875457999655466799679879845678913348987654346987630298867897895988965432946794459435
|
||||
7653245678989799876767899867578954568989658799101234999869234598541349998976789876894321987895568910
|
||||
9654134789678986988979989878989243456799767895312349898998945987676556789345698765789210398996678999
|
||||
8765245696567895399898979999599012367899898986423498776566899999887687990296987984694331239989989888
|
||||
9954346897379964219787767893478943479901999597434569653465678999998798921989896593789452398978998767
|
||||
0976557898298965398656456794569894998919589439765698542324599689929899439976789432599543987767999656
|
||||
2998678999997897987642347895698769867898478929876987321015689599845996598765678953498959876656897545
|
||||
9859789899886789998756468998789655456797569999989996432326796498656789987654567894997899965545998968
|
||||
7643996798654567949898979659897643245698979989994987543689895329868999998765678949876778964234899879
|
||||
5432345697543458957999989545998732124569998967943198964599943210979098969876899129875467996446789989
|
||||
8584557987654569767899993123989841034979987654599979989679985341989197854987989039654346789677899899
|
||||
7675678998765699898988932034976432149898998863278954393989876832698986543298968998765956799898998789
|
||||
8776789439897789909977794255987543298787899985369893212399989764567997432129456789979899989949987699
|
||||
9899899524989896429865689356898655398656799876456789105679899895678987521012359891398797778932397569
|
||||
9987978939878987898754578967898786499545892998968994323456789989989498432154467910987676567891986478
|
||||
9876568998767898999743679879969897987656901239879895544678999879897599543265998934977567456789765399
|
||||
7765459876546999898654798989456998998767893446989796665678998768789987654399899949765432345996996989
|
||||
6984345995437898759995987892399999439978998669995689776889997656667998969989789899986645567895789878
|
||||
5493234989425789647889876891987899321989998778934578987999876543459549998875569789998786688934899767
|
||||
4321049875414678936979965789976678910198989989323469998967987652368932987654414579989887999023987656
|
||||
5493959954323469324767894679864567891987878993212378999545698710456891099843203458976998942125998543
|
||||
7989898965434568913456893589653456789876567992105567894434789322567992129874212367895459993349879432
|
||||
8979767996576679102378921098732345898676456789213458943226798763456789298765673456789345989659765310
|
||||
9865656889677889293467892129641237899545345698924567899012999654567899349876654597891299978978987821
|
||||
8654345679898999989679953498432356789321234567897698978929898765678998956987875698910987567899398932
|
||||
7543234578929898878989769976544578996532356878998789567998769888789987897898986789321297478921239543
|
||||
5432143456919657667899898989757689987653479989019893478987847999898796789949997996548396567890198656
|
||||
8961012367898943456789987698768789998954568998929989569876435445989654678929898987657987678954239967
|
||||
7642123456976432367893297569899898769765679456998678978987321334678965799898769598767998999876349879
|
||||
8843234568965321245892195479901989859887894367899568989765410123457896893799654329878999899987456989
|
||||
9754545678976432496789989567899876543998999578965467999876923245569987932679954212989898789998567894
|
||||
9898758789876545789897678998967989862369998679754345699989874356998898921569896102398767678999978943
|
||||
9998767894988656896935569549459898973456899789643234987998765459876789932499789213988654589989899432
|
||||
8789978943299767965423478921298767895569964996532146986799878598765999893987689929876542679878798921
|
||||
9689989652129878987314567890989856789678953987844299875989989679754666789986567898986321299767687899
|
||||
6567897541012989796205679999976545679999654598765987654678998798673245678965438957895410987954576778
|
||||
4489995432123497654317895798765432459898767679876798763567899899542134589875312346689929996543134567
|
||||
3235789543235698785456954349876721248789878789989899874698999998753234696543201234567898987654235678
|
||||
2124678965346789896787893212987210187678989892198942976789998769876545987654415345698976798966547899
|
||||
1014589878456893987899954301297321234589899999977893988894987456998668998765623466989765429987858943
|
||||
2123578989569902398978975212976542475789789998756789199953986568989889679876734569879954312398969652
|
||||
3234567897698943469869865323987643567898699999547894349992197689679995566987655698767893202459878943
|
||||
4345978998997899598756998764598764678987569898769976998989999796598754324499878999859994312378989965
|
||||
5656899569866968965431279879689985789998498769878989876865778965469843212347989898948975459459999876
|
||||
6787932499754357896542456998789699899886329856989999765954567894345954353456798797837988678969878997
|
||||
7898953987643239919757567899897543998765498745899886644212678901234969754668997655126898789998868998
|
||||
8999654996544128929898978934998631349877899635789765432103789212349898975678986543235679892987657899
|
||||
9998969875431017999939989325698752356989998523489876643214897423598787896999697655356799921098545989
|
||||
9997978996652126789129893216799763467899896412678999765625996545987676569896598766587898943985432877
|
||||
9886899429863245679399789109999878978998765324569769889436789679876543456789439898698987899874321466
|
||||
8765678910964376899987698998784989989549995445678945996547899798965432387696429999789556789766440355
|
||||
7654569899875487999896587899543494399929987678799434987667934977994321234597998998995445678954321234
|
||||
6543456789989568998785476998932359239898998989893223699788999866789410165789896987654324479765535445
|
||||
5432567899987678997665365767899498998767999699932104567999987654789421256899765698985212356986787568
|
||||
4321256789999789886543214456798997987656789569543213469765498765678933345998754109876323567897898679
|
||||
5434345678998998765432102349987856798545993498994999578954329878989654658987653212987434878998929989
|
||||
6565656799567899876545214498986534987656789597789878989865912989498768767899864324598546789989939999
|
||||
7676768923456999987756725987995423898967896986678767993999894994239989878998975435987659896765798989
|
||||
8787879734567898998998999876789545789989954965483458912987789892129796989787896745898789975454447678
|
||||
9898989656789987889999987494899656789195899754312379909876556789097645692546999856789897654322334589
|
||||
8969398797996545978899976323678967991024789876106567899985434567998732101235678969897998854310123459
|
||||
7654249899975323456789985214567898942195678998217879999876524567986544212346989989976799965924265678
|
||||
8652135987976896568999953107998929769989899999356989998765213456987656434587896492455678999895696799
|
||||
9543299876989987689659864315789319898878989876467999989874301345699786547998954321234589987689989892
|
||||
7654987664698998796549874323498901997659878987578997779765432456789987856899967432355678996578678921
|
||||
9965799543567899987856975434567892987543656898989986569876753697897598767977898645698789975457568910
|
||||
9899898632356792198977896565678969876532345689995987432987884789966449878956789876899899764325457891
|
||||
6678997653458999299989979876799349865431334678953294321098765789654323989345999988967998955212345789
|
||||
4599298964578998988998968987891234986210123689964498753129877897654312993234789999654987742101234699
|
||||
3989129765679997667987899998910129876433234589876569876534988998973209894365678919869876543232348789
|
||||
2878939876799896543566999879321236997645345679987893997699999529994698765489799201978997654343469892
|
||||
3467899987986789432355698765432345698987456789298932398988965410989989876569895412399989765499598921
|
||||
5679979999895678921234569989543456789998987891019643599877896329879879999678976523989878979988987932
|
||||
6789568987784569990146678998656567899969898989998754988756899499764768998789897949876767989877656893
|
||||
9893499976543478989236789239767898998756789878899869876645798987653456799898789498765456799765346789
|
||||
6912987665421299879345678949879989976545698765789979865434687898432567895965689398754345678974235699
|
||||
5439876543210987769967899999989567895431987543695491984323456989943458954397899999665265667895127678
|
||||
6545997654521976458899910989996468986532398654599392395664677979894568965989959876543123456789024568
|
||||
7666798766439894346778929878987347897747498765678989987775899866789789999878943997651016567892123456
|
||||
8789899876598789234567898969876456798856569876789778998986798754989899989766959898764323456789236768
|
||||
9898999987697655139879987655987898949987899987894566989987986543478999876745898759877467897897345679
|
||||
8967998799798743016791098943498929956798999898913455678999876542359998765636789542976578949985498789
|
||||
7649876549899752145892987632349547897899498769101234589212987656767899443323498931987989539876569893
|
||||
8432987632999863236789876545678956789902349854213455678903498987898954321014567890198994321987689912
|
Loading…
x
Reference in New Issue
Block a user