This commit is contained in:
a 2021-12-13 01:11:38 -06:00
parent 9eb6a0f2c1
commit 1313b8cb7e
8 changed files with 272 additions and 0 deletions

69
2021/09/09.py Normal file
View File

@ -0,0 +1,69 @@
hmap = {}
with open("input.txt") as f:
lines = f.read().split('\n')
height = len(lines)
width = len(lines[0])
for i, line in enumerate(lines):
for j, n in enumerate(line):
hmap[(i,j)] = int(n)
def adjacent(i,j):
adjacent = [
(i-1,j),
(i+1,j),
(i,j-1),
(i,j+1)
]
return [ (i,j) for (i,j) in adjacent if (-1 < i < height) and (-1 < j < width) ]
# part 1
lowpoints = []
for i in range(height):
for j in range(width):
current = hmap[(i,j)]
neighbors = [hmap[(x,y)] for (x,y) in adjacent(i,j)]
if current < min(neighbors):
lowpoints.append(current)
risk = 0
for h in lowpoints:
risk += (h + 1)
print(risk)
# part 2
basins = []
allpoints = set(
[ (i,j)
for i in range(height)
for j in range(width)
]
)
def which_basin(i,j,hmap):
"""
Identifies the basin that (i,j) is part of.
"""
basin = set()
visited = set()
q = [(i,j)]
while q:
(x, y) = q.pop()
if (x,y) not in visited:
visited.add((x,y))
if hmap[(x,y)] != 9:
basin.add((x,y))
neighbors = []
q += [(m,n) for (m,n) in adjacent(x,y) if (m,n) not in visited]
return basin
identified = set()
for i in range(height):
for j in range(width):
if (i,j) not in identified:
basin = which_basin(i,j,hmap)
for point in basin:
identified.add(point)
basins.append(basin)
three_biggest_basins = sorted([len(basin) for basin in basins], reverse=True)[0:3]
from math import prod
part2 = prod(three_biggest_basins)
print(part2)

100
2021/09/input.txt Normal file
View File

@ -0,0 +1,100 @@
9874321292198975458901239986401245679543234590129878943236789999863345679876578901978456778910976423
9965434789987854346893498793212357898753125789998767954345789987651266799765457899854234567899997910
9876545695986543234989987654323569999654247999898757895456793499540656989755348999765195678978999891
5988856894597654345678999876435678998765348998789546796569894568932349876543257998976989989569998789
6799768943498785656789456987545989579876456789689235689878965679899499986432167577989878993468987678
7999879894679896777892349987656993467987598996563124598989876799767989997621012356798767892389898465
9899998796789987888921298798867894678998789876432012457899987987655678998543143456789659910198764343
5789997689891298999210987689988965789469898765432123678989999876543568987654554567998799791999953212
4579886578910989434991396578999879894398979876543267989567789998712456999875665678929898679899999923
6797675487899876526889987467899989999987654998665348995434678939901267899988776899019976535678987899
8965452346789865435679876578999894698996543239765467894324579129894345678999988992198765324579876798
9894321257894976576789987889698743987989992123986567975434598999789966989129999789349896545698654567
6789432345995987987999999996569654596577889965987678996945997876567897891098945689467987657976543456
5678943489989998998999898995459875987465679896799789879899876743456789932987334999978998798989942465
4599976568978989999998767889310989876323568789989898965789985432579899949996229879899869939399861254
7678997679767869899987656778921299975212345679765977896895796431298989898765109768789654321298750123
8799398989756556799786545568942349954326656789763256789954989910987676789843297655678969572359842354
9892129996545445988654335459959498765545767899854345898769878899886545998764987643789998765498756878
3932099987632339876543212367898999876787878998765656789899765798765434899979843212456999879699867889
2393989998741024987543101234567899987899989659878767893987654597654323789998732104767898998789978998
1989878999843266898754215345788969998930296545989979992098543129973212679876543212378987679992989456
9876767998764356999865326456799457999321989432599898789198762098765323567999698345679996565894599568
8765659899875667899987547567892398998949878921398779564989654599896464778998796456789876434789798999
8954345799986878989798658789943456987894567892987654453578966789987975689999987967894989545678987989
7543234689498989877659879999654667976789779999876543212346987894398986799989598978943498956989876567
5432129795239998767545990198765678985678989998987662103567899999459997898765439989912987967898965475
6554578894346899843235689239876789876789697897998743215678912398967998999875323499899986898987893234
7685678965456987656123579345987899998993466976899764376789904987899899789996784789798765989656999645
9796789876767999764334989956798999659201245998939878988897899896789653678987895679679989878537898997
9898998989878998765445996898989998995313359899910989999976798765678932489899998789543298767423957989
3959987693989989876567895789978987989924599789892997896545987654687893599767999896532129854212345678
2348799432397879997678964598767576567895978656789765987659976743556789678956799974321045965102389789
0147698921236568998989532349754321389999767545699986798798765432345678989347899865432129873216578995
1234597990123459219297543498765410499997656434987897899979987631496789690157899976559998765323679343
2349986789245569109109694589879531459876542123456789999762198542387899532378989987698759865445789212
3498965678976678998999887678998952378997683012567890198951019653498978943489678998797643986776789101
4987654549797789456789998789987967456789794324778932987943434966579769894596559879898759997887893212
5698643235679896567899989899876798767898765446989993986799659899789656789689349765929898999998985543
6987654456998997678999978989765989898959898767897989985678969768997647898789234954310987894319976754
7898765769897898899998765678954578989431939878956879874789498756795438999892129865499876789423987976
8999879898756789910987654567893465678910123989543768953499319897976656796931012976987654597975699987
9998989987645699991296423458921234567891234599532359767988998969988768895432323987899543456797893499
9997698765436987889999101568953345798932359698763579899876797655399879996654435699998932345689932349
9876579876545696877898242679854656799985498987654578999754986543212989989766545998767891234579321098
9775454987679985656987656898765767899876997899786799999643497654523599978987699897658799545678932987
8654323498798974345798987999976898954989876899999899878932198976654569867899989654347678968799899876
6543212349987661256999098998989999743299987988899989768921019899767698754568979792124567899896799765
7684102478999543569892129897991987654368999876789878959432398798998979843979757989235678998975678976
8878523567987654799789246796510199875456798765198766698943497656789567959897646879345789597643467897
9865439798998786987678957895423469976788899854097954577894989899993469869765434567976898489932345699
6976598989459897976568978976534567897899998762196543456789878978932346979887123578987897567896556789
5987987978969989875459899987676699989998999973987654567899955767891237898765012356899998989997678998
4699876567898776984345789998787988878987998765698765678998643456899998999654323589999989794649789026
3498765456799665695656898999899867569876649976799876789987654667978789998765537678989976543239893245
2239875349896543469868976989998754398764534989890987997699768778965678999877645789667899694398999656
1099874235789662356989765678987654239973223498921298986543979899354567895988789897545798989987698787
2987663124578954967999854579998766345985104567892349897932989943213678934599996936435987879543459898
3976543023679869899898743498989897459876715779954556789893497654101289025698654321029976768932346999
9865432146789998789797654987678989968987896898767967896789589864262568934989896543198765457891097989
9876563237899987698689765976565679899998997999979878985679678987653478949876987654239985376789989674
5988678945999876576578996985434598778999698987989989764568989698754567899545799865349873265679976543
4399989856789986432456789543129987669899899996592099543456996549765678978426578976598762123457897632
3297798767899987964589899653298976545799998789439978432349897639876989764212376987679854348967898544
9975689899978998875678978969987895434598998677998764321298789321987899543101345698889975457898999656
8764789934569899876789467998976789321987797566899954210965678939998998654213456899999876567899789767
9643489946698797997892348987675699490196542365789854399899789998999897654338767998754988678925678979
8776567897997676798951257896544568989984321234698769988678991987898798965445698999543298789434567899
9887879999876545679432345986432345678976542345799998776589890976765679896676789989694349897547698998
6998999989987858989543456796543456789987653456967897655458789865454598779788895878989459987698789476
4239998975498767897654568987994579896498764878943498432345678986323989568999934567979598998789892345
5345987654329878969795979099876689965349876989432987651234789875459876479989423989867987989899954756
5467898743210989978976789123997791094234997896521098862345699986599765345678964698754346678999865677
6568929854332397899987898236789892989375698997432129877657789997989876234599879989543234598987987789
7679019965465456794598997545899919879986999989643334998779996899878987945789998767434123457896598892
8989998987698767893459989656898939767899898879965455799892025698767998986898987654321012356789439993
9797897698799988976569878967897898656789767767896566893943934599748799998967899976542153467899546789
5556986439999799897898769898956984345697656856789677892969895987635689869456789987654767578998958893
4349987746997689789997657799439876767987643447899789999898789997546789954347894398786777679997899932
4298987659876545678976546689323998998988532138967992398765678999869899875956901239898898789876689321
9987598767989756789683634578939889319875421012347890239854789998978954989898913398999989897664598910
8976459889899867897542123456998778929876732123456789398753678987989763098789994987999878976543767891
7984341996789978965432012359894567999998543234687893987654567896799862198697889996789767989432349943
6543210345678989997543143598763456789497654356798901999865678965679654297576678945678959876543998964
8765431256789899987656264598654567892398767467899219865976789954778976986464569123799643988999897895
9996932345698798999894347698765678901299978578989397654989899853567897985323678934895432499987656989
9889893476789577899975498979878789312987989989679986543296998762456789875412357895996591991298545678
9765789687895456789989569656989897993976593096578997410145699851267899965101345976789989789987656789
9896999798954345878999678948997976889998432123479994321234987540348999754212468897997875679898787898
9987979899999234569898789139986565678987643234567989932355699631259398767323456789966764598769898957
8998967999887956779789894398765454568998754645679878895456798743459219975456567898755323987654929346
7899543597676799898657999999654323456799876896998768789578899854598934989567778999544212598732101245
6789432976545678987545298898743212347913987939876456699789967967987895698779899989432103496543212356
5678943987435899998932187799993201237895698924954334578994359879896789789989999978999312987695323467
3567899876596910976543345698789513456987789939890123689965212996785678996490198969988924598987834678
2778943989989921297655456898689434589998999899789235789754329985634579987321987858877895989898945689
3469642498979892398986987996578995678919988689699946899866598764323789996469976646566789977659867792
6578931987656789989497899975434989789109876534587897896977899876545678987998765431345699867345978893
7989649896648799875398989764325678997219765423466798954989965998656789599869896542476789643236989954
8999798765536689953239579875537889765329899312345899653297543569767894469953987656789898754547897995
9212987654324567892123457986756999976434987501234999867898321349898902398654599877899999865679976789

5
2021/09/sample.txt Normal file
View File

@ -0,0 +1,5 @@
2199943210
3987894921
9856789892
8767896789
9899965678

42
2021/12/12.py Normal file
View File

@ -0,0 +1,42 @@
adj = {}
with open("input.txt") as f:
for line in f.read().split('\n'):
[node1, node2] = line.split('-')
if node1 not in adj:
adj[node1] = set()
if node2 not in adj:
adj[node2] = set()
if node1 in adj and node2 in adj:
adj[node1].add(node2)
adj[node2].add(node1)
search = [['start']]
paths = []
while search:
path = search.pop()
current = path[-1]
for cave in adj[current]:
if cave == 'end':
paths.append(path+[cave])
elif cave.isupper() or cave not in path:
search.append(path+[cave])
print(len(paths))
search = [(['start'], True)]
paths = []
while search:
path, flag = search.pop()
current = path[-1]
for cave in adj[current]:
if cave == 'end':
paths.append(path+[cave])
elif cave == 'start':
continue # skip this case bc we can't go back
elif cave.isupper() or cave not in path: # big cave or unvisited,
search.append((path+[cave],flag)) # so explore further
elif flag: # we still haven't visited a cave twice,
search.append((path+[cave],False)) # so go there and explore, but remember we've used our exception
print(len(paths))

21
2021/12/input.txt Normal file
View File

@ -0,0 +1,21 @@
KF-sr
OO-vy
start-FP
FP-end
vy-mi
vy-KF
vy-na
start-sr
FP-lh
sr-FP
na-FP
end-KF
na-mi
lh-KF
end-lh
na-start
wp-KF
mi-KF
vy-sr
vy-lh
sr-mi

7
2021/12/sample1.txt Normal file
View File

@ -0,0 +1,7 @@
start-A
start-b
A-c
A-b
b-d
A-end
b-end

10
2021/12/sample2.txt Normal file
View File

@ -0,0 +1,10 @@
dc-end
HN-start
start-kj
dc-start
dc-HN
LN-dc
HN-end
kj-sa
kj-HN
kj-dc

18
2021/12/sample3.txt Normal file
View File

@ -0,0 +1,18 @@
fs-end
he-DX
fs-he
start-DX
pj-DX
end-zg
zg-sl
zg-pj
pj-he
RW-he
fs-DX
pj-RW
zg-RW
start-pj
he-WI
zg-he
pj-fs
start-RW