Advertisement
rolfvanoven

AoC 2022 dag 8

Dec 9th, 2022
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. # bestand inlezen:
  2. #bestand = open('voorbeeld.txt', 'r')
  3. bestand = open('input.txt', 'r')
  4. alles = bestand.readlines()
  5. for x in range(len(alles)):
  6.   alles[x] = alles[x].replace('\n', '')
  7.  
  8. antwoord = 0
  9.  
  10. # kies een boom:
  11. for y in range(len(alles)):
  12.   for x in range(len(alles[y])):
  13.  
  14. #kijk naar links:
  15.     zichtl = True
  16.     for z in range(x):
  17.       if alles[y][x-z-1] >= alles[y][x]:
  18.         zichtl = False
  19.  
  20. #kijk naar rechts:
  21.     zichtr = True
  22.     for z in range(len(alles[y])-x-1):
  23.       if alles[y][x+z+1] >= alles[y][x]:
  24.         zichtr = False
  25.  
  26. #kijk naar boven:
  27.     zichtb = True
  28.     for z in range(y):
  29.       if alles[y-z-1][x] >= alles[y][x]:
  30.         zichtb = False
  31.  
  32. # kijk naar onder:
  33.     zichto = True
  34.     for z in range(len(alles)-y-1):
  35.       if alles[y+z+1][x] >= alles[y][x]:
  36.         zichto = False
  37.  
  38. #zichtbare bomen optellen:
  39.     if zichtl or zichtr or zichtb or zichto:
  40.       antwoord += 1
  41.  
  42. #antwoord A:
  43. print(antwoord)
  44.  
  45. antwoord2 = 0
  46.  
  47. # kies een boom:
  48. for y in range(len(alles)):
  49.   for x in range(len(alles[y])):
  50.  
  51. #kijk naar links:
  52.     zichtl = True
  53.     links = 0
  54.     for z in range(x):
  55.       if alles[y][x-z-1] >= alles[y][x]:
  56.         if zichtl:
  57.           links = z+1
  58.         zichtl = False
  59.       if zichtl:
  60.         links = z+1
  61.  
  62. #kijk naar rechts:
  63.     zichtr = True
  64.     rechts = 0
  65.     for z in range(len(alles[y])-x-1):
  66.       if alles[y][x+z+1] >= alles[y][x]:
  67.         if zichtr:
  68.           rechts = z+1
  69.         zichtr = False
  70.       if zichtr:
  71.         rechts = z+1
  72.  
  73. #kijk naar boven:
  74.     zichtb = True
  75.     boven = 0
  76.     for z in range(y):
  77.       if alles[y-z-1][x] >= alles[y][x]:
  78.         if zichtb:
  79.           boven = z+1
  80.         zichtb = False
  81.       if zichtb:
  82.         boven = z+1
  83.  
  84. # kijk naar onder:
  85.     zichto = True
  86.     onder = 0
  87.     for z in range(len(alles)-y-1):
  88.       if alles[y+z+1][x] >= alles[y][x]:
  89.         if zichto:
  90.           onder = z+1
  91.         zichto = False
  92.       if zichto:
  93.         onder = z+1
  94.  
  95. # score uitrekenen:
  96.     score = links*rechts*boven*onder
  97.     if score > antwoord2:
  98.       antwoord2 = score
  99.  
  100. print(antwoord2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement