Initial commit
This commit is contained in:
commit
97a4330bc0
110 changed files with 7006 additions and 0 deletions
47
2024/6/one.py
Normal file
47
2024/6/one.py
Normal file
|
@ -0,0 +1,47 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
|
||||
input_file = sys.argv[1]
|
||||
|
||||
with open(input_file) as fd:
|
||||
lines = [line.rstrip() for line in fd.readlines()]
|
||||
|
||||
height = len(lines)
|
||||
width = len(lines[0])
|
||||
|
||||
for i in range(height):
|
||||
if "^" in lines[i]:
|
||||
j = lines[i].index("^")
|
||||
break
|
||||
|
||||
d = 0
|
||||
directions = [
|
||||
(-1, 0), # ^
|
||||
(0, 1), # >
|
||||
(1, 0), # v
|
||||
(0, -1), # <
|
||||
]
|
||||
|
||||
vis = [[False] * width for h in range(height)]
|
||||
|
||||
while True:
|
||||
print(i, j)
|
||||
vis[i][j] = True
|
||||
ii, jj = i + directions[d][0], j + directions[d][1]
|
||||
if ii not in range(height) or jj not in range(width):
|
||||
break
|
||||
if lines[ii][jj] == "#":
|
||||
d += 1
|
||||
d %= len(directions)
|
||||
continue
|
||||
i, j = ii, jj
|
||||
|
||||
count = 0
|
||||
for i in range(height):
|
||||
for j in range(width):
|
||||
if vis[i][j]:
|
||||
count += 1
|
||||
print(count)
|
||||
|
||||
|
67
2024/6/two.py
Normal file
67
2024/6/two.py
Normal file
|
@ -0,0 +1,67 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
|
||||
input_file = sys.argv[1]
|
||||
|
||||
with open(input_file) as fd:
|
||||
lines = [line.rstrip() for line in fd.readlines()]
|
||||
|
||||
height = len(lines)
|
||||
width = len(lines[0])
|
||||
|
||||
for i_start in range(height):
|
||||
if "^" in lines[i_start]:
|
||||
j_start = lines[i_start].index("^")
|
||||
break
|
||||
|
||||
directions = [
|
||||
(-1, 0), # ^
|
||||
(0, 1), # >
|
||||
(1, 0), # v
|
||||
(0, -1), # <
|
||||
]
|
||||
|
||||
positions: set[tuple[int, int]] = set()
|
||||
|
||||
i, j = i_start, j_start
|
||||
d = 0
|
||||
|
||||
while True:
|
||||
positions.add((i, j))
|
||||
ii, jj = i + directions[d][0], j + directions[d][1]
|
||||
if ii not in range(height) or jj not in range(width):
|
||||
break
|
||||
if lines[ii][jj] == "#":
|
||||
d += 1
|
||||
d %= len(directions)
|
||||
continue
|
||||
i, j = ii, jj
|
||||
|
||||
print(len(positions))
|
||||
positions.remove((i_start, j_start))
|
||||
|
||||
tot = 0
|
||||
for obstacle in positions:
|
||||
i, j = i_start, j_start
|
||||
d = 0
|
||||
path: set[tuple[int, int, int]] = set()
|
||||
|
||||
while True:
|
||||
state = (i, j, d)
|
||||
if state in path:
|
||||
loop = True
|
||||
tot += 1
|
||||
break
|
||||
path.add(state)
|
||||
ii, jj = i + directions[d][0], j + directions[d][1]
|
||||
if ii not in range(height) or jj not in range(width):
|
||||
loop = False
|
||||
break
|
||||
if lines[ii][jj] == "#" or (ii, jj) == obstacle:
|
||||
d += 1
|
||||
d %= len(directions)
|
||||
continue
|
||||
i, j = ii, jj
|
||||
|
||||
print(tot)
|
Loading…
Add table
Add a link
Reference in a new issue