day 5
This commit is contained in:
@@ -0,0 +1,44 @@
|
|||||||
|
import copy
|
||||||
|
|
||||||
|
map = []
|
||||||
|
with open("input21.txt") as file:
|
||||||
|
for line in file:
|
||||||
|
map.append(list(line[:-1]))
|
||||||
|
for y in range(0,26501365):
|
||||||
|
print(str(y)+" out of 26501365")
|
||||||
|
newmap = copy.deepcopy(map)
|
||||||
|
for i in range(0,len(map)):
|
||||||
|
for j in range(0,len(map[i])):
|
||||||
|
if map[i][j] == "S":
|
||||||
|
newmap[i][j] = "."
|
||||||
|
if i > 0:
|
||||||
|
if map[i-1][j] != "#":
|
||||||
|
newmap[i-1][j] = "S"
|
||||||
|
else:
|
||||||
|
if map[len(map)-1][j] != "#":
|
||||||
|
newmap[len(map)-1][j] = "S"
|
||||||
|
if i+1 < len(map):
|
||||||
|
if map[i+1][j] != "#":
|
||||||
|
newmap[i+1][j] = "S"
|
||||||
|
else:
|
||||||
|
if map[0][j] != "#":
|
||||||
|
newmap[0][j] = "S"
|
||||||
|
if j > 0:
|
||||||
|
if map[i][j-1] != "#":
|
||||||
|
newmap [i][j-1] = "S"
|
||||||
|
else:
|
||||||
|
if map[i][len(map[i])-1] != "#":
|
||||||
|
newmap[i][len(map[i])-1] = "S"
|
||||||
|
if j+1 < len(map[i]):
|
||||||
|
if map[i][j+1] != "#":
|
||||||
|
newmap[i][j+1] = "S"
|
||||||
|
else:
|
||||||
|
if map[i][0] != "#":
|
||||||
|
newmap[i][0] = "S"
|
||||||
|
map = copy.deepcopy(newmap)
|
||||||
|
cnt = 0
|
||||||
|
for r in map:
|
||||||
|
for a in r:
|
||||||
|
cnt += 1 if a == "S" else 0
|
||||||
|
print(cnt)
|
||||||
|
|
||||||
|
|||||||
50
2024/05/05.py
Normal file
50
2024/05/05.py
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import copy
|
||||||
|
|
||||||
|
|
||||||
|
def checkPage(upd,rules):
|
||||||
|
out = True
|
||||||
|
for rule in rules:
|
||||||
|
if rule[1] in upd:
|
||||||
|
ind = upd.index(rule[1])
|
||||||
|
for i in range(ind,len(upd)):
|
||||||
|
out = False if upd[i] == rule[0] else out
|
||||||
|
return out
|
||||||
|
|
||||||
|
def findMid(arr):
|
||||||
|
return int(arr[int((len(arr)-1)/2)])
|
||||||
|
|
||||||
|
def orderUpd(upd,rules):
|
||||||
|
out = copy.deepcopy(upd)
|
||||||
|
temp = ""
|
||||||
|
for rule in rules:
|
||||||
|
if rule[0] in out and rule[1] in out:
|
||||||
|
temp = out[out.index(rule[0])]
|
||||||
|
out[out.index(rule[0])] = out[out.index(rule[1])]
|
||||||
|
out[out.index(rule[1])] = temp
|
||||||
|
if not checkPage(out,rules):
|
||||||
|
out = orderUpd(out,rules)
|
||||||
|
return out
|
||||||
|
|
||||||
|
flag = False
|
||||||
|
updates = []
|
||||||
|
rules = []
|
||||||
|
|
||||||
|
with open("input05.txt") as file:
|
||||||
|
for line in file:
|
||||||
|
if line == "\n":
|
||||||
|
flag = True
|
||||||
|
elif not flag:
|
||||||
|
rules.append([line.split("|")[0],line.split("|")[1][:-1]])
|
||||||
|
else:
|
||||||
|
updates.append(line.split(","))
|
||||||
|
updates[-1][-1] = updates[-1][-1][:-1]
|
||||||
|
out = 0
|
||||||
|
out2 = 0
|
||||||
|
for upd in updates:
|
||||||
|
if checkPage(upd,rules):
|
||||||
|
out += findMid(upd)
|
||||||
|
else:
|
||||||
|
upd=orderUpd(upd,rules)
|
||||||
|
out2 += findMid(upd)
|
||||||
|
print("sum of middles of correct updates: " + str(out))
|
||||||
|
print("sum of middles of newly ordered updates: " + str(out2))
|
||||||
1367
2024/05/input05.txt
Normal file
1367
2024/05/input05.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user