Advertisement
rolfvanoven

AoC 2022 dag 9a

Dec 17th, 2022
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 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. hx = 0
  9. hy = 0
  10. tx = 0
  11. ty = 0
  12.  
  13. staart = [[0,0]]
  14.  
  15. for stap in range(len(alles)):
  16.  
  17. #naar boven lopen:
  18.   if alles[stap][0] == 'U':
  19.     for stapje in range(int(alles[stap][2:])):
  20.       hy += 1
  21.       if hy - ty > 1:
  22.         if hx == tx:
  23.           ty += 1
  24.         else:
  25.           ty += 1
  26.           tx = hx
  27.       staartje = [ty,tx]
  28.       if staartje not in staart:
  29.         staart.append(staartje)
  30.  
  31. #naar onder lopen:
  32.   if alles[stap][0] == 'D':
  33.     for stapje in range(int(alles[stap][2:])):
  34.       hy -= 1
  35.       if ty - hy > 1:
  36.         if hx == tx:
  37.           ty -= 1
  38.         else:
  39.           ty -= 1
  40.           tx = hx
  41.       staartje = [ty,tx]
  42.       if staartje not in staart:
  43.         staart.append(staartje)
  44.      
  45. #naar rechts lopen:
  46.   if alles[stap][0] == 'R':
  47.     for stapje in range(int(alles[stap][2:])):
  48.       hx += 1
  49.       if hx - tx > 1:
  50.         if hy == ty:
  51.           tx += 1
  52.         else:
  53.           tx += 1
  54.           ty = hy
  55.       staartje = [ty,tx]
  56.       if staartje not in staart:
  57.         staart.append(staartje)
  58.  
  59. #naar links lopen:
  60.   if alles[stap][0] == 'L':
  61.     for stapje in range(int(alles[stap][2:])):
  62.       hx -= 1
  63.       if tx - hx > 1:
  64.         if hy == ty:
  65.           tx -= 1
  66.         else:
  67.           tx -= 1
  68.           ty = hy
  69.       staartje = [ty,tx]
  70.       if staartje not in staart:
  71.         staart.append(staartje)
  72.  
  73. print(len(staart))
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement