Browse Source

Day 8

master
Noëlle Anthony 5 months ago
parent
commit
464d4dd5d8
2 changed files with 209 additions and 0 deletions
  1. 110
    0
      day08-1.py
  2. 99
    0
      day08.input

+ 110
- 0
day08-1.py View File

@@ -0,0 +1,110 @@
MAP = []
DEBUG = False

class Tree:
def __init__(self, x_pos, y_pos, height):
self.x = x_pos
self.y = y_pos
self.height = height
self.view_up = 0
self.view_down = 0
self.view_left = 0
self.view_right = 0

def check_left(self):
if DEBUG:
print("### Checking left ###")
trees = MAP[self.y][:self.x]
tallest = 0
distance = 0
tallest_distance = 0
for tree in trees[::-1]:
distance += 1
if DEBUG:
print(f"{self.height=}, {tree=}, {distance=}, {tallest=}, {tallest_distance=}")
if tree >= self.height:
tallest_distance = distance
break
if tree > tallest:
tallest = tree
tallest_distance = distance
self.view_left = tallest_distance
def check_right(self):
if DEBUG:
print("### Checking right ###")
trees = MAP[self.y][self.x+1:]
tallest, distance, tallest_distance = 0, 0, 0
for tree in trees:
distance += 1
if DEBUG:
print(f"{self.height=}, {tree=}, {distance=}, {tallest=}, {tallest_distance=}")
if tree >= self.height:
tallest_distance = distance
break
if tree > tallest:
tallest = tree
tallest_distance = distance
self.view_right = tallest_distance

def check_up(self):
if DEBUG:
print("### Checking up ###")
trees = [row[self.x] for row in MAP[:self.y]]
tallest, distance, tallest_distance = 0, 0, 0
for tree in trees[::-1]:
distance += 1
if DEBUG:
print(f"{self.height=}, {tree=}, {distance=}, {tallest=}, {tallest_distance=}")
if tree >= self.height:
tallest_distance = distance
break
if tree > tallest:
tallest = tree
tallest_distance = distance
self.view_up = tallest_distance

def check_down(self):
if DEBUG:
print("### Checking down ###")
trees = [row[self.x] for row in MAP[self.y+1:]]
tallest, distance, tallest_distance = 0, 0, 0
for tree in trees:
distance += 1
if DEBUG:
print(f"{self.height=}, {tree=}, {distance=}, {tallest=}, {tallest_distance=}")
if tree >= self.height:
tallest_distance = distance
break
if tree > tallest:
tallest = tree
tallest_distance = distance
self.view_down = tallest_distance

@property
def scenic_score(self):
self.check_down()
self.check_up()
self.check_left()
self.check_right()
return self.view_down + self.view_up + self.view_left + self.view_right

def main():
global MAP, DEBUG
with open("day08.input", "r") as file:
lines = [line.strip() for line in file.readlines()]
MAP = [[int(char) for char in line] for line in lines]

trees = []
for y, line in enumerate(MAP):
for x, tree in enumerate(line):
trees.append(Tree(x, y, tree))
max_score = max(trees, key=lambda t: t.scenic_score)
# max_score = Tree(67, 38, MAP[38][67])
# DEBUG = True
print(max_score.scenic_score)
print(max_score.__dict__)

if __name__ == "__main__":
main()

+ 99
- 0
day08.input View File

@@ -0,0 +1,99 @@
122102002221220113213120331300324403204320144533424135412222404323003104143241322330203332311001201
122101021201100333103013020111134433225413441345223355455242454420324041424144143320331213313011211
002020102202133322040121034302303042143134335334145322255225234524340004204432223232112102301322020
220012312123310100132111431130112432541411111551552451554212423331432121222413114341113030121331012
201002212201312002313134042122454351541442441552442121522445315151245342234433410040022122332121102
022032110101034413122002140132134344355151432255122144512123321252231413241444212332310003201030321
202221121312021412211244435533212115121444135555133441115332134551132345451112300141040402100300202
012312220202241130000011113153354512345554455246352624652351411552522235252320220001241023300202002
012333201303122341124245121245445543543226522355634323552563215553344215343251121231221244211102101
321301231011002302232325451215424531566442635526336536256234645441454222315444511024423430030333330
133003322422223002304115312425243455525232535565226423436454233333321535512211142403210421302020033
101133010023010323543533152214433423352362654562322325233322536523633121145214413250132044411100133
320221132334300334142255424225345445666343362342664556562225525336544242242512134411121411023431302
220303334444424011351432244365435464622533322535533646426655434625632334622324221515223224200440022
221304441214044232254431226254626563623435252665353462634363535462254534545515141433333003012101220
232021444421003153155555563266552625654464556746467434364574426222364436522543214355422403000441033
012201023103313335534141242342266633235333737773456366635754757353643562353464411522131503110214013
001312341320422254534224254526463545477565754347565653364577576546344354353323444331444231021332302
122314301442525352324344444352624253544367643445553345537646357736556342425322323521153132114042013
131322304413521551452533624455557357566464547433467466564456657667357636364246536151545131243120104
101132340252151355245364224544646644673755576656444335747744436346774662556234254213342313124344420
130103214431542422644566665465754735364345635355473343456557534564534635262346623535423523550344041
330034234123153543264362365445456574446667446477765454656433537477573634363523253455433535354111002
204234335312443233246566554734755677764544558775884567857655576575577433632344636352213523414341223
044030434221432326234443327755374553678585776744775578465877876735477453377634335434323551354532034
114130014233253356656333357773543354874657857588588876787764664453673464577465555655421532452423003
041034312535533436645454347453545467746785575488446587668787558887463467345435652322365453343142313
333023313452134226454456635364764574886675646587758884567648486854576437434736652646564143145413342
134105212414452636233646446677654655547478864656677574578784868445788574536575756534346424211354320
004111511452463433623563777743754767457886788586559968978657668848857455547475664223622231451253210
231425442312332453557755476636755467675688856998977765968574677675455785675433663225554531115333201
124445531132562536325476335364456677445588998868757697666786985857544675564437635334353523541435112
002314442116354466363644545675644678689967785786789798578758958685655484644676347435562536232245230
400145131452336525274473557654464558676985658789768977865877655664657458663373374764564455354413541
002141524435534543377634638676587467985995589687759588896599677884847786856467363442555235244313233
211312353532635354377353674675557447558785689899696557779759568777546775568455456442255222433414313
433542544533525424746746776557785859999988779569786898985989757666656554745566637574245534533532121
102115525355343357447434747864878698959765977979867866786777675895578765548855746376443465451125512
351523154635255336464346858657666597765969966989679667699987766755796475565486736744752343364321213
423235155355234367635766676588667966778586879877697788968986866566759764754786567645535554563312353
131553436636563367663475685874458967969697977997979788867686658965787574768454574765644522224342452
425311513252536365474678658644687955758996766996688996687966869869656656647766647546756256425343432
035535316265363537444744585758789587587969697669788689878886799787756565545488643377356463666143551
433111352346543655675648867788955697676896776696888997669867696797998575846847643365566322666253422
142554222353623634663476656576576759599877876678987899796866667665667796758885566656763524326523412
553221114466652537653487484688965785987978798787879778886869687879856576446844475475572665553322311
322215126226235677355378464486855799889979787987789899998978999898888758566687435633462236545525221
313112235462254453576755488856759997896886799799899988899779876666587756446785655466666326223312454
244411355552535377345666688746856978696796898988979889888979699868688867445865557755755235454313531
423534126443225554666348575686988895986769679879798788787867989986787998457447645534733633624642232
534222565323663376567746886777776797666869788888888789789997876686865895557746476676743534663612234
431433255624436444667577468887878697867788699999988897788987767687857655468476674645743656323315423
345545125463354463476474887467975687668769888997978988987887696676889857556487434757444653262441443
435223443263442335775687868588775867886768868998798978788987976865875989454457863563445263354635354
345242142456554446376374765856966766687687679897898788899886788978978876846454864444446656626623124
341531133422344366636755584885865569968787887898998898996888799658576887586564853536644355333123531
531354324434252444447647888455677658969668989897887798896967867688667687764444653657655322242251515
341243442253632754364654775476696998967688876686679796776869979996666974854784474465565646364251513
032311355356424443443777684847859565797966967889979769999967767679557564857757576335644553354134133
114145555335443455354745857886867865567989796787779986777997699798865678867787636364742266233335424
344514245524255637545457646776555786678587787778666899669978956577965646645567343453432466522454441
251343344325264256643575758776657986688857969886799699688966778578556554468453753547753446445535151
043443135333624377566634886567746955695958989989876666788667678685785785467565477654355655425532543
035122224554345443363445557578744885768878677698887879778669577958975774475734676457563326462311124
021124344524353323337534574456675858888969557976787668957958679785646545746754447757546355225333125
445245122262623336443754345874854655587699975769955678888677978667454758765665774473353522653153134
104351424332455533735435545656474658675675897879799765785667865795858476786347753776343645631324454
324542322453625462236334665774655447696856667668969589857996678545867547453554754724544322213213214
034543343326354233237754643775676575846669956677888556676767776756756575665365735666445646541212213
130313332234223236545753335468466765488769858995988665888657746744844845465747456426255455543114233
333414434421244465363554774634545886574878696787956899785964544558487666454465433252355535125444511
343134321142365333354375476744758675546757477969969756787556785478857636537677745565253615431223030
024113431151145262433473566363374846657667848764887455678675566746848644354545643353233645124234002
102102554415326564222565443454737686568866774874688677648666478578456576354334523555356551352521330
214032555331126556325325576536535774644884484764877867877668468655443677744345435423522245154212003
331000231233323222545443445746557477855878875855558887468857875583574453546353556365265531554514210
230230424552513432642224545447756563555488468477556644765475748446436775655455522222211315542543210
034320135334452542333332232775535436456684444444855755768555777637474433673263662664253121152140021
300102443215153322652244553275445743347747738784785556788453473544744457466535556264245423134001034
221330433531344432453662425623775347435735437537436475354373747763636375236342545441431315521104342
242432404041145244545256562346644463454767445536533675363437667467336466445364256651231214512031101
332012112315352345356344626244566335366433444363364747434465474775344245636463443145553211304430313
014014200315441234414554263333352363376453765473366537567363537445554656442562362334315122140032213
331231141401352354453214345322446344775336554354775777676774643466655266626653435541521122313333033
022032100414421141543523356654425642335563433647543446343354573364364432655634342533311521220334022
332024143231012134544415364652343322443645664433666673334362624566644436664443133454115432123322232
022012440221044225513415343456223242262536443423746733235554554333652662342154134155131232242101021
032200401321204114414341313223565442522255533422443554662333465325653524225531145252133030102012203
132130302204012241232432525353555345426365455634555652356424333353266321331333533513424422431331312
333331204200411111455334351333415422334456364533265252642262345446236431543241225432412423040033121
021121100043220232314115541242255262646345554552356434566525625665344225331141424342424113302012010
010232013234411124340321315251315331554254233364232653533255563233423221135354120214411114303133003
000233213332310143202041141254132411515256356225645646462564621242424112342134113021100212310230021
210233133023004431030124243353411441443323552256255462343313541135345353412553144201140321002301110
120223033103020130313221301115522445342123311254451312533135135344423123513034312111432333300203030
000032003312202042314110102051553525152114432152521441455154531252112324540404240320002132003031111
120020023012112241030432400312341452211445333435122342123254353543241311324432422134302211113331100
100200020233330030322133232440212423132312154431542113113331325525211133104212342300301221121211110
121200200322220022421214003023013441412113134513431242215243245241223111100231441333010110323110110

Loading…
Cancel
Save