18-2
This commit is contained in:
14
17/17-2.py
14
17/17-2.py
@@ -24,6 +24,12 @@ Register C: 0
|
|||||||
Program: 2,4,1,1,7,5,0,3,4,7,1,6,5,5,3,0
|
Program: 2,4,1,1,7,5,0,3,4,7,1,6,5,5,3,0
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
real_erik = """Register A: 25358015
|
||||||
|
Register B: 0
|
||||||
|
Register C: 0
|
||||||
|
Program: 2,4,1,2,7,5,4,5,1,3,5,5,0,3,3,0
|
||||||
|
"""
|
||||||
|
|
||||||
def read_input(input_var:str)->list[int]:
|
def read_input(input_var:str)->list[int]:
|
||||||
inp = input_var.split("\n")
|
inp = input_var.split("\n")
|
||||||
program = list(map(int, inp[3].split(':')[1].split(",")))
|
program = list(map(int, inp[3].split(':')[1].split(",")))
|
||||||
@@ -64,14 +70,17 @@ def do(instruction:int, operand:int, point:int):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
#prog = read_input(ex_p1_input)
|
#prog = read_input(ex_p1_input)
|
||||||
#prog = read_input(ex_p2_input)
|
prog = read_input(ex_p2_input)
|
||||||
prog = read_input(real_input)
|
#prog = read_input(real_input)
|
||||||
|
#prog = read_input(real_erik)
|
||||||
|
count = 0
|
||||||
sol_list = [""]
|
sol_list = [""]
|
||||||
for x in range(2,len(prog)+2,2):
|
for x in range(2,len(prog)+2,2):
|
||||||
new_list = []
|
new_list = []
|
||||||
for i in sol_list:
|
for i in sol_list:
|
||||||
for o1 in range(8):
|
for o1 in range(8):
|
||||||
for o2 in range(8):
|
for o2 in range(8):
|
||||||
|
count += 1
|
||||||
i_str=f"{i}{o1}{o2}"
|
i_str=f"{i}{o1}{o2}"
|
||||||
ra_a = int(i_str, 8)
|
ra_a = int(i_str, 8)
|
||||||
ra = int(i_str, 8)
|
ra = int(i_str, 8)
|
||||||
@@ -91,4 +100,5 @@ if __name__ == "__main__":
|
|||||||
print()
|
print()
|
||||||
print(f"Prog: {prog}")
|
print(f"Prog: {prog}")
|
||||||
print(f"Solution Part2: {min(integer_liste)}")
|
print(f"Solution Part2: {min(integer_liste)}")
|
||||||
|
print(f"Checked Numbers: {count}")
|
||||||
print(f'Runtime: {time()-start_time:.5f} s')
|
print(f'Runtime: {time()-start_time:.5f} s')
|
||||||
|
|||||||
80
18/18-1.py
Normal file
80
18/18-1.py
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
file = "./input.txt"
|
||||||
|
XY_DIMENSION = 70
|
||||||
|
FIRST_BYTES = 1024
|
||||||
|
|
||||||
|
|
||||||
|
#file = "./ex.txt"
|
||||||
|
#XY_DIMENSION = 6
|
||||||
|
#FIRST_BYTES = 12
|
||||||
|
|
||||||
|
from time import time
|
||||||
|
start_time = time()
|
||||||
|
|
||||||
|
def make_empty_grid(dimension:int)->list[list[str]]:
|
||||||
|
result = []
|
||||||
|
for _ in range(dimension+1):
|
||||||
|
result.append(["." for _ in range(dimension+1)])
|
||||||
|
return result
|
||||||
|
|
||||||
|
def print_grid(f):
|
||||||
|
for r in f:
|
||||||
|
for s in r:
|
||||||
|
if s == 0:
|
||||||
|
print(".", end="")
|
||||||
|
else:
|
||||||
|
print(s, end="")
|
||||||
|
print()
|
||||||
|
|
||||||
|
def fill_grid(gri:list[list[str]], ma:list[list[int]], filler:str)->list[list[str]]:
|
||||||
|
for m in ma:
|
||||||
|
x , y = m[0],m[1]
|
||||||
|
gri[y][x] = filler
|
||||||
|
return gri
|
||||||
|
|
||||||
|
def read_input(input_file:str, ) -> list[list[int]]:
|
||||||
|
out = []
|
||||||
|
f = open(input_file, "r")
|
||||||
|
for line in f:
|
||||||
|
line = line.strip()
|
||||||
|
if line == "":
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
out.append(list(map(int,line.split(','))))
|
||||||
|
f.close()
|
||||||
|
return out
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
|
def shortest_path(array, start, end):
|
||||||
|
zeilen, spalten = len(array), len(array[0])
|
||||||
|
visited = set()
|
||||||
|
queue = deque([(start, [start])])
|
||||||
|
while queue:
|
||||||
|
(x, y), pfad = queue.popleft()
|
||||||
|
if (x, y) == end:
|
||||||
|
return len(pfad) - 1, pfad
|
||||||
|
for dx, dy in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
|
||||||
|
nx, ny = x + dx, y + dy
|
||||||
|
if 0 <= nx < zeilen and 0 <= ny < spalten and array[nx][ny] == '.' and (nx, ny) not in visited:
|
||||||
|
queue.append(((nx, ny), pfad + [(nx, ny)]))
|
||||||
|
visited.add((nx, ny))
|
||||||
|
|
||||||
|
return -1, [] # Kein Pfad gefunden
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
grid = make_empty_grid(XY_DIMENSION)
|
||||||
|
byte_list = read_input(file)[:FIRST_BYTES]
|
||||||
|
#print_grid(field)
|
||||||
|
#print(read_input(file))
|
||||||
|
fill_grid(grid,byte_list,"#")
|
||||||
|
print_grid(grid)
|
||||||
|
|
||||||
|
sol = shortest_path(grid,(0,0),(XY_DIMENSION,XY_DIMENSION))
|
||||||
|
print(f"Shortest Path Length: {sol[0]}")
|
||||||
|
print_grid(fill_grid(grid,sol[1],"O"))
|
||||||
|
|
||||||
|
|
||||||
|
print(f'Runtime: {time()-start_time:.4f} s')
|
||||||
74
18/18-2.py
Normal file
74
18/18-2.py
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
file = "./input.txt"
|
||||||
|
XY_DIMENSION = 70
|
||||||
|
FIRST_BYTES = 1024
|
||||||
|
|
||||||
|
|
||||||
|
#file = "./ex.txt"
|
||||||
|
#XY_DIMENSION = 6
|
||||||
|
#FIRST_BYTES = 12
|
||||||
|
|
||||||
|
from time import time
|
||||||
|
start_time = time()
|
||||||
|
|
||||||
|
def make_empty_grid(dimension:int)->list[list[str]]:
|
||||||
|
result = []
|
||||||
|
for _ in range(dimension+1):
|
||||||
|
result.append(["." for _ in range(dimension+1)])
|
||||||
|
return result
|
||||||
|
|
||||||
|
def print_grid(f):
|
||||||
|
for r in f:
|
||||||
|
for s in r:
|
||||||
|
if s == 0:
|
||||||
|
print(".", end="")
|
||||||
|
else:
|
||||||
|
print(s, end="")
|
||||||
|
print()
|
||||||
|
|
||||||
|
def fill_grid(gri:list[list[str]], ma:list[list[int]], filler:str)->list[list[str]]:
|
||||||
|
for m in ma:
|
||||||
|
x , y = m[0],m[1]
|
||||||
|
gri[y][x] = filler
|
||||||
|
return gri
|
||||||
|
|
||||||
|
def read_input(input_file:str, ) -> list[list[int]]:
|
||||||
|
out = []
|
||||||
|
f = open(input_file, "r")
|
||||||
|
for line in f:
|
||||||
|
line = line.strip()
|
||||||
|
if line == "":
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
out.append(list(map(int,line.split(','))))
|
||||||
|
f.close()
|
||||||
|
return out
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
|
def shortest_path(array, start, end):
|
||||||
|
zeilen, spalten = len(array), len(array[0])
|
||||||
|
visited = set()
|
||||||
|
queue = deque([(start, [start])])
|
||||||
|
while queue:
|
||||||
|
(x, y), pfad = queue.popleft()
|
||||||
|
if (x, y) == end:
|
||||||
|
return len(pfad) - 1, pfad
|
||||||
|
for dx, dy in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
|
||||||
|
nx, ny = x + dx, y + dy
|
||||||
|
if 0 <= nx < zeilen and 0 <= ny < spalten and array[nx][ny] == '.' and (nx, ny) not in visited:
|
||||||
|
queue.append(((nx, ny), pfad + [(nx, ny)]))
|
||||||
|
visited.add((nx, ny))
|
||||||
|
|
||||||
|
return -1, [] # Kein Pfad gefunden
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
grid = make_empty_grid(XY_DIMENSION)
|
||||||
|
corrupted = read_input(file)
|
||||||
|
for i in range(FIRST_BYTES, len(corrupted)):
|
||||||
|
if shortest_path(fill_grid(grid,corrupted[:i],"#"),(0,0),(XY_DIMENSION,XY_DIMENSION))[0] == -1:
|
||||||
|
print(f"First Blocking Field: {corrupted[:i][-1]}")
|
||||||
|
break
|
||||||
|
print(f'Runtime: {time()-start_time:.4f} s')
|
||||||
Reference in New Issue
Block a user