summary refs log tree commit diff stats
path: root/day19.py
diff options
context:
space:
mode:
Diffstat (limited to 'day19.py')
-rw-r--r--day19.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/day19.py b/day19.py
new file mode 100644
index 0000000..eddbe77
--- /dev/null
+++ b/day19.py
@@ -0,0 +1,37 @@
+with open("day19.txt") as diagram:
+    l = [line.strip('\n') for line in diagram]
+
+lx, ly = 0, 0
+sx, sy = 0, 0
+while l[sx][sy] == ' ':
+	sy += 1
+dx, dy = 1, 0
+
+ddx = [1, -1, 0, 0]
+ddy = [0, 0, -1, 1]
+ans = 0
+ret = ""
+while True:
+	ans += 1
+	if l[sx][sy].isupper():
+		ret += l[sx][sy]
+	nx, ny = sx + dx, sy + dy
+	if nx >= 0 and nx < len(l) and ny >= 0 and ny < len(l[nx]) and l[nx][ny] != ' ':
+		lx, ly, sx, sy = sx, sy, nx, ny
+	else:
+		k = 0
+		good = False
+		while k < len(ddx):
+			dx, dy = ddx[k], ddy[k]
+			nx, ny = sx + dx, sy + dy
+			if nx == lx and ny == ly:
+				k += 1
+				continue
+			if nx >= 0 and nx < len(l) and ny >= 0 and ny < len(l[nx]) and l[nx][ny] != ' ':
+				lx, ly, sx, sy = sx, sy, nx, ny
+				good = True
+				break
+			k += 1
+		if not good:
+			print("trail ends at {} {} with string {} and length {}".format(sx, sy, ret, ans))
+			break
\ No newline at end of file