rolfvanoven

AoC 2024 dag 1

Dec 2nd, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. #bestand inlezen:
  2. bestand = open('voorbeeld.txt', 'r')
  3. #bestand = open('input.txt', 'r')
  4. lijst = bestand.readlines()
  5.  
  6. #splitsen:
  7. for x in range(len(lijst)):
  8.   lijst[x] = lijst[x].split()
  9.  
  10. #lege lijsten maken:
  11. links = []
  12. rechts = []
  13.  
  14. #linksrechts vullen:
  15. for x in range(len(lijst)):
  16.   links.append(int(lijst[x][0]))
  17.   rechts.append(int(lijst[x][1]))
  18.  
  19. #sorteren:
  20. links.sort()
  21. rechts.sort()
  22.  
  23. #tellen:
  24. verschil = 0
  25. for x in range(len(lijst)):
  26.   verschil += abs(links[x]-rechts[x])
  27. #  print(verschil)
  28.  
  29.  
  30. print(verschil)
  31.  
  32.  
  33. #deel 2:
  34. antwoord = 0
  35.  
  36. for x in range(len(links)):
  37.   aantaldeze = 0
  38.   for y in range(len(rechts)):
  39.     if links[x] == rechts[y]:
  40.       aantaldeze += 1
  41. #  print(links[x], 'komt', aantaldeze, 'keer voor')
  42.   antwoord += (aantaldeze * links[x])
  43.  
  44. print(antwoord)
Advertisement
Add Comment
Please, Sign In to add comment