summaryrefslogtreecommitdiffstats
path: root/prog/aoc/23/10/1.py
diff options
context:
space:
mode:
authorAnton Luka Šijanec <anton@sijanec.eu>2023-12-10 17:35:32 +0100
committerAnton Luka Šijanec <anton@sijanec.eu>2023-12-10 17:35:32 +0100
commit5f8234b55656b00672b9f4eb34d0ffa9a392a67d (patch)
tree522a4288959d6df32e9b02a6012f1ee03dbfb7de /prog/aoc/23/10/1.py
parentds1kolokvij1nedokončan (diff)
downloadr-5f8234b55656b00672b9f4eb34d0ffa9a392a67d.tar
r-5f8234b55656b00672b9f4eb34d0ffa9a392a67d.tar.gz
r-5f8234b55656b00672b9f4eb34d0ffa9a392a67d.tar.bz2
r-5f8234b55656b00672b9f4eb34d0ffa9a392a67d.tar.lz
r-5f8234b55656b00672b9f4eb34d0ffa9a392a67d.tar.xz
r-5f8234b55656b00672b9f4eb34d0ffa9a392a67d.tar.zst
r-5f8234b55656b00672b9f4eb34d0ffa9a392a67d.zip
Diffstat (limited to 'prog/aoc/23/10/1.py')
-rwxr-xr-xprog/aoc/23/10/1.py123
1 files changed, 123 insertions, 0 deletions
diff --git a/prog/aoc/23/10/1.py b/prog/aoc/23/10/1.py
new file mode 100755
index 0000000..a2e48e2
--- /dev/null
+++ b/prog/aoc/23/10/1.py
@@ -0,0 +1,123 @@
+#!/usr/bin/python3
+from mmap import mmap
+from sys import argv
+file = open(argv[1], "r+b")
+b = mmap(file.fileno(), 0)
+linelen = b.find(b'\n')+1
+numlines = len(b)//linelen
+S = b.find(b'S')
+def korak(idx, camefrom):
+ if chr(b[idx]) in ['S', "-", "J", "7"]:
+ if idx%linelen != 0:
+ if chr(b[idx-1]) in ["-", "L", "F", "S"]:
+ if camefrom != idx-1:
+ return idx-1
+ if chr(b[idx]) in ['S', "-", "F", "L"]:
+ if idx%linelen != -2%linelen:
+ if chr(b[idx+1]) in ["-", "J", "7", "S"]:
+ if camefrom != idx+1:
+ return idx+1
+ if chr(b[idx]) in ['S', "|", "F", "7"]:
+ if idx//linelen != linelen-1:
+ if chr(b[idx+linelen]) in ["|", "L", "J", "S"]:
+ if camefrom != idx+linelen:
+ return idx+linelen
+ if chr(b[idx]) in ['S', "|", "J", "L"]:
+ if idx//linelen != 0:
+ if chr(b[idx-linelen]) in ["|", "F", "7", "S"]:
+ if camefrom != idx-linelen:
+ return idx-linelen
+pos = S
+prev = -1
+numsteps = 0
+tiles = []
+while numsteps == 0 or pos != S:
+ pos, prev = korak(pos, prev), pos
+ # print("korak iz ", prev//linelen, ", ", prev%linelen, "na ", pos//linelen, ", ", pos%linelen)
+ tiles.append(pos)
+ numsteps += 1
+print(numsteps//2)
+# print(tiles)
+larger = max(tiles[0], tiles[-2])
+smaller = min(tiles[0], tiles[-2])
+# print(smaller, larger, S)
+schar = None
+if larger-smaller == 2:
+ schar = "-"
+if larger-smaller == 2*linelen:
+ schar = "|"
+if smaller+linelen+1 == larger:
+ if smaller == S-1:
+ schar = "7"
+ else:
+ schar = "L"
+if smaller+linelen-1 == larger:
+ if smaller == S+1:
+ schar = "F"
+ else:
+ schar = "J"
+x3 = {}
+def postavi(pos):
+ line = pos//linelen
+ column = pos%linelen
+ x3[(line*3+1, column*3+1)] = True
+ znak = chr(b[pos])
+ if pos == S:
+ znak = schar
+ match znak:
+ case "-":
+ x3[(line*3+1, column*3+1+1)] = True
+ x3[(line*3+1, column*3+1-1)] = True
+ case "|":
+ x3[(line*3+1+1, column*3+1)] = True
+ x3[(line*3+1-1, column*3+1)] = True
+ case "L":
+ x3[(line*3+1-1, column*3+1)] = True
+ x3[(line*3+1, column*3+1+1)] = True
+ case "J":
+ x3[(line*3+1-1, column*3+1)] = True
+ x3[(line*3+1, column*3+1-1)] = True
+ case "7":
+ x3[(line*3+1+1, column*3+1)] = True
+ x3[(line*3+1, column*3+1-1)] = True
+ case "F":
+ x3[(line*3+1+1, column*3+1)] = True
+ x3[(line*3+1, column*3+1+1)] = True
+for tile in tiles:
+ postavi(tile)
+fillstack = [(0, linelen-1)]
+def fill(coords):
+ if coords in x3:
+ return
+ x3[coords] = False
+ if coords[1]+1 < linelen*3:
+ if (coords[0], coords[1]+1) not in x3:
+ fillstack.append((coords[0], coords[1]+1))
+ if coords[1]-1 >= 0:
+ if (coords[0], coords[1]-1) not in x3:
+ fillstack.append((coords[0], coords[1]-1))
+ if coords[0]+1 < numlines*3:
+ if (coords[0]+1, coords[1]) not in x3:
+ fillstack.append((coords[0]+1, coords[1]))
+ if coords[0]-1 >= 0:
+ if (coords[0]-1, coords[1]) not in x3:
+ fillstack.append((coords[0]-1, coords[1]))
+while len(fillstack) > 0:
+ fill(fillstack.pop())
+def visx3():
+ for line in range(numlines*3):
+ for column in range(linelen*3):
+ if (line, column) not in x3:
+ print(".", end="")
+ else:
+ if x3[(line), column] == True:
+ print("x", end="")
+ else:
+ print("@", end="")
+ print()
+count = 0
+for line in range(numlines):
+ for column in range(linelen):
+ if (line*3+1, column*3+1) not in x3:
+ count += 1
+print(count)