|
| 1 | +import re |
| 2 | + |
| 3 | +from python.download_input import get_input_content |
| 4 | + |
| 5 | + |
| 6 | +turn_right = { |
| 7 | + 'R': 'D', |
| 8 | + 'D': 'L', |
| 9 | + 'L': 'U', |
| 10 | + 'U': 'R' |
| 11 | +} |
| 12 | + |
| 13 | +turn_left = { |
| 14 | + 'R': 'U', |
| 15 | + 'U': 'L', |
| 16 | + 'L': 'D', |
| 17 | + 'D': 'R' |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +def first(board, path): |
| 22 | + pos_x = pos_y = None |
| 23 | + direction = 'R' |
| 24 | + tiles = [] |
| 25 | + walls = [] |
| 26 | + |
| 27 | + for y, line in enumerate(board): |
| 28 | + for x, cell in enumerate(line): |
| 29 | + if cell == ' ': |
| 30 | + continue |
| 31 | + tiles.append((x, y)) |
| 32 | + if cell == '.': |
| 33 | + # Set starting position at first tiles seen |
| 34 | + if pos_x is None and pos_y is None: |
| 35 | + pos_x = x |
| 36 | + pos_y = y |
| 37 | + if cell == '#': |
| 38 | + walls.append((x, y)) |
| 39 | + |
| 40 | + # Read path |
| 41 | + for instruction in re.findall(r'(\d+|[RL])', path): |
| 42 | + try: |
| 43 | + steps = int(instruction) |
| 44 | + except ValueError: |
| 45 | + match instruction: |
| 46 | + case 'R': direction = turn_right[direction] |
| 47 | + case 'L': direction = turn_left[direction] |
| 48 | + case _: raise ValueError(f'{instruction} not recognized') |
| 49 | + else: |
| 50 | + for i in range(steps): |
| 51 | + # Find next_position |
| 52 | + next_x = pos_x |
| 53 | + next_y = pos_y |
| 54 | + match direction: |
| 55 | + case 'R': |
| 56 | + next_x += 1 |
| 57 | + if (next_x, next_y) not in tiles: |
| 58 | + next_x = 0 |
| 59 | + while (next_x, next_y) not in tiles: |
| 60 | + next_x += 1 |
| 61 | + case 'D': |
| 62 | + next_y += 1 |
| 63 | + if (next_x, next_y) not in tiles: |
| 64 | + next_y = 0 |
| 65 | + while (next_x, next_y) not in tiles: |
| 66 | + next_y += 1 |
| 67 | + case 'L': |
| 68 | + next_x -= 1 |
| 69 | + if (next_x, next_y) not in tiles: |
| 70 | + next_x = len(board[0]) |
| 71 | + while (next_x, next_y) not in tiles: |
| 72 | + next_x -= 1 |
| 73 | + case 'U': |
| 74 | + next_y -= 1 |
| 75 | + if (next_x, next_y) not in tiles: |
| 76 | + next_y = len(board) |
| 77 | + while (next_x, next_y) not in tiles: |
| 78 | + next_y -= 1 |
| 79 | + |
| 80 | + # Check if it is a wall |
| 81 | + if (next_x, next_y) in walls: |
| 82 | + # Stop here |
| 83 | + break |
| 84 | + |
| 85 | + # Check if it is a tile |
| 86 | + if (next_x, next_y) in tiles: |
| 87 | + # Go forward |
| 88 | + pos_x = next_x |
| 89 | + pos_y = next_y |
| 90 | + |
| 91 | + return 1000 * (pos_y + 1) + 4 * (pos_x + 1) + 'RDLU'.index(direction) |
| 92 | + |
| 93 | + |
| 94 | +def second(board, path): |
| 95 | + return 2 |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + content = get_input_content(__file__, split_by_lines=False) |
| 100 | + test_input = ''' ...# |
| 101 | + .#.. |
| 102 | + #... |
| 103 | + .... |
| 104 | +...#.......# |
| 105 | +........#... |
| 106 | +..#....#.... |
| 107 | +..........#. |
| 108 | + ...#.... |
| 109 | + .....#.. |
| 110 | + .#...... |
| 111 | + ......#. |
| 112 | +
|
| 113 | +10R5L5R10L4R5L5''' |
| 114 | + # if test_input: |
| 115 | + # content = test_input\ |
| 116 | + |
| 117 | + board, path = content.split('\n\n') |
| 118 | + board = board.split('\n') |
| 119 | + |
| 120 | + print(f'Le résultat de la première partie est :\n{first(board, path)}') |
| 121 | + |
| 122 | + print(f'Le résultat de la deuxième partie est :\n{second(board, path)}') |
0 commit comments