Skip to content

Commit 700a5ab

Browse files
solution: add 2021 day 9
1 parent 2640cec commit 700a5ab

File tree

3 files changed

+311
-0
lines changed

3 files changed

+311
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
9976557856799875679875642456989998767978931098989876587878999876565667896543210123567899876794310234
2+
8765432145789983989864321345978999954569532987679965426567899865423457899654329439698987665679442545
3+
9898553234567899898765432499767899895689549876567894312458998543212679998785698998999996543458953456
4+
8987668765679998769986753987656798799899698765456789101345697654323567899896797987899989542557894597
5+
7698778978895129954397864797543445678978939988367895212467899875534568999999986576789877410136789989
6+
7539989989943239865298985698432134799654329876238954324588921976646899889998765435698765321245695678
7+
4321398991294345976129876986553346999743219765359995435789890989887898767899754324569876434557894567
8+
9932357890989496987235999898764457898752102976998789756899789999999976556689865546894987678967942456
9+
7893459939878989998499989789898768987643244599896578997947567898899875434599986657975998989878921347
10+
6899978919965578999987875678999978998754395699789468998933458987798654323678998768976899293989842568
11+
5767899998764455899976534779899989329895989987685357999212347976539875435678929899987920191099754989
12+
4356899987643234789987413566789998912989879987531238994301656987645989596789434921098939989198769899
13+
3245689998432124568998302345678987893979769898750126789212797899787898989899865699999998878999898789
14+
4346897998543345689863212558789796999868757789942334599433458998999987778999976987899987657899987645
15+
6567976697654576797654323567896545987654645679643545678945579987899896569998989876999874545678998434
16+
7988965498865687899875434578987856998763238998759856789757899876798765378987898765689963234567894323
17+
8999654319976798967989565689498967899542127789898769898968989965669876567896439876799854355989995434
18+
9498765423498899656798779894329878998743235678999889967899877654554987678989321997898769866798789645
19+
4349876434599999348939889986445989999987345789998994457898766542443498799678934598969899877895678956
20+
3256987595989888999423994399578997879996568998987653216789654321012349896579949789345999989924569969
21+
4345698989865676789212349298999875457899689767499864434578975993153569965456898993234998999212479898
22+
9559899876543134694325498987899764345698797654398765675678999789234698543234567894399877787993998787
23+
7998942965421012349434997876798621234899999865239977788789987698946987652126989975989566546889876546
24+
6897891977632163768949876565689540135679998752145698899898776587799998763234991099877452335679985434
25+
5956789876543245689769954434578932348799989543234999954987654465688999954756789198765321025899974323
26+
3347979997659456789898743125567896556999878956399876743498943334567899975678998989876432134679765434
27+
1234567998767768999987659012389987669898767898989954342349832123479999876989546976987563245678976755
28+
3569678969889879898998798923568998798789659999969895201298753334567891987895439765987675656789988767
29+
4678989657996998787899987894567999979678945899756789212459896567978953998998598654399899777893299878
30+
6989298743235987656799986789789398768599434698645698924599987798989769869897698765478967989965459989
31+
7892019651019876545899875678990239843489321986534567895989998939599898756789999896569546599996598796
32+
8989198772124985434599989889789349652578999898423498999878989012399999646796899997678933498989697654
33+
9678949989234997523678992997678998543456789789212349898956578993989898731234679998989321987679798793
34+
3568931098949886212789751034567987654567898698901298797543499989876789820124567899999910199568999989
35+
8689543987898775102457942123457998865689965457992999697621989875465698931435789967989891398459999876
36+
8797699876987654323568943234567989976799954354989876543210178954324987892387991349877689987378989765
37+
9898987654599875464899754345789876988898890123976998679633267893219876789998910598765578996569876543
38+
3979998943456987596789965956899965499997689359895679798545456789439765676789321679654356987679987654
39+
4568989894569998687892979877919894345986478998654567987656567996598754355698945998543245699789999765
40+
5699876795678969788921989989109789499894351989743456798767678987987643234567899876542123479899987976
41+
6989965689789459899542498999998678987643210578932345679878789898998792123676899965432014567929876987
42+
9976864578994344989656987999876534987654592469321236789989899789329989012345789987843123458912965698
43+
8985923467890123478968976899987621298766784578910123499993998543219879193456891298954434568929894569
44+
7654312356921354567899865678998542379879897699321234568932987654598768989569910129865677679998643459
45+
7842101239892465679999654567999653457989989789432545699653998767697657978978923457978788989998764578
46+
6543214356789568799998732356899764568997679896543756789979899879898744567899944767989899899899865789
47+
7654525487897689899876420124678975789996431997679898898998799989999932978967899988999987678789977897
48+
8775686598979799998765431285789996799987549989989989957899678994598763799545988999129876567698998956
49+
9989887679865988999896542376798987899999698777899976546789569965699854678934567898999865466567899349
50+
5698998989534567998987664567987699988931987656789988723499439876989965799123698987689654315456791298
51+
4567899998676979887699775679999532167899894345678999834568921989879876893244569754598765402367893987
52+
5989989769989897654545986799898954358998765456789321946789910198765987894997689643459897514589999996
53+
6795678999998796543235697998767895469999876567898943497899891987654598999889789652667959776689898987
54+
7894569878997689632123998989656976979981987698967964598998789999743459987679997543489542987896797699
55+
8932398767586579743434899978945897898870198799459895799878689998432369876568898764678931099945689431
56+
9543987656323489854575698656934789987653239912349789987964567896543498967456789898799992998756797210
57+
8665796546212349965689987545895678999795356799498678986543456989654987652345891949898789899898965423
58+
9976987632101467896798554436789789439896987898987567965432345678969887431337990134987678789969876675
59+
5698999543232345697987432124592995423997999967896459876521256899998776210126789999876598689756989776
60+
3569898674346556789996549036693987549979323456789345987432768999987654321234598789989434568934595987
61+
8698798765456687899989698945789999798765444578993234896545679988798765438756789578998623457899694598
62+
9987659878767798999878987957897889999879556989654346798959899976549887659867895489876534669998989999
63+
5699845989979899298767896898986578998989669998765457899767999895326998767998965345997645978987767899
64+
4987631296989932109656355789975467987898798889976769979978998789435679878999879469998756789896646789
65+
3986542345698654998743234678954389876569897679899878967989877698945797989999998998999867898765434890
66+
4987653457789799775810124589643298765456986565678989556299956567896896599878987887899998929854324691
67+
5698767878892987654321238899955129892349876464899995432198843477897999323459765676998799013992013989
68+
9979878989991098775543646789876399989498765343487896546297652346969788912399834345899695324989329978
69+
8767989796789989987655757899987988678997654212346797757986543457956567893987321256789589439878998967
70+
7656896545678976598866869998999876512987654301237898868997654579544456789875452387893478998767987656
71+
9738998437899865329877978956798765433496543212578999978998785989432345678986563598954567969659876544
72+
7629689545678974310989989432349986595987854323456893989129896792101567899997654569767679854546985433
73+
5434578959899765451398996545689597689898965454578912494298987893243459998999867689978898763434599212
74+
6545789767953986532567987856789498798769976579789894999987998954956598787895979799989987662125988301
75+
7856892979892397645698998969892359989656797679896799898976569879897987676796989999699876543019876312
76+
8769921989799498776789569878999459876545798789945698787895456999769876585689999998567987653236995423
77+
9878932998688999987893499989698998765434989890199987656965345987656987434567899876439798765345986564
78+
8989549876567896898992989796587899987325679931987654545893239876545987323489910987649649976456798775
79+
7497657988456954559789876572456799875456789899996543236789129766434596544595439998798432989987899889
80+
6569878999567893245699798341248678999578998658789654127679297654323987965689598999897521299999998996
81+
7698989998688932134987653210234569878989876545699943234568979865434899878799987899995439988913987895
82+
9987699889799541035699654321234698767999985435567899545678956976645789989899876789987598877894996654
83+
9876597679899432123498765452345987656899876423456998757789349798756892193998765679398697656999875443
84+
3987986546998543234679876743456797645799989910287899768996298659977891012989324589219987647899954321
85+
2199977634987657845789998754567988534678999891235989879765498843298932229876434695456898729999765210
86+
3398965420199789657893198766678976323459998789345678989879987654129646547986565789667999835798754321
87+
9987896554239898768999019897789875212345987698969799596997698865439897656798779899989989545679865432
88+
8766799669999929879198998998898984303459876567998999435989539976545998767899889999998978957999877545
89+
9954678998789012989297897549957895212569865456897678949878920987676999878942999987987569998910987696
90+
8765789654699123499986789321245987823498754349976577898769891398987898989656789876643468999699998989
91+
9986898943488939698765698932359876437987653298765456789655789459598976499867898765432127896587899878
92+
9998956732367898998983567893467987545698732129954398998744568969329995354978999865321056789456798769
93+
9899542101456997997542456789579998957789821019894289987623458978910989212989899976435145699212977556
94+
8789653212568976987631345997698999998997932198743168976545667899999878901296789997543234578909865445
95+
7698765333457895798545456789997899899876544987654347897996778921987767892345789898654357678998754323
96+
6549896544679934679656768999876585799987865699765756789889889899876645789556798798765468999698768210
97+
5431987758989124569867989799975464689999976899879867895678996799865434678967987679987689996569989432
98+
3210198767891012679878995689864343457998987987989879924899345679876656989979897565699998989432399553
99+
4523459898942123789989987897653212346897598995498989215679234578988767993298769434569876578941498764
100+
6735568999643445678990198987543203567965439875357894326789146678999879754579854325678998459653459895
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import asyncio
2+
from asyncio import Queue
3+
from collections import defaultdict
4+
from typing import List, DefaultDict, Set
5+
6+
import math
7+
8+
from adventofcode.util.exceptions import SolutionNotFoundException
9+
from adventofcode.util.helpers import solution_timer
10+
from adventofcode.util.input_helpers import get_input_for_day
11+
12+
Position = tuple[int, int]
13+
CaveFloor = DefaultDict[Position, int]
14+
15+
16+
def parse_input(input_data: List[str]) -> tuple[CaveFloor, int, int]:
17+
cave_floor = defaultdict(lambda: 10)
18+
height = 0
19+
width = 0
20+
21+
for y, line in enumerate(input_data):
22+
for x, number in enumerate(line):
23+
cave_floor[(x, y)] = int(number)
24+
width = max(x, width)
25+
height = max(y, height)
26+
27+
return cave_floor, width, height
28+
29+
30+
def get_low_points(cave_floor: CaveFloor, width: int, height: int) -> List[Position]:
31+
positions: List[Position] = []
32+
33+
for y in range(height + 1):
34+
for x in range(width + 1):
35+
number = cave_floor[(x, y)]
36+
adjacent_values = [
37+
cave_floor[(x - 1, y)], cave_floor[(x + 1, y)], cave_floor[(x, y - 1)], cave_floor[(x, y + 1)]
38+
]
39+
40+
if all([number < value for value in adjacent_values]):
41+
positions.append((x, y))
42+
43+
return positions
44+
45+
46+
def calculate_basin(cave_floor: CaveFloor, position: Position, basin: Set[Position]) -> Set[Position]:
47+
x, y = position
48+
left, right, top, down = (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)
49+
50+
if cave_floor[left] < 9 and left not in basin:
51+
basin.add(left)
52+
basin = calculate_basin(cave_floor, left, basin)
53+
54+
if cave_floor[right] < 9 and right not in basin:
55+
basin.add(right)
56+
basin = calculate_basin(cave_floor, right, basin)
57+
58+
if cave_floor[top] < 9 and top not in basin:
59+
basin.add(top)
60+
basin = calculate_basin(cave_floor, top, basin)
61+
62+
if cave_floor[down] < 9 and down not in basin:
63+
basin.add(down)
64+
basin = calculate_basin(cave_floor, down, basin)
65+
66+
return basin
67+
68+
69+
def find_basin_size_product(cave_floor: CaveFloor, width: int, height: int) -> int:
70+
low_points = get_low_points(cave_floor, width, height)
71+
basin_sizes: List[int] = []
72+
73+
for point in low_points:
74+
basin: Set[Position] = set()
75+
basin.add(point)
76+
basin = calculate_basin(cave_floor, point, basin)
77+
basin_sizes.append(len(basin))
78+
79+
basin_sizes.sort()
80+
return math.prod(basin_sizes[-3:])
81+
82+
83+
async def calculate_basin_async(cave_floor: CaveFloor, position: Position) -> list[tuple[int, int]]:
84+
basin: Set[Position] = set()
85+
basin = calculate_basin(cave_floor, position, basin)
86+
return list(basin)
87+
88+
89+
async def find_basin_size_product_async(cave_floor: CaveFloor, width: int, height: int) -> int:
90+
low_points = get_low_points(cave_floor, width, height)
91+
position_queue: Queue[Position] = asyncio.Queue()
92+
tasks = []
93+
94+
for position in low_points:
95+
await position_queue.put(position)
96+
tasks.append(asyncio.create_task(calculate_basin_async(cave_floor, position)))
97+
98+
result = await asyncio.gather(*tasks)
99+
lengths = sorted([len(res) for res in result])
100+
return math.prod(lengths[-3:])
101+
102+
103+
def get_risk_level(cave_floor: CaveFloor, width: int, height: int) -> int:
104+
points: List[int] = []
105+
106+
for y in range(height + 1):
107+
for x in range(width + 1):
108+
number = cave_floor[(x, y)]
109+
adjacent_values = [
110+
cave_floor[(x - 1, y)], cave_floor[(x + 1, y)], cave_floor[(x, y - 1)], cave_floor[(x, y + 1)]
111+
]
112+
113+
if all([number < value for value in adjacent_values]):
114+
points.append(number + 1)
115+
116+
return sum(points)
117+
118+
119+
@solution_timer(2021, 9, 1)
120+
def part_one(input_data: List[str]):
121+
answer = get_risk_level(*parse_input(input_data))
122+
123+
if not answer:
124+
raise SolutionNotFoundException(2021, 9, 1)
125+
126+
return answer
127+
128+
129+
@solution_timer(2021, 9, 2)
130+
def part_two(input_data: List[str]):
131+
answer = find_basin_size_product(*parse_input(input_data))
132+
133+
if not answer:
134+
raise SolutionNotFoundException(2021, 9, 2)
135+
136+
return answer
137+
138+
139+
@solution_timer(2021, 9, 2, version='async')
140+
def part_two_async(input_data: List[str]):
141+
# answer = find_basin_size_product(*parse_input(input_data))
142+
answer = asyncio.run(find_basin_size_product_async(*parse_input(input_data)))
143+
144+
if not answer:
145+
raise SolutionNotFoundException(2021, 9, 2)
146+
147+
return answer
148+
149+
150+
if __name__ == '__main__':
151+
data = get_input_for_day(2021, 9)
152+
part_one(data)
153+
part_two(data)
154+
part_two_async(data)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import pytest
2+
3+
from adventofcode.year_2021.day_09_2021 import part_two, part_one, parse_input, get_risk_level, find_basin_size_product, \
4+
calculate_basin, get_low_points
5+
6+
test_input = [
7+
'2199943210',
8+
'3987894921',
9+
'9856789892',
10+
'8767896789',
11+
'9899965678',
12+
]
13+
14+
15+
def test_parse_input():
16+
cave_floor, width, height = parse_input(test_input)
17+
assert width == 9
18+
assert height == 4
19+
assert dict(cave_floor) == {
20+
(0, 0): 2, (0, 1): 3, (0, 2): 9, (0, 3): 8, (0, 4): 9, (1, 0): 1, (1, 1): 9, (1, 2): 8, (1, 3): 7, (1, 4): 8,
21+
(2, 0): 9, (2, 1): 8, (2, 2): 5, (2, 3): 6, (2, 4): 9, (3, 0): 9, (3, 1): 7, (3, 2): 6, (3, 3): 7, (3, 4): 9,
22+
(4, 0): 9, (4, 1): 8, (4, 2): 7, (4, 3): 8, (4, 4): 9, (5, 0): 4, (5, 1): 9, (5, 2): 8, (5, 3): 9, (5, 4): 6,
23+
(6, 0): 3, (6, 1): 4, (6, 2): 9, (6, 3): 6, (6, 4): 5, (7, 0): 2, (7, 1): 9, (7, 2): 8, (7, 3): 7, (7, 4): 6,
24+
(8, 0): 1, (8, 1): 2, (8, 2): 9, (8, 3): 8, (8, 4): 7, (9, 0): 0, (9, 1): 1, (9, 2): 2, (9, 3): 9, (9, 4): 8
25+
}
26+
27+
28+
def test_get_low_points():
29+
assert get_low_points(*parse_input(test_input)) == [(1, 0), (9, 0), (2, 2), (6, 4)]
30+
31+
32+
def test_get_risk_level():
33+
assert get_risk_level(*parse_input(test_input)) == 15
34+
35+
36+
@pytest.mark.parametrize(['position', 'expected'], [
37+
((1, 0), 3),
38+
((9, 0), 9),
39+
((2, 2), 14),
40+
((6, 4), 9),
41+
])
42+
def test_calculate_basin(position, expected):
43+
cave_floor, width, height = parse_input(test_input)
44+
basin = set()
45+
assert len(calculate_basin(cave_floor, position, basin)) == expected
46+
47+
48+
def test_find_basin_size_product():
49+
assert find_basin_size_product(*parse_input(test_input)) == 1134
50+
51+
52+
def test_part_one():
53+
assert part_one(test_input) == 15
54+
55+
56+
def test_part_two():
57+
assert part_two(test_input) == 1134

0 commit comments

Comments
 (0)