Skip to content

Commit 36754ef

Browse files
committed
day08
1 parent 5eb80da commit 36754ef

File tree

3 files changed

+160
-1
lines changed

3 files changed

+160
-1
lines changed

2022/08/__init__.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
def cast_input(inputs: str):
2+
return [list(map(int, x)) for x in inputs.splitlines()]
3+
4+
5+
def sight(I: int, J: int, i: int, j: int, di: int, dj: int):
6+
'''
7+
top -> di,dj = -1, 0
8+
left -> di,dj = 0,-1
9+
bottom -> di,dj = 1, 0
10+
right -> di,dj = 0, 1
11+
'''
12+
steps = min([I, I - i, i + 1][di], [J, J - j, j + 1][dj])
13+
return ((i + s * di, j + s * dj) for s in range(steps))
14+
15+
16+
DIRECTIONS = [
17+
(-1, 0),
18+
(0, -1),
19+
(1, 0),
20+
(0, 1),
21+
]
22+
23+
24+
def part1(inputs: list[list[int]]):
25+
I, J = len(inputs), len(inputs[0])
26+
27+
def visible(i: int, j: int, di: int, dj: int):
28+
line = sight(I, J, i, j, di, dj)
29+
fi, fj = next(line)
30+
return all(inputs[curr_i][curr_j] < inputs[fi][fj] for curr_i, curr_j in line)
31+
32+
return sum(
33+
any(visible(i, j, *d) for d in DIRECTIONS) for i in range(I) for j in range(J)
34+
)
35+
36+
37+
def mul(it):
38+
result = 1
39+
for n in it:
40+
result *= n
41+
return result
42+
43+
44+
def part2(inputs: list[list[int]]):
45+
I, J = len(inputs), len(inputs[0])
46+
47+
def score(i: int, j: int, di: int, dj: int):
48+
line = sight(I, J, i, j, di, dj)
49+
fi, fj = next(line)
50+
result = 0
51+
for curr_i, curr_j in line:
52+
result += 1
53+
if inputs[curr_i][curr_j] >= inputs[fi][fj]:
54+
break
55+
return result
56+
57+
def scenic_score(i: int, j: int):
58+
return mul(score(i, j, *d) for d in DIRECTIONS)
59+
60+
return max(scenic_score(i, j) for i in range(I) for j in range(J))

2022/08/input

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
202002313322443443333413205214140320025450316364504141264123114304203303114001123204003420102030300
2+
212313000221124342025540453251212255520310035503014622512241404324240404402541331004400300331322110
3+
332010030224230340301531501004040246520352260460326354116252046344244524210334140401240030314221201
4+
221301221024131121450340303252214260131364111213343344405130626304123155314054543541311324344211033
5+
001134433000214135052445324455546624654355260523323125133253553045204251430400315121003433044422220
6+
110024340203302220432202030041005313231555605566115533505425534600532263042244310025013301002122432
7+
202142343241025110345415445624446445344113355202423663433331226040240361512310234150014422014312341
8+
120231221014040554044220640131325544631255541041127752253252103362402320335611055442052140412241043
9+
304101021100233012133444436554401661205535772414667667451754115102616366431662351045410402233002402
10+
230331042045503255333014364102251313262666434422256626256566764614412322001313140132320342244311432
11+
301141102400324512330561535320415044526674414642717654456164545365745044506116261040015244323140033
12+
231132220241110100324130130545143322166173563556616563372125554727356021654340613261035251251413332
13+
203320424451202552446214505356253135265527113533231423523554524551155161163143626222334550222001114
14+
024231523321503230631342521245372255435215736461556537371121121751317427505314306502002123010412034
15+
040145510232550020202124627124455516526155425234552673277347264467144563753114463520045522011145324
16+
240244304425550155132364624615433274255211852862645772235364532654142113137206602450504451232422342
17+
424334205511552415565560671715111772112734875278832283468554433426522451521742450535105641322532120
18+
133100401525326032650034477716317357844566548887884636586578835685745454237757622666646610535221104
19+
242223305304135213415343353725345243424627362843632335577356738268866222511635745254444064454403022
20+
331241503204144326221625132445526654437248248754447372872277553375762277344562444520505011503314140
21+
222451434543300006524657732724686755826462744865327773328272774372386881361277646413144100602223020
22+
414454544234262543464462244368365765473624458443347748378276353357284728521731472310150061145432122
23+
431102216135016433434413645583863866258756823459689676856722332224673767726144732654216036601312045
24+
110042056021302336337562427723268467778543586356696798849484555383634828423561624661541545036033040
25+
233130533366340424442262664763637476353997734438457636478474458384564333326465264262124602145421015
26+
241204063143034137555565335625773883567883575869635693564466395342787853372316311463254316554250312
27+
204021106434203474532574577732644238364483977987654549695578595965788345665673122334133343010255050
28+
545511454122526751271377583327826879494576898565775448864596738363442346635565474635455064243232543
29+
124015635625434375317238826384863483959359789986783666664869677434837343823842652356111546364644132
30+
354241210264473276647664584326653653647946783946785857466783387358894383428654661215717710324561130
31+
244056226450134737153283634386947648667343795757554497954898635566776763644735545173126352624425020
32+
501324141203722175363238762648494967439479987887696675878464444733446494872553246567237465053516212
33+
124454612231317724443537666649848658354577798467554676466685998895664436347585644634216747461605663
34+
444220351051321723632583255595773459868897594678655845666879757469695495456426765727653232001214613
35+
543415566433561456232786335548987337775874478855474645667479645683933635533375645234665367636633651
36+
530466162316434373545644848445489775499587455748589658584498988576664989538548655685716224502165333
37+
235553430155252418662326494855584476654765446847698998974986756847936484493638765583146163725434525
38+
310360654533722362728523488436493558969996756767575985755767774769466767793646674763166562576015621
39+
325614303744164255433237739477846644568969987655575799855985845775957857639672455744841244170500440
40+
503515002355336284762634697976487678966879556557679889675955589869487648536567234728851425445561434
41+
543324643653742448624433599753598966565767666598867958799856754678485656376459464724213745353256500
42+
424103217216134323283434554946357547857767597585675689859575974787896473853787835228441512611606640
43+
250304641276277558773624439786746445898556998895887655878685889784588785835399278825226257656316456
44+
345463264572334567387256787858445544645556959975787756996857575877564946576444487536853156173434515
45+
161602652143411253367838475869799955968676977879766978885597865889664759447954958773226534633601264
46+
425255315411463546634735936474878586798978997677669676798889865697568457553348834768551317414265641
47+
234033463437423662763283959368645469455555866987776986698877578774866488666969955245374145544441623
48+
263636217315313825577865757479668665967695777867766779768668996864488656989883458726646674514525511
49+
414022642352612738772698548548946555775959857777688888669975865965945954569559444566834371561235240
50+
626645264156227353287767567366856886676578966679977979667696756679988685477675656584254614315521534
51+
141252624567664677828886434838768857955979876666969698786698969754567989787396562527238135476552660
52+
561116074375644526333736555595544896786968966976977997766676677666986777499545768822354754143466332
53+
640134335336664367747687845787987597685679698978788978879896585767488999869854847362476731511356241
54+
122566575557672854685858389735665896958579588778989888987596889896554676975794558735663243216725024
55+
622222555471614528744598736564486874589976996698777696679756695597854959586596528866486767125750425
56+
420556462341434263425678855644549848459689687977998667968775668946499845947638767286571573445302316
57+
153401561272311464882769939493697496869895765798867978898769959646765788757935955564662266664350020
58+
013313231114626266432864594633565767798995787676787879588887569996649773766959635454884663212340545
59+
532215627116623645744769753668949665878576988558956966987866657689789897574757843566561641651164202
60+
450601415622125355356368488837847498867787786799888699666587767988546989739799625367647376766514525
61+
132355556713615268557775868585955849749679856695885666656767667454658976438866274426545173713503424
62+
140363023176115776535475859453935445956676569686687689879769756896678849374653357232443563250603624
63+
555455453332353643328265463674488487677659785966787969895954479496764396934557558828651662273322156
64+
126055665555676465455653358794473489784867699566988677887774657674865436594725534325162527364425454
65+
424236562661632716477544757996995899646765977676757796446899758789936969594733554244341314246106356
66+
135664342046737243753657865736877354784767579696499494585854598586968344888482585764775221150460432
67+
344344143357321137635487849484434336494875487675577984647444795955996398565666287683142755200412361
68+
414412353051761177373387236955658343565579456876599987585868476443368777592334237632432165500034134
69+
344222350343246157155255424535673748655865864986555696699778567479637584386522672344234517610314264
70+
303525035340142365235266633256899897484946659875878967575986439538577438532348255423232624315603544
71+
510360601501172132135462465243343789945984697644794559468647896656775633328382288156772775241356112
72+
244355065300035434315633638622366843368655487769664895769979663363655675357453742377443633663346142
73+
543050131342642155264665386866379467794949993697734675839557966585757563732683714633526145166334412
74+
251422612632162765477554485862386899688967865977755368655846774644737246723864225342434523316423234
75+
535030453166161574317736827787562575357946899856345696768476976499427585553774225613174414155545203
76+
451512340514125213652261633852837673947556656599369877579547445535876827567436531662350302444041243
77+
033335530105503441451253564883648443326876639459998839433543553388842558572361376415226141134602541
78+
304432111501100651754116177585225473385738444645866989935538477775342885231334255374502153460230350
79+
532031530144611101611317655656232366528772853465566773976378637453482452343331275146210626455403524
80+
012504432204304640225665641317778444385425328864724366378324622327743456125442217372022160305110234
81+
423330153504112303542265616531228643228676746426328835672356636538675673621573612560004436420330151
82+
323321500212164213037737677445413688473546378425227262668778538578545546734311666243650663254002451
83+
040305323234252401530453457637172235782628463783548274258737388837337454577642612225610462012021013
84+
014421203421445064446506474465661471783776826638336854777768476773252621522666325565433244545553213
85+
203353533234533642651045661415434436734475362377466258588352323543664136375224260403562354030401203
86+
430143224545323216313162327161435122244647775242765557228261267335671742531564542126211410421135434
87+
311115413443044633036265137463752661235223746112744444721551372635624713663424662444323552052513344
88+
134313534132001040304122015236726566356671331522244364434524166341154325353311501362040351444450020
89+
411143423205141252652033316260315365572263763132742674322315253667543711643300545065310242032324020
90+
144341145402305205020640440563232327377646232274132447222637451213446334050331022204312235341032244
91+
100112332452301131334340450350201034154563646746333747731225537677742552525011500010314510431132014
92+
221320130310004120220446050664152326055763773453162437516516576134605322403126405442152350241130304
93+
241101113220305530315466104556331100306117166126725673116524503062565103142063320533420154322010431
94+
300413413431025050053455545511501350201353245414366717162612025321566536123553051412314441133131101
95+
330231204004004415320523326541625211601352531640552445135123515524650251203003153345052023224223041
96+
323031413443333543540440514335332454342430446605433036305445612534333404345522552510512242414121433
97+
201012341123333315224534110123345050436205551211152343641503546320233130510552230421204123134014033
98+
030231013300430225134314421310156226302452500603162133325435662050366331135501505435404110101401101
99+
301302243133113000115553304435551505352615146115031661551161050412521420141400134222424010304012133

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Advent of code Python solutions and CLI tool.
1515
| Day🎄05 | Supply Stacks |||
1616
| Day🎄06 | Tuning Trouble |||
1717
| Day🎄07 | No Space Left On Device |||
18-
| Day🎄08 | | | |
18+
| Day🎄08 | Treetop Tree House | | |
1919
| Day🎄09 | | | |
2020
| Day🎄10 | | | |
2121
| Day🎄11 | | | |

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy