|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +import os, drawing |
| 3 | + |
| 4 | +test_data = {} |
| 5 | + |
| 6 | +test = 1 |
| 7 | +test_data[test] = {"input": """..# |
| 8 | +#.. |
| 9 | +...""", |
| 10 | + "expected": ['Unknown', 'Unknown'], |
| 11 | + } |
| 12 | + |
| 13 | +test = 'real' |
| 14 | +input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt')) |
| 15 | +test_data[test] = {"input": open(input_file, "r+").read().strip(), |
| 16 | + "expected": ['5182', '2512008'], |
| 17 | + } |
| 18 | + |
| 19 | +# -------------------------------- Control program execution -------------------------------- # |
| 20 | + |
| 21 | +case_to_test = 'real' |
| 22 | +part_to_test = 2 |
| 23 | +verbose_level = 1 |
| 24 | + |
| 25 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 26 | + |
| 27 | +puzzle_input = test_data[case_to_test]['input'] |
| 28 | +puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
| 29 | +puzzle_actual_result = 'Unknown' |
| 30 | + |
| 31 | + |
| 32 | +# -------------------------------- Actual code execution -------------------------------- # |
| 33 | + |
| 34 | +def turn_left (direction): |
| 35 | + return (direction[1], -direction[0]) |
| 36 | + |
| 37 | +def turn_right (direction): |
| 38 | + return (-direction[1], direction[0]) |
| 39 | + |
| 40 | +if part_to_test == 1: |
| 41 | + grid = drawing.text_to_grid (puzzle_input) |
| 42 | + position = (len(puzzle_input.split('\n'))//2, len(puzzle_input.split('\n'))//2) |
| 43 | + direction = (0, -1) |
| 44 | + new_infections = 0 |
| 45 | + |
| 46 | + for i in range (10**4): |
| 47 | + if position in grid: |
| 48 | + if grid[position] == '.': |
| 49 | + direction = turn_left(direction) |
| 50 | + grid[position] = '#' |
| 51 | + new_infections += 1 |
| 52 | + else: |
| 53 | + direction = turn_right(direction) |
| 54 | + grid[position] = '.' |
| 55 | + else: |
| 56 | + direction = turn_left(direction) |
| 57 | + grid[position] = '#' |
| 58 | + new_infections += 1 |
| 59 | + |
| 60 | + position = (position[0] + direction[0], position[1] + direction[1]) |
| 61 | + |
| 62 | + puzzle_actual_result = new_infections |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +else: |
| 67 | + grid = drawing.text_to_grid (puzzle_input) |
| 68 | + position = (len(puzzle_input.split('\n'))//2, len(puzzle_input.split('\n'))//2) |
| 69 | + direction = (0, -1) |
| 70 | + new_infections = 0 |
| 71 | + |
| 72 | + for i in range (10**7): |
| 73 | + if position in grid: |
| 74 | + if grid[position] == '.': |
| 75 | + direction = turn_left(direction) |
| 76 | + grid[position] = 'W' |
| 77 | + elif grid[position] == 'W': |
| 78 | + grid[position] = '#' |
| 79 | + new_infections += 1 |
| 80 | + elif grid[position] == '#': |
| 81 | + direction = turn_right(direction) |
| 82 | + grid[position] = 'F' |
| 83 | + else: |
| 84 | + direction = turn_right(turn_right(direction)) |
| 85 | + grid[position] = '.' |
| 86 | + else: |
| 87 | + direction = turn_left(direction) |
| 88 | + grid[position] = 'W' |
| 89 | + |
| 90 | + position = (position[0] + direction[0], position[1] + direction[1]) |
| 91 | + |
| 92 | + puzzle_actual_result = new_infections |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +# -------------------------------- Outputs / results -------------------------------- # |
| 98 | + |
| 99 | +if verbose_level >= 3: |
| 100 | + print ('Input : ' + puzzle_input) |
| 101 | +print ('Expected result : ' + str(puzzle_expected_result)) |
| 102 | +print ('Actual result : ' + str(puzzle_actual_result)) |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
0 commit comments