2024 Dec 06th

This commit is contained in:
Julia Lange 2024-12-10 09:43:54 -08:00
parent d6b3b26f63
commit de311040d8
Signed by: Julia
SSH key fingerprint: SHA256:KI8YxpkPRbnDRkXPgCuQCVz181++Vy7NAvmQj8alOhM

51
Python/2024/06/main.py Normal file
View file

@ -0,0 +1,51 @@
input = open("input", 'r')
# d = [line for line in input]
# d = [[val for val in line.strip()] for line in input]
d = {(complex(x,y)): c for y,line in enumerate(input) for x,c in enumerate(line)}
guard = 0
for pos,char in d.items():
if char == "^":
guard = pos
break
def step(pos,dir):
npos = pos + dir
if npos not in d:
return -1,-1
elif d[npos] == "#":
return pos,(dir*1j)
else:
return npos,dir
def run():
pos = guard
sposs = set([pos])
dir = -1j
loop = set([(pos,dir)])
while pos != -1:
pos,dir = step(pos,dir)
if (pos,dir) in loop:
return -1
if pos == -1:
break
sposs.add(pos)
loop.add((pos,dir))
return len(sposs)
def run2():
sm = 0
for pos,char in d.items():
if char != ".":
continue
d[pos] = "#"
if run(True) == -1:
sm += 1
d[pos] = "."
return sm
print(run())
print(run2())