This commit is contained in:
2024-12-13 15:14:29 +01:00
parent 58d0015201
commit 148a1426c7
3 changed files with 106 additions and 0 deletions

44
02/02-1.py Normal file
View File

@@ -0,0 +1,44 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
file = "./input.txt"
#file = "./ex.txt"
from time import time
start_time = time()
keypad=[[1,2,3],[4,5,6],[7,8,9]]
def read_file(f:str) -> list:
out = []
input_file = open(f, "r")
for line in input_file:
line = line.strip()
if line == "":
continue
else:
line = line.strip()
out.append(line)
input_file.close()
return out
if __name__ == "__main__":
x, y = 1, 1
part2 = True
input_list = read_file(file)
print("Solution: ",end="")
for elem in input_list:
for b in elem:
if b == "R" and y < 2:
y += 1
elif b == "L" and y > 0:
y -= 1
elif b == "U" and x > 0:
x -= 1
elif b == "D" and x < 2:
x += 1
print(keypad[x][y],end="")
print()
# print(input_list)
print(f'Runtime: {time()-start_time:.2f} s')

58
02/02-2.py Normal file
View File

@@ -0,0 +1,58 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
file = "./input.txt"
#file = "./ex.txt"
from time import time
start_time = time()
keypad=[
["-","-","1","-","-"],
["-","2","3","4","-"],
["5","6","7","8","9"],
["-","A","B","C","-"],
["-","-","D","-","-"]]
def read_file(f:str) -> list:
out = []
input_file = open(f, "r")
for line in input_file:
line = line.strip()
if line == "":
continue
else:
line = line.strip()
out.append(line)
input_file.close()
return out
def index_exists(array, i, j):
try:
if i>=0 and j>=0 and array[i][j] != "-":
return True
else:
return False
except IndexError:
return False
if __name__ == "__main__":
x, y = 2, 0
part2 = True
input_list = read_file(file)
print("Solution: ",end="")
for elem in input_list:
for b in elem:
if b == "R" and index_exists(keypad, x, y+1):
y += 1
elif b == "L" and index_exists(keypad, x, y-1):
y -= 1
elif b == "U" and index_exists(keypad, x-1, y):
x -= 1
elif b == "D" and index_exists(keypad, x+1, y):
x += 1
print(keypad[x][y],end="")
print()
# print(input_list)
print(f'Runtime: {time()-start_time:.2f} s')

4
02/ex.txt Normal file
View File

@@ -0,0 +1,4 @@
ULL
RRDDD
LURDL
UUUUD