From 9459f5cc97c6777a6e7ace8b5bfdcf9154ee681d Mon Sep 17 00:00:00 2001 From: starnakin Date: Sun, 10 Dec 2023 17:04:57 +0100 Subject: [PATCH] add: part 1 --- 2023/day10/example1.txt | 6 +++++ 2023/day10/src/part1.py | 57 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 2023/day10/example1.txt create mode 100644 2023/day10/src/part1.py diff --git a/2023/day10/example1.txt b/2023/day10/example1.txt new file mode 100644 index 0000000..ebe6f82 --- /dev/null +++ b/2023/day10/example1.txt @@ -0,0 +1,6 @@ +......, +.S-7... +.|.|.., +.L7L-7. +..L--J. +....... \ No newline at end of file diff --git a/2023/day10/src/part1.py b/2023/day10/src/part1.py new file mode 100644 index 0000000..15b7546 --- /dev/null +++ b/2023/day10/src/part1.py @@ -0,0 +1,57 @@ +text: str = open("input.txt", "r").read() + +_value: int = 0 + +lines = text.splitlines() + +def get_symbol_vector(symbol: str): + match symbol: + case "|": + return ((0, -1), (0, +1)) + case "L": + return ((0, -1), (+1, 0)) + case "J": + return ((-1, 0), (0, -1)) + case "7": + return ((-1, 0), (0, +1)) + case "F": + return ((+1, 0), (0, +1)) + case "-": + return ((+1, 0), (-1, 0)) + +def get_start(lines: [str]): + for y, line in enumerate(lines): + for x, char in enumerate(line): + if (char == "S"): + return (x, y) + +def get_around(lines, x, y): + lst = [] + if (len(lines[y]) != x + 1 and lines[y][x + 1] != '.'): + lst.append((x + 1, y)) + if (x > 0 and lines[y][x - 1] != '.'): + lst.append((x - 1, y)) + if (len(lines) != y + 1 and lines[y + 1][x] != '.'): + lst.append((x, y + 1)) + if (y > 0 and lines[y - 1][x] != '.'): + lst.append((x, y - 1)) + return lst + +old_x, old_y = get_start(lines) +x, y = get_around(lines, old_x, old_y)[0] +nb_moves = 1 +while True: + backward, forward = get_symbol_vector(lines[y][x]) + if (backward == (old_x - x, old_y - y)): + old_x, old_y = x, y + x, y = map(sum, zip((x, y), forward)) + else: + old_x, old_y = x, y + x,y = map(sum, zip((x, y), backward)) + print(lines[y][x], x, y) + nb_moves += 1 + if (lines[y][x] == "S"): + _value = nb_moves // 2 + break + +print(_value) \ No newline at end of file