Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def brute(position: int, rotation: int, counter: int) -> (int, int):
- while rotation < 0:
- position = position - 1
- rotation = rotation + 1
- if position == 0:
- counter = counter + 1
- if position == -1:
- position = 99
- while rotation > 0:
- position = position + 1
- rotation = rotation - 1
- if position == 100:
- counter = counter + 1
- position = 0
- return(position, counter)
- def a1(position: int, rotation: int, counter: int) -> (int, int):
- counter = counter + abs((position + rotation) // 100)
- position = (rotation + position) % 100
- return(position, counter)
- def a2(position: int, rotation: int, counter: int) -> (int, int):
- full_loops, remainder = divmod(abs(rotation), 100)
- counter += full_loops
- if remainder + rotation > 100:
- counter += 1
- position = (position + remainder) % 100
- if rotation - remainder > 0:
- counter += 1
- position = (position - remainder) % 100
- return(position, counter)
- def main():
- with open("day1_real.txt", 'r') as file:
- directions = file.readlines()
- directions_processed = [d.strip() for d in directions]
- directions_numerical = []
- for d in directions_processed:
- if d[0] == "L":
- directions_numerical.append(int('-' + d[1:]))
- if d[0] == "R":
- directions_numerical.append(int('+' + d[1:]))
- position = 50
- count = 0
- p1 = position
- c1 = count
- p2 = position
- c2 = count
- p3 = position
- c3 = count
- for direction in directions_numerical:
- p1,c1 = brute(p1,direction, c1)
- p2,c2 = a1(p2,direction, c2)
- p3,c3 = a2(p3,direction, c3)
- print(direction,"",p1,c1,"",p2,c2,"",p3,c3,sep='\t')
- print("\n",c1,c2,c3,sep='\t')
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment