Guest User

Untitled

a guest
Jan 7th, 2026
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.94 KB | None | 0 0
  1. def brute(position: int, rotation: int, counter: int) -> (int, int):
  2.     while rotation < 0:
  3.         position = position - 1
  4.         rotation = rotation + 1
  5.         if position == 0:
  6.             counter = counter + 1
  7.         if position == -1:
  8.             position = 99
  9.     while rotation > 0:
  10.         position = position + 1
  11.         rotation = rotation - 1
  12.         if position == 100:
  13.             counter = counter + 1
  14.             position = 0
  15.     return(position, counter)
  16.                
  17. def a1(position: int, rotation: int, counter: int) -> (int, int):
  18.     counter = counter + abs((position + rotation) // 100)
  19.     position = (rotation + position) % 100
  20.     return(position, counter)
  21.    
  22. def a2(position: int, rotation: int, counter: int) -> (int, int):
  23.     full_loops, remainder = divmod(abs(rotation), 100)
  24.     counter += full_loops
  25.  
  26.     if remainder + rotation > 100:
  27.         counter += 1
  28.         position = (position + remainder) % 100
  29.     if rotation - remainder > 0:
  30.         counter += 1
  31.         position = (position - remainder) % 100
  32.     return(position, counter)
  33.  
  34. def main():
  35.  
  36.     with open("day1_real.txt", 'r') as file:
  37.         directions = file.readlines()
  38.  
  39.     directions_processed = [d.strip() for d in directions]
  40.     directions_numerical = []
  41.     for d in directions_processed:
  42.         if d[0] == "L":
  43.             directions_numerical.append(int('-' + d[1:]))
  44.         if d[0] == "R":
  45.             directions_numerical.append(int('+' + d[1:]))
  46.  
  47.     position = 50
  48.     count = 0
  49.    
  50.     p1 = position
  51.     c1 = count
  52.     p2 = position
  53.     c2 = count
  54.     p3 = position
  55.     c3 = count
  56.    
  57.    
  58.    
  59.     for direction in directions_numerical:
  60.         p1,c1 = brute(p1,direction, c1)
  61.         p2,c2 = a1(p2,direction, c2)
  62.         p3,c3 = a2(p3,direction, c3)
  63.         print(direction,"",p1,c1,"",p2,c2,"",p3,c3,sep='\t')
  64.        
  65.    
  66.     print("\n",c1,c2,c3,sep='\t')
  67.  
  68.  
  69. if __name__ == "__main__":
  70.     main()
  71.  
  72.  
Advertisement
Add Comment
Please, Sign In to add comment