Skip to content

Commit 624d94e

Browse files
committed
2022: Day 8
1 parent fc903b1 commit 624d94e

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed

2022/08/input.txt

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
332003300013411421030123310000325304355245416315402454123304251100551413414301042114403240340202013
2+
130120131034131214123330450045513234151453520504226501101432120555550112535000130014212133321332113
3+
221120234102441024240343113122535350650301140511165442365451334130334102412103112220300240240410033
4+
201003103341210433533532125351404201522621325222421563264456331504145322423000453224222123321220333
5+
031230441334332445422025052033156043024163224106103336113645142121364161131252032003041114411412021
6+
220020011034120131030421351112001560056302340102663363436621063030163253101311044513331310020121210
7+
303040332431011132533443553321623462145336356503166211061351546411261306522112352524223111304442113
8+
222232314244103121400531144211642025551410166524434251333155410020524252654152222025520123303142134
9+
042224141010553350340352020211461062016047171732434651455473401346405306461643421210440205441310240
10+
131414414351042410204054060152045335375171544547253174334561651556000661445663062351033351130040204
11+
421330222101314443436633206550451571441147331752243741723515751137750300263144202141521145051242430
12+
304413012541454453626212024316241455547757536354351542142777234173745662454551154032335331331021134
13+
210210020432025225546350564503461126533444677377311761441622516326645176426625622336441140312530032
14+
321331544225031511163062251235515725645116556552716447134611121422154147221206525245321405031141022
15+
132304244223434034644164163431246762324642345277558431347372614572744147525404511143400153134025230
16+
433014255554304626035066241262275264471542552344752837378423431364621367116254664210141202530542210
17+
341013013022206652606231157612322543268825577322777368835244322537337447537335222426156022255415144
18+
441525355153361333331104577746612725264233863646327752763365353845325222561423350253602520443533440
19+
331325510313351211266777763761227567864572264785467455456335374866514554722241446502256653043414352
20+
223021111542064104141467134673114548632465363887386464736623733276867135422463573365121002544203051
21+
234440212451036151623744516551447238632584877658432527675226444645478351316311314130326060612445003
22+
033044450411501311543214676112356456332628328268867775386867238847585733154276434130400024043455141
23+
205131331521440160315623445652882338268255429855577746736386265847343838432324621120436063113151534
24+
140402552431642627426711567572286656272365849859756599936647555338227372237714366515211422436232144
25+
200320133113652127237741127458347233869549439984447347799396896884254487755137323273723662433004102
26+
454424155234224212665443255248273448987696973685793654947967344658738363223344343225642564053032543
27+
204042231254546227227173383425466368946763933885435669749596368564787364536585763761277210314664203
28+
000413654454332177326723345672532779349633764356436834584654668495657884625378221164774505026425413
29+
104211300205311715275673636382227444595567898434345599648544779845678768388347314614742310641262315
30+
314445336034346211516832335578373876466544933499649884577395587843454938885463235516577345221004303
31+
312454541364371767524685754878934666577363898489498779648546864385644596874462483134516113200642424
32+
045303023541545211177774473328459887983569477675788545664497958663479946723273556551566745654414551
33+
315104421032435152386476237653439634759548869484955567445998495777843384344352472827152536033066153
34+
005541041351421165773462627984936336589775654486658558888479674838753737875276667512234774606631641
35+
342465056121667145785833876599887748648776987865445984558545644948958639677226254687646734301231545
36+
550456224133553143372382765354534539768845445646854596849896894999799849377256565255732636263050651
37+
252500660515557345284865448359656747876866486595579856584858769494858684374873544562312172202212334
38+
052616136615134615787657448653579745658966465855689959587489489794773794834525445883123525523333601
39+
252230540571447783264468947593555864557685577987986869757576675658486967664678685272326377531121454
40+
413163451427745626242667677845887769799976669995586696577985969684648578656883457628861571436025642
41+
533405114525476142726689645385596554758496765765598795556989875896575787974383873637675463761256614
42+
454326344554154642756573364987695594479865587966585689568895957855974975333548887427326446522203044
43+
216114153574223527762678337544477597569767966689859979656975697545485575737839438443886132712045365
44+
125156666622272632365556785997895855986558977786966869968767985974484866833363785734652313512061636
45+
322321415352243487443396957878886784758759777788777677655788869779499684595638364356883237212251446
46+
122013647613644872445494688665557499576659588698877977985696986649564754335394787334241464246322355
47+
605654146537634766832593985758685444759955657999799769877988857994746676947396585553386542761263544
48+
213653425317331334336886398546858769868586766969966669676559789578969885393464626654824257614332425
49+
660654431433636254452474779474874757675959989699776669987955967958545665666456488723857762667632624
50+
302316342766454766363278374838448478956968676979999687797875698557444688784488876226726637647532365
51+
636242311725575834473585863576767447899876977687688888867867959567798495435865837265222631673466406
52+
062235543254168266743397385348444696655777679668976789966989675764697759836733964552835613545462646
53+
640333544126212773487299459967465446887655668986886976666767658875567556473478954563422657411264042
54+
540625433215572383378434996869859464995669567997788969786675997787457759898479882426636752542310414
55+
445535166771723352824654693847865478896865796669698987668955589559967975639596952255564346215524205
56+
221425127235511746738568359348455498598575855997977899887788556776765485458747726328451547753230153
57+
214532604757232738234837789469967949479868968786789687989865889988588497583593337657226241341115620
58+
004244467622352758548665446367486478788877985697798995669999777644666456469933826356824242327113564
59+
266353354374115362785466543499745995759668555669555957566778696566987598783854846782725636654233143
60+
531631243311643228822426956378987478954886887766685959675789555656676595553996574356555472444216454
61+
140666423163211644345466589664758965767788585865675857896579844968677958746766768557511213772051231
62+
234421130317541287775845944673487775548876959858669796556656565784695558999352648657476544226554635
63+
412624633546563177676442768954956647579597689996578669686556674944546967458667638377245761312144410
64+
352621121571142172762553299483794557966844949979766996656658586465459334647545764377443526446306142
65+
006346322575555262872376536466933545779748779879878867649456994569974998665447554338732211431303113
66+
554334563237315531875257467895335488595964994888856875774884857555594969967225623822432356664611145
67+
451004264172434426643257788958894598687845878998475494658586487656436966346528627424414757464352412
68+
221050361362741524742282864394477367968877699857448774499568854837979743562628644665437676533622521
69+
355255324661136164174557438655596744798777687569864749968955695944564933443722484772514174556311265
70+
430065024241631523322743433843653488937876477676994649678667784553585674765334237555126451026263325
71+
235505456452451176728742366547446897648939846457559957659763984484633798325463455573553116236021123
72+
115234031514534736163577244834455659564966385864459866983788898754869654436588443511424124332535540
73+
351404033625617526367482264456275584336747746958739743757367759776843883862727732164231653501306242
74+
503055012541653215346262842272827896834747967447549377968493597455838645446365744773157220423462223
75+
553143615436255477475667832483727897549434843838855833978798965894468635542322233222537021036615034
76+
002222462560346626774546373548542854933338778555785475683957754684227337526515767775450220143425433
77+
131504260406323354665445316566825527286679849878744668534384467583656384425625441776212266506542432
78+
215054255514263012751367165784852227765689933769757763433568352646878327626173155737352644405300343
79+
552314225454222510753211174655762567463268427333439583897865725467264347257215367462301306160354104
80+
255302200521051251765326645715455744274263586466448448464856568583488367547737246743335131401512412
81+
101245322521566243614773147142427484788364664826554268425888478476726656461566577523262156415054432
82+
442223113356204112601267136315116673824647668456465767535574488583325342131741417460621414452243501
83+
030021515554410652516146332263743763547244733553874755783747688225247344423456213264214215420224550
84+
413231405342104505430605655136127652642884747258725854637357624386534551152676140060146342324553214
85+
043025432315263603012410724771723562544436732537283686754328266144444737731263642624006034433532321
86+
104334254513411453146423047163424563275165826658888562874364647322215523235751010535365201041550134
87+
300020553012410516650503127144151151366257317647264676757751532461215753567001666406110512441403243
88+
102330504341544132334643313664461577763552415276253277551421216256257366432140103125631532553012441
89+
133121400000310324062254440466672675621365462615365334463422371553612311060246343023025050324513003
90+
414433432204250221550404116102162474676751443756673535654632655537372506016342016305523520322020433
91+
300431421523212003523352645563411534764236144446653634512147234367106145251253516223042015230414132
92+
243312200244203131000464166042310223354312441365122363622347664211452666653252200441444455224101342
93+
234101331402523132440002366305604106515143667473324626357574660410555433104644155051000114411404322
94+
323231340213153413415545102230611245662526641323132514750054002302435343103551525112155333241003331
95+
011130141043235255230523325530536445442030625261123460622614140226632004323354541551014140002214411
96+
102244200240002203012052520545350235023153142054205355115153522126310220541122512230331031112130111
97+
013231143131433131124534302204532206566450455256355310122012430310255401351304225525542241003133010
98+
132200122232433121333410132153131541542513264261463154353021340403623114231305043110312440012243111
99+
001320030221212412130340411450015410126162032430413232552634423102222044153052245310344121344231122

2022/08/main.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import math
2+
3+
4+
class Day08:
5+
6+
def __init__(self):
7+
self.puzzle_input = self.load_input()
8+
self.scenic_score, self.visible_trees = self.find_trees()
9+
10+
def part_one(self) -> int:
11+
return self.visible_trees
12+
13+
def part_two(self) -> int:
14+
return self.scenic_score
15+
16+
def find_trees(self):
17+
visible_trees, scenic_score = 0, 0
18+
for x, row in enumerate(self.puzzle_input):
19+
for y, tree in enumerate(row):
20+
left, right, top, down = self.get_views(x=x, y=y, input=self.puzzle_input)
21+
22+
if (self.is_visible(tree=tree, input=left)
23+
or self.is_visible(tree=tree, input=right)
24+
or self.is_visible(tree=tree, input=top)
25+
or self.is_visible(tree=tree, input=down)):
26+
visible_trees += 1
27+
28+
viewing_distance = [
29+
self.get_viewing_distance(tree=tree, input=right),
30+
self.get_viewing_distance(tree=tree, input=reversed(left)),
31+
self.get_viewing_distance(tree=tree, input=reversed(top)),
32+
self.get_viewing_distance(tree=tree, input=down)
33+
]
34+
35+
if not self.is_edge(x=x, y=y, input=self.puzzle_input):
36+
score = math.prod(map(int, [x for x in viewing_distance if x > 0]))
37+
if score > scenic_score:
38+
scenic_score = score
39+
40+
return scenic_score, visible_trees
41+
42+
@staticmethod
43+
def get_views(x: int, y: int, input: list) -> tuple:
44+
left = input[x][:y]
45+
right = input[x][y+1:]
46+
column = [i[y] for i in input]
47+
top = column[:x]
48+
down = column[x+1:]
49+
50+
return (left, right, top, down)
51+
52+
@staticmethod
53+
def is_edge(x: int, y: int, input: list) -> bool:
54+
return x in [0, len(input[0]) - 1] and y in [0, len(input) - 1]
55+
56+
@staticmethod
57+
def is_visible(tree: int, input: list) -> bool:
58+
if all(i < tree for i in input):
59+
return True
60+
return False
61+
62+
@staticmethod
63+
def get_viewing_distance(tree: int, input: list) -> int:
64+
trees = 0
65+
for num in input:
66+
trees += 1
67+
if num >= tree:
68+
break
69+
return trees
70+
71+
@staticmethod
72+
def load_input() -> list:
73+
list_input = []
74+
with open("input.txt") as file:
75+
for line in file:
76+
list_input.append(line.rstrip())
77+
78+
return list_input
79+
80+
81+
day = Day08()
82+
print(f'Result part 1: {day.part_one()}')
83+
print(f'Result part 2: {day.part_two()}')

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ My Python Solutions to [Advent of Code](https://adventofcode.com/)
1111
|5 |Supply Stacks||| [🔗](2022/05/main.py) |
1212
|6 |Tuning Trouble||| [🔗](2022/06/main.py) |
1313
|7 |No Space Left On Device||| [🔗](2022/07/main.py) |
14+
|8 |Treetop Tree House||| [🔗](2022/08/main.py) |
1415

1516
## [2021](https://adventofcode.com/2021)
1617
| Day | Name | Part 1 | Part 2 | Code |

0 commit comments

Comments
 (0)