diff --git a/2023/10.py b/2023/10.py new file mode 100644 index 0000000..51c2fe6 --- /dev/null +++ b/2023/10.py @@ -0,0 +1,53 @@ +input = [[i for i in l] for l in open("input.txt", 'r').read().splitlines()] +traversed = [] + +whereami = (0, 0) +next = (0,0) + + +for x in range(len(input)): + for y in range(len(input[x])): + if input[x][y] == 'S': + whereami = (x, y) + if x > 0 and input[x-1][y] in ['|', '7', 'F']: + next = (x-1, y) + break + if x+1 < len(input) and input[x+1][y] in ['|', 'L', 'J']: + next = (x+1, y) + break + if y > 0 and input[x][y-1] in ['-', 'L', 'F']: + next = (x, y-1) + break + if y+1 < len(input[0]) and input[x][y+1] in ['-', '7', 'J']: + next = (x, y+1) + break + +while True: + x, y = next + traversed.append((x,y)) + if input[x][y] == 'F': + next = (x+1, y) if whereami == (x,y+1) else (x, y+1) + elif input[x][y] == '|': + next = (x+1, y) if whereami == (x-1,y) else (x-1, y) + elif input[x][y] == '-': + next = (x, y+1) if whereami == (x,y-1) else (x, y-1) + elif input[x][y] == 'L': + next = (x-1, y) if whereami == (x,y+1) else (x, y+1) + elif input[x][y] == 'J': + next = (x-1, y) if whereami == (x,y-1) else (x, y-1) + elif input[x][y] == '7': + next = (x+1, y) if whereami == (x,y-1) else (x, y-1) + if next in traversed: + break + whereami = (x, y) + +print(len(traversed)/2) + +area = 0 +for i in range(len(traversed)-1): + area += (traversed[i][1] + traversed[i+1][1]) * (traversed[i][0] - traversed[i+1][0]) +area += (traversed[-1][1] + traversed[0][1]) * (traversed[-1][0] - traversed[0][0]) +area = abs(area / 2) + +points = area - len(traversed) / 2 + 1 +print(points) \ No newline at end of file