2024 Dec 06th
This commit is contained in:
parent
d6b3b26f63
commit
de311040d8
1 changed files with 51 additions and 0 deletions
51
Python/2024/06/main.py
Normal file
51
Python/2024/06/main.py
Normal 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())
|
||||
Loading…
Add table
Add a link
Reference in a new issue