with open("day9/input.txt") as f:
input = f.read().splitlines()Day 9
Part 1
- Read in input file
First 100 lines of input looks like this:
['U 1',
'D 1',
'R 1',
'L 2',
'D 2',
'U 1',
'R 2',
'D 2',
'U 1',
'R 1',
'L 1',
'D 2',
'R 2',
'U 2',
'L 1',
'D 1',
'U 1',
'L 1',
'D 2',
'U 1',
'D 1',
'L 1',
'U 2',
'R 1',
'L 1',
'R 1',
'L 1',
'U 2',
'L 2',
'U 2',
'L 1',
'U 2',
'D 1',
'U 2',
'D 1',
'L 2',
'U 1',
'D 1',
'U 2',
'R 2',
'D 2',
'R 1',
'D 2',
'L 1',
'U 1',
'L 2',
'D 2',
'L 2',
'R 1',
'U 1',
'L 2',
'D 1',
'U 1',
'R 2',
'L 2',
'D 1',
'R 1',
'L 1',
'R 2',
'U 1',
'R 2',
'D 1',
'U 1',
'L 2',
'D 1',
'R 2',
'D 1',
'U 2',
'R 1',
'D 1',
'R 2',
'U 1',
'L 2',
'D 2',
'U 2',
'R 2',
'L 1',
'D 2',
'R 1',
'D 1',
'U 2',
'D 2',
'L 2',
'R 2',
'L 2',
'D 1',
'R 2',
'D 2',
'L 1',
'U 1',
'D 1',
'L 2',
'U 1',
'R 1',
'D 1',
'U 2',
'D 1',
'L 2',
'D 2',
'R 2']
A function that processes the moves
Finally the answer for the top 1 given the input
rope, all_occupieds = process_moves(input, knots=2)
print(f'the correct answer for part 1 is {len(all_occupieds)}')the correct answer for part 1 is 6332
Part 2
rope2, all_occupieds2 = process_moves(input, knots=10)
print(f'the correct answer for part 2 is {len(all_occupieds2)}')the correct answer for part 2 is 2511