From c583bfb813dbf03abb95b88b16e8fff2e545f75b Mon Sep 17 00:00:00 2001 From: Samuel Ortion Date: Wed, 27 Nov 2024 10:50:13 +0100 Subject: [PATCH] Solve day 3 with python --- 2015/days/03/walk.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2015/days/03/walk.py diff --git a/2015/days/03/walk.py b/2015/days/03/walk.py new file mode 100644 index 0000000..463dcaf --- /dev/null +++ b/2015/days/03/walk.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +count = 0 +x = 0 +y = 0 +visited = {} + +MOTION = { + "^": [0, -1], + "<": [-1, 0], + "v": [0, 1], + ">": [1, 0] +} + +def move(char): + global x + global y + motion = MOTION[char] + x += motion[0] + y += motion[1] + visit(x, y) + +def visit(x, y): + global count + if (x, y) not in visited: + visited[(x, y)] = 1 + count += 1 + +visit(x, y) +with open("./data/input", "r") as f: + for line in f: + for char in line: + move(char) +print(count)