Compare commits

..

2 Commits

Author SHA1 Message Date
d3bd335e2b Added old 2022 AoC solves 2023-12-09 22:06:15 +01:00
be08eff4ed Added old 2021 AoC solves 2023-12-09 22:01:20 +01:00
23 changed files with 1231 additions and 0 deletions

19
2021/day1/day1.py Normal file
View File

@ -0,0 +1,19 @@
count = 0
grow = 0
with open('day1_data.txt') as f:
list_array = f.readlines()
for x in range(0,len(list_array),1):
if count != 0:
if int(list_array[count]) > int(list_array[count-1]):
grow += 1
print("Count: "+str(count)+"\t\t "+str(int(list_array[count]))+" ~ "+str(int(list_array[count-1]))+"\t\tGrow: "+str(grow))
count += 1
print("I count "+str(count)+" lines")
print("I found "+str(grow)+" depth measurement increases")

38
2021/day2/day2.py Normal file
View File

@ -0,0 +1,38 @@
with open("day2_data.txt", "r") as f:
x = [[ j for j in x.split() ] for x in f]
horizontal=0
depth=0
#print(x)
direction=""
for i in range(len(x)):
direction=x[i][0]
temp=int(x[i][1])
if(direction=="up"):
#print("up "+ str(temp))
depth-=temp
elif(direction=="down"):
#print("down " + str(temp))
depth+=temp
elif(direction=="forward"):
#print("forward " + str(temp))
horizontal+=temp
else:
print("ERROR")
print("horizontal: "+str(horizontal))
print("depth: " + str(depth))
print("result: " + str(horizontal*depth))
#print(direction + " " + str(temp))
#print(temp,end=" ")
#print("("+str(type(temp))+")",end=" ")

43
2021/day2/day2_part2.py Normal file
View File

@ -0,0 +1,43 @@
with open("day2_data.txt", "r") as f:
x = [[ j for j in x.split() ] for x in f]
horizontal=0
depth=0
aim=0
#print(x)
direction=""
for i in range(len(x)):
direction=x[i][0]
temp=int(x[i][1])
if(direction=="up"):
#print("up "+ str(temp))
aim-=temp
#depth-=temp
elif(direction=="down"):
#print("down " + str(temp))
aim+=temp
#depth+=temp
elif(direction=="forward"):
#print("forward " + str(temp))
depth = depth + (aim * temp)
horizontal+=temp
direction="forw"
else:
print("ERROR")
print(direction+" "+str(temp)+ "\t horizontal: " + str(horizontal) + "\t depth: " + str(depth) + "\t\t aim: " + str(aim))
print("horizontal: "+str(horizontal))
print("depth: " + str(depth))
print("result: " + str(horizontal*depth))
#print(direction + " " + str(temp))
#print(temp,end=" ")
#print("("+str(type(temp))+")",end=" ")

152
2021/day3/day3_part1.py Normal file
View File

@ -0,0 +1,152 @@
import numpy as np
filename = open("day3_data.txt","r")
x = []
xr = filename.readline().strip()
while xr:
xr = list(map(int,str(xr)))
x.append(xr)
xr = filename.readline().strip()
x = np.array(x)
print(x)
print(x[0][0])
print(x[0,0])
print("---------------")
#for i in range(len(x[:,0])):
# print(x[i][0])
print("------Gamma rate---------")
g=[]
for i in range(len(x[0,:])):
g.append(np.bincount(x[:,i]).argmax())
g = np.array(g)
print("Binary: ",end="")
print(g[:])
print("Decimal: ",end="")
gs=""
gs=''.join(str(n) for n in g)
print(int(gs,2))
print("---------Epsilon rate----------")
e=[]
for i in range(len(x[0,:])):
e.append(np.bincount(x[:,i]).argmin())
e = np.array(e)
print("Binary: ",end=" ")
print(e[:])
print("Decimal: ",end=" ")
es=""
es=''.join(str(n) for n in e)
print(int(es,2))
print("-------power consumption-------")
print("Power Consumption ",end="")
print(str(int(gs,2)*int(es,2)))
#print(np.array2string(g,precision=2, separator='',suppress_small=True))
#print()
exit()
print(np.base_repr("10110",10))
g1=np.bincount(x[:,0]).argmax()
g2=np.bincount(x[:,1]).argmax()
g3=np.bincount(x[:,2]).argmax()
g4=np.bincount(x[:,3]).argmax()
g5=np.bincount(x[:,4]).argmax()
exit()
with open("day3_test.txt", "r") as f:
x = [ j for j in x.split() ]
print(x)
pc=0
g=0
e=0
for i in range(len(x)):
direction=x[i][0]
temp=int(x[i][1])
if(direction=="up"):
#print("up "+ str(temp))
depth-=temp
elif(direction=="down"):
#print("down " + str(temp))
depth+=temp
elif(direction=="forward"):
#print("forward " + str(temp))
horizontal+=temp
else:
print("ERROR")
print("horizontal: "+str(horizontal))
print("depth: " + str(depth))
print("result: " + str(horizontal*depth))
#print(direction + " " + str(temp))
#print(temp,end=" ")
#print("("+str(type(temp))+")",end=" ")

320
2021/day3/day3_part2.py Normal file
View File

@ -0,0 +1,320 @@
import numpy as np
filename = open("day3_data.txt","r")
x = []
xr = filename.readline().strip()
while xr:
xr = list(map(int,str(xr)))
x.append(xr)
xr = filename.readline().strip()
x = np.array(x)
x2=x
print(x)
print(x[0][0])
print(x[0,0])
print("---------------")
#for i in range(len(x[:,0])):
# print(x[i][0])
print("------Gamma rate---------")
g=[]
for i in range(len(x[0,:])):
g.append(np.bincount(x[:,i]).argmax())
g = np.array(g)
print("Binary: ",end="")
print(g[:])
print("Decimal: ",end="")
gs=""
gs=''.join(str(n) for n in g)
print(int(gs,2))
print("---------Epsilon rate----------")
e=[]
for i in range(len(x[0,:])):
e.append(np.bincount(x[:,i]).argmin())
e = np.array(e)
print("Binary: ",end=" ")
print(e[:])
print("Decimal: ",end=" ")
es=""
es=''.join(str(n) for n in e)
print(int(es,2))
print("-------power consumption-------")
print("Power Consumption ",end="")
print(str(int(gs,2)*int(es,2)))
print("--------part 2---------------")
a = np.bincount(x[:,0])
print("There are: ")
print("0: " + str(a[0]))
print("1: " + str(a[1]))
keep=0
print("Column 1 keep ", end="")
if a[0] > a[1]:
keep=0
if a[0] < a[1]:
keep=1
if a[0] == a[1]:
keep=1
print(str(keep))
new_a = []
for i in x:
if i[0] == keep:
new_a.append(i)
new_a = np.array(new_a)
x = new_a
print("New array is: ")
print(new_a)
a = np.bincount(x[:,1])
print("There are: ")
print("0: " + str(a[0]))
print("1: " + str(a[1]))
keep=0
print("Column 2 keep ", end="")
if a[0] > a[1]:
keep=0
if a[0] < a[1]:
keep=1
if a[0] == a[1]:
keep=1
print(str(keep))
new_a = []
for i in x:
if i[1] == keep:
new_a.append(i)
new_a = np.array(new_a)
x = new_a
print("New array is: ")
print(new_a)
a = np.bincount(x[:,2])
print("There are: ")
print("0: " + str(a[0]))
print("1: " + str(a[1]))
keep=0
print("Column 3 keep ", end="")
if a[0] > a[1]:
keep=0
if a[0] < a[1]:
keep=1
if a[0] == a[1]:
keep=1
print(str(keep))
new_a = []
for i in x:
if i[2] == keep:
new_a.append(i)
new_a = np.array(new_a)
x = new_a
print("New array is: ")
print(new_a)
a = np.bincount(x[:,3])
print("There are: ")
print("0: " + str(a[0]))
print("1: " + str(a[1]))
keep=0
print("Column 4 keep ", end="")
if a[0] > a[1]:
keep=0
if a[0] < a[1]:
keep=1
if a[0] == a[1]:
keep=1
print(str(keep))
new_a = []
for i in x:
if i[3] == keep:
new_a.append(i)
new_a = np.array(new_a)
x = new_a
print("New array is: ")
print(new_a)
a = np.bincount(x[:,4])
print("There are: ")
print("0: " + str(a[0]))
print("1: " + str(a[1]))
keep=0
print("Column 5 keep ", end="")
if a[0] > a[1]:
keep=0
if a[0] < a[1]:
keep=1
if a[0] == a[1]:
keep=1
print(str(keep))
new_a = []
for i in x:
if i[4] == keep:
new_a.append(i)
new_a = np.array(new_a)
x = new_a
print("New array is: ")
print(new_a)
g = np.array(x[0])
print("Binary: ",end="")
print(g[:])
print("Decimal: ",end=" ")
ogr=""
ogr=''.join(str(n) for n in g)
print(int(ogr,2))
print("------find OXYGEN-----")
x=x2
for i in range(len(x[0,:])):
a = np.bincount(x[:,i])
keep=0
if a[0] > a[1]:
keep=0
if a[0] < a[1]:
keep=1
if a[0] == a[1]:
keep=1
new_a = []
for j in x:
if j[i] == keep:
new_a.append(j)
new_a = np.array(new_a)
x = new_a
print("new array is: ")
print(new_a)
print("TEST" + str(i) + " LEN: "+ str(len(x[:,0])))
if len(x[:,0]) == 1:
break
g = np.array(x[0])
print("Binary: ",end="")
print(g[:])
print("Decimal: ",end=" ")
og=""
og=''.join(str(n) for n in g)
print(int(og,2))
print("------find CO2-----")
x=x2
for i in range(len(x[0,:])):
a = np.bincount(x[:,i])
keep=0
if a[0] < a[1]:
keep=0
if a[0] > a[1]:
keep=1
if a[0] == a[1]:
keep=0
new_a = []
for j in x:
if j[i] == keep:
new_a.append(j)
new_a = np.array(new_a)
x = new_a
print("new array is: ")
print(new_a)
print("TEST" + str(i) + " LEN: "+ str(len(x[:,0])))
if len(x[:,0]) == 1:
break
g = np.array(x[0])
print("Binary: ",end="")
print(g[:])
print("Decimal: ",end=" ")
co2=""
co2=''.join(str(n) for n in g)
print(int(co2,2))
print("------ RESULT IS: -------")
print(str(int(co2,2)*int(og,2)))

75
2021/day4/day4_part1.py Normal file
View File

@ -0,0 +1,75 @@
import numpy as np
import re, sys
filename = open(sys.argv[1],"r")
x = []
xr = filename.readlines()[0].strip()
#print(filename.readline().rstrip())
#for i in filename.readlines():
# print (i.strip())
llist=[]
bingo_numbers=[]
with open(sys.argv[1]) as f:
# x = [ j for j in x.split() ]
#lines = f.readlines()
#lines = [line.rstrip() for line in lines]
for line in f:
if re.search("^[0-9]+,",line.rstrip()):
bingo_numbers = [int(i) for i in line.rstrip().split(',')]
else:
for x in line.rstrip().split(' '):
if x != '':
llist.append(x)
plates = len(llist)/25
numbers=np.zeros((int(plates),5,5))
bingo=np.zeros((int(plates),5,5))
for l in range(0,int(plates)):
for i in range(0,5):
for j in range(0,5):
numbers[l][i][j]=llist[0]
llist.pop(0)
#print()
#print()
print("----")
print(bingo_numbers)
force_break = False
called_numbers = []
winner = []
for k in range(0,len(bingo_numbers)):
for l in range(0,int(plates)):
for i in range(0,5):
for j in range(0,5):
if numbers[l][i][j] == bingo_numbers[0]:
bingo[l][i][j] = 1
if sum(bingo[l][i]) == 5:
winner = numbers[l]
force_break = True
break
called_numbers.append(bingo_numbers[0])
bingo_numbers.pop(0)
if force_break:
break
print(called_numbers)
print(winner)
winner_sum = 0
for i in winner:
for j in i:
if int(j) not in called_numbers:
winner_sum += int(j)
print(winner_sum*called_numbers[-1])

View File

@ -0,0 +1,13 @@
import numpy as np
import sys
n, *b = open(sys.argv[1])
b = np.loadtxt(b,int).reshape(-1,5,5)
for n in map(int, n.split(',')):
b[b == n] = -1
m = (b == -1)
win = (m.all(1) | m.all(2)).any(1)
if win.any():
print((b * ~m)[win].sum() * n)
b = b[~win]

View File

@ -0,0 +1,46 @@
import numpy as np
import re
import sys
import pprint
f = open(sys.argv[1],"r")
lines=[]
for line in f:
line = line.strip('\n').split(' ')
left = [int(x) for x in line[0].split(',')]
right = [int(x) for x in line[2].split(',')]
lines.append([left,right])
grid = [[0]* 1000 for i in range(1000)]
#grid = [[0]* 10 for i in range(10)]
for line in lines:
if line[0][0] == line[1][0]: #vertical
a = min(line[0][1],line[1][1])
b = max(line[0][1],line[1][1])
for i in range(a, b+1):
grid[line[0][0]][i] += 1
elif line[0][1] == line[1][1]: #horizontal
a = min(line[0][0],line[1][0])
b = max(line[0][0],line[1][0])
for i in range(a, b+1):
grid[i][line[0][1]] += 1
else:
if line[0][0] > line[1][0]:
line = [line[1], line[0]]
for i in range(line[1][0] - line[0][0] + 1):
v = i if line[1][1] > line[0][1] else -i
h = i if line[1][0] > line[0][0] else -i
grid[line[0][0] + h] [line[0][1] + v] += 1
count = 0
for i in grid:
for j in i:
if j > 1:
count += 1
print(count)
#pprint.pprint(grid)

23
2021/day6/day6_part1.py Normal file
View File

@ -0,0 +1,23 @@
import sys
f = open(sys.argv[1],"r")
for line in f:
line = line.strip().split(',')
line = [int(i) for i in line]
print("Initial state: " + str(line))
days = 80
for i in range(1,days+1):
for j in range(0,len(line)):
if line[j] == 0:
line[j] = 6
line.append(8)
elif line[j] >= 1 and line[j] <= 8:
line[j] -= 1
#print("After " + str(i).zfill(2) + " days: " + str(line))
print('After ' + str(days) + ' days there are ' + str(len(line)) + ' lanternfish')

19
2022/day1/day1_part1.py Normal file
View File

@ -0,0 +1,19 @@
with open('input') as f:
list_array = f.readlines()
sun_array = []
count = 0
temp = 0
max = 0
for x in list_array:
if x != '\n':
temp += int(x)
else:
print('total: ' + str(temp))
if temp > max: max = temp
temp = 0
print('total: ' + str(temp))
print('The Elf with the most Calories carry ' + str(max))

23
2022/day1/day1_part2.py Normal file
View File

@ -0,0 +1,23 @@
with open('input') as f:
list_array = f.readlines()
sun_array = []
count = 0
temp = 0
max = 0
top3 = []
for x in list_array:
if x != '\n':
temp += int(x)
else:
#print('total: ' + str(temp))
if temp > max: max = temp
top3.append(temp)
temp = 0
#print('total: ' + str(temp))
top3.append(temp)
print('The Elf with the most Calories carry ' + str(max))
print('The top 3 Elves are carrying ' + str(sum(sorted(top3,reverse=True)[:3])) + ' Calories')

39
2022/day2/day2_part1.py Normal file
View File

@ -0,0 +1,39 @@
arr = []
with open('input') as f:
for l in f:
arr.append(l.split())
score = 0
total_score = 0
for x in arr:
#print(x)
if x[0] == 'A':
if x[1] == 'X':
score += (1+3)
elif x[1] == 'Y':
score += (2+6)
elif x[1] == 'Z':
score += (3+0)
if x[0] == 'B':
if x[1] == 'X':
score += (1+0)
elif x[1] == 'Y':
score += (2+3)
elif x[1] == 'Z':
score += (3+6)
if x[0] == 'C':
if x[1] == 'X':
score += (1+6)
elif x[1] == 'Y':
score += (2+0)
elif x[1] == 'Z':
score += (3+3)
#print (score)
total_score += score
score = 0
print(total_score)

40
2022/day2/day2_part2.py Normal file
View File

@ -0,0 +1,40 @@
import sys
arr = []
with open(sys.argv[1]) as f:
for l in f:
arr.append(l.split())
score = 0
total_score = 0
for x in arr:
#print(x)
if x[0] == 'A':
if x[1] == 'X':
score = (3+0)
elif x[1] == 'Y':
score = (1+3)
elif x[1] == 'Z':
score = (2+6)
if x[0] == 'B':
if x[1] == 'X':
score = (1+0)
elif x[1] == 'Y':
score = (2+3)
elif x[1] == 'Z':
score = (3+6)
if x[0] == 'C':
if x[1] == 'X':
score = (2+0)
elif x[1] == 'Y':
score = (3+3)
elif x[1] == 'Z':
score = (1+6)
#print (score)
total_score += score
score = 0
print(total_score)

18
2022/day3/day3_part1.py Normal file
View File

@ -0,0 +1,18 @@
import sys
import numpy as np
import pprint, string
arr = []
alpha=list(string.ascii_letters)
values=[]
with open(sys.argv[1]) as f:
for l in f:
line=l.split()[0]
first, second = line[:len(line)//2],line[len(line)//2:]
shared=list(set(first)&set(second))[0]
index=alpha.index(shared)+1
values.append(index)
arr.append([first,second,shared,int(index)])
#pprint.pprint(arr)
print(sum(values))

21
2022/day3/day3_part2.py Normal file
View File

@ -0,0 +1,21 @@
import sys, pprint, string
arr = []
alpha=list(string.ascii_letters)
values_part1=[]
values_part2=[]
with open(sys.argv[1]) as f:
for l in f:
line=l.split()[0]
first, second = line[:len(line)//2],line[len(line)//2:]
shared=list(set(first)&set(second))[0]
index=alpha.index(shared)+1
values_part1.append(index)
arr.append([line,first,second,shared,int(index)])
print(sum(values_part1))
for i in range(0,len(arr),3):
shared=list(set(arr[i][0])&set(arr[i+1][0])&set(arr[i+2][0]))[0]
values_part2.append(int(alpha.index(shared)+1))
print(sum(values_part2))

53
2022/day4/day4_part1.py Normal file
View File

@ -0,0 +1,53 @@
import sys
from pprint import pprint
from functools import reduce
from operator import and_
with open(sys.argv[1],"r") as f:
line = [line.rstrip('\n').split(',') for line in f]
lines=[]
def contains(superset, subset) -> bool:
# creates a list of boolean values and
# combines them using the and operator
return reduce(and_, [i in superset for i in subset])
for i in line:
lines.append([i[0].split('-'),i[1].split('-')])
for i in range(0, len(lines)):
temp = []
if int(lines[i][0][0]) == int(lines[i][0][1]):
temp = [int(lines[i][0][0])]
else:
for j in range(int(lines[i][0][0]),int(lines[i][0][1])+1):
temp.append(j)
lines[i].append(temp)
temp = []
if int(lines[i][1][0]) == int(lines[i][1][1]):
temp = [int(lines[i][1][0])]
else:
for j in range(int(lines[i][1][0]),int(lines[i][1][1])+1):
temp.append(j)
lines[i].append(temp)
temp = []
superset = lines[i][2]
subset = lines[i][3]
if contains(superset,subset):
lines[i].append(True)
elif contains(subset,superset):
lines[i].append(True)
else:
lines[i].append(False)
count=0
for i in range(0,len(lines)):
if lines[i][4]:
count+=1
print(count)

52
2022/day4/day4_part2.py Normal file
View File

@ -0,0 +1,52 @@
import sys
from pprint import pprint
from functools import reduce
from operator import and_
with open(sys.argv[1],"r") as f:
line = [line.rstrip('\n').split(',') for line in f]
lines=[]
def contains(A, B):
n = len(A)
return any(A == B[i:i + n] for i in range(len(B)-n + 1))
for i in line:
lines.append([i[0].split('-'),i[1].split('-')])
for i in range(0, len(lines)):
temp = []
if int(lines[i][0][0]) == int(lines[i][0][1]):
temp = [int(lines[i][0][0])]
else:
for j in range(int(lines[i][0][0]),int(lines[i][0][1])+1):
temp.append(j)
lines[i].append(temp)
temp = []
if int(lines[i][1][0]) == int(lines[i][1][1]):
temp = [int(lines[i][1][0])]
else:
for j in range(int(lines[i][1][0]),int(lines[i][1][1])+1):
temp.append(j)
lines[i].append(temp)
temp = []
superset = lines[i][2]
subset = lines[i][3]
if any(item in superset for item in subset):
lines[i].append(True)
elif any(item in subset for item in superset):
lines[i].append(True)
else:
lines[i].append(False)
count=0
for i in range(0,len(lines)):
if lines[i][4]:
count+=1
print(count)

45
2022/day5/day5_part1.py Normal file
View File

@ -0,0 +1,45 @@
import sys
from pprint import pprint
arr = []
if sys.argv[1] == "test_input":
stacks = [['N','Z'],['D', 'C','M'], ['P']]
else:
stacks = [['Q','F','L','S','R'],['T','P','G','Q','Z','N'],['B','Q','M','S'],['Q','B','C','H','J','Z','G','T'],['S','F','N','B','M','H','P'],['G','V','L','S','N','Q','C','P'],['F','C','W'],['M','P','V','W','Z','G','H','Q'],['R','N','C','L','D','Z','G']]
with open(sys.argv[1],"r") as f:
line = [line.rstrip('\n') for line in f]
for i in range(0,len(line)):
arr.append([int(x) for x in line[i].split() if x.isdigit()])
pprint(arr)
pprint(stacks)
print('----')
for i in arr:
temp = []
#print('pre: ' + str(stacks))
#print('move ' + str(i[0]) + ' letter from stack ' + str(i[1]) + ' to stack ' + str(i[2]) )
for j in range(0,i[0]):
#print('range: ' + str(0) + ' to ' + str(i[0]))
#print('move ' + str(stacks[i[1]-1][0] + ' from '), end="")
#print('' + str(stacks[i[1]-1]), end="")
#print(' to ' + str(stacks[i[2]-1]))
temp += stacks[i[1]-1].pop(0)
if len(temp) > 1:
temp.reverse()
#print('stacks:: ' + str(temp))
#print(i[0])
if len(temp) != 0:
for l in temp:
stacks[i[2]-1].insert(0,l)
else:
print('somehow temp length is 0')
temp = []
#print('post: ' + str(stacks))
for i in stacks:
print(i[0],end="")
print()

23
2022/day6/day6_part1.py Normal file
View File

@ -0,0 +1,23 @@
import sys
from pprint import pprint
from collections import Counter
with open(sys.argv[1], "r") as f:
line = [line.rstrip('\n') for line in f]
print(line[0])
letters = line[0]
def find_dupe(input):
WC = Counter(input)
for letter, count in WC.items():
if (count > 1):
return True
for i in range(0,len(letters)-3):
temp=""
for j in range(0,4):
temp += letters[i+j]
if not find_dupe(temp):
print(temp)
print(i+4)
break

23
2022/day6/day6_part2.py Normal file
View File

@ -0,0 +1,23 @@
import sys
from pprint import pprint
from collections import Counter
with open(sys.argv[1], "r") as f:
line = [line.rstrip('\n') for line in f]
print(line[0])
letters = line[0]
def find_dupe(input):
WC = Counter(input)
for letter, count in WC.items():
if (count > 1):
return True
for i in range(0,len(letters)-13):
temp=""
for j in range(0,14):
temp += letters[i+j]
if not find_dupe(temp):
print(temp)
print(i+14)
break

30
2022/day7/day7_part1.py Normal file
View File

@ -0,0 +1,30 @@
from collections import defaultdict
import sys
def main():
lines = map(str.split, open(sys.argv[1]).read().splitlines())
path, dirs = [], defaultdict(int)
for l in lines:
if l[0] == "$":
if l[1] == "cd":
if l[2] == "..":
path.pop()
else:
path.append(l[2])
elif l[0] != "dir":
for i in range(len(path)):
dirs[tuple(path[: i + 1])] += int(l[0])
print(sum(size for size in dirs.values() if size <= 100000))
required = 30000000 - (70000000 - dirs[("/",)])
print(min(size for size in dirs.values() if size >= required))
if __name__ == '__main__':
main()

58
2022/day8/day8_part1.py Normal file
View File

@ -0,0 +1,58 @@
import sys
from pprint import pprint
with open(sys.argv[1],'r') as f:
lines = [[tree for tree in lines.rstrip('\n')] for lines in f]
forrest = [['O' for i in range(len(lines))] for j in range(len(lines[0]))]
for i in range(0,len(lines)):
for j in range(0,len(lines[i])):
forrest[i][j] = 'O'
for i in range(0,len(lines)):
for j in range(0,len(lines[i])):
print(lines[i][j],end='')
print('(' + str(i) + ',' + str(j)+ ') ',end='')
print()
for i in range(0,len(lines)):
for j in range(0,len(lines[i])):
#print(lines[i][j],end='')
#print('(' + str(i) + ',' + str(j)+ ') ',end='')
if (i > 0 and i < len(lines)-1) and (j > 0 and j < len(lines[i])-1):
#check up
print('-> Checking: ' + str(lines[i][j]) + '(' + str(i) + ',' + str(j) + ')')
for l in range(0,i): #check up
if forrest[i][j] == 'X':
break
#print('Range (up) : ' + forrest[i][j] +'>> ' + '0-' + str(i-1) + ' - ' + str(lines[i][j]) + '(' + str(i) + ',' + str(j)+ ')' + ' <=> ' + str(lines[l][j]) + '(' + str(l) + ',' + str(j)+ ') ')
if (lines[i][j] <= lines[l][j]) and forrest[i][j] != 'X':
for k in range(0,j): #check left
if forrest[i][j] == 'X':
break
#print('Range (left): ' + forrest[i][j] +'>> ' + '0-' + str(j-1) + ' - ' + str(lines[i][j]) + '(' + str(i) + ',' + str(j)+ ')' + ' <=> ' + str(lines[i][k]) + '(' + str(i) + ',' + str(k)+ ') ')
if lines[i][j] <= lines[i][k] and forrest[i][j] != 'X':
for p in range(i+1,len(lines)): #check down
if forrest[i][j] == 'X':
break
#print('Range (down): ' + forrest[i][j] +'>> ' + str(i+1) + '-' + str(len(lines)) + ' - ' + str(lines[i][j]) + '(' + str(i) + ',' + str(j)+ ')' + ' <=> ' + str(lines[p][j]) + '(' + str(p) + ',' + str(j)+ ') ')
if lines[i][j] <= lines[p][j] and forrest[i][j] != 'X':
for q in range(j+1,len(lines[0])): #check right
if forrest[i][j] == 'X':
break
#print('Range (right): ' + forrest[i][j] + '>> ' + str(j+1) + '-' + str(len(lines[0])) + ' - ' + str(lines[i][j]) + '(' + str(i) + ',' + str(j)+ ')' + ' <=> ' + str(lines[i][q]) + '(' + str(i) + ',' + str(q)+ ') ')
if lines[i][j] <= lines[i][q] and forrest[i][j] != 'X':
forrest[i][j] = 'X'
#print('adding X')
count=0
for i in forrest:
for j in i:
if j == 'O':
count+=1
print(count)

58
2022/day8/day8_part2.py Normal file
View File

@ -0,0 +1,58 @@
import sys
from pprint import pprint
with open(sys.argv[1],'r') as f:
lines = [[tree for tree in lines.rstrip('\n')] for lines in f]
forrest = [['O' for i in range(len(lines))] for j in range(len(lines[0]))]
for i in range(0,len(lines)):
for j in range(0,len(lines[i])):
forrest[i][j] = 'O'
for i in range(0,len(lines)):
for j in range(0,len(lines[i])):
print(lines[i][j],end='')
print('(' + str(i) + ',' + str(j)+ ') ',end='')
print()
for i in range(0,len(lines)):
for j in range(0,len(lines[i])):
#print(lines[i][j],end='')
#print('(' + str(i) + ',' + str(j)+ ') ',end='')
if (i > 0 and i < len(lines)-1) and (j > 0 and j < len(lines[i])-1):
#check up
print('-> Checking: ' + str(lines[i][j]) + '(' + str(i) + ',' + str(j) + ')')
for l in range(0,i): #check up
print('Range (up) : ' + forrest[i][j] +'>> ' + '0-' + str(i-1) + ' - ' + str(lines[i][j]) + '(' + str(i) + ',' + str(j)+ ')' + ' <=> ' + str(lines[l][j]) + '(' + str(l) + ',' + str(j)+ ') ')
if forrest[i][j] == 'X':
break
if (lines[i][j] <= lines[l][j]):
for k in range(0,j): #check left
print('Range (left): ' + forrest[i][j] +'>> ' + '0-' + str(j-1) + ' - ' + str(lines[i][j]) + '(' + str(i) + ',' + str(j)+ ')' + ' <=> ' + str(lines[i][k]) + '(' + str(i) + ',' + str(k)+ ') ')
if forrest[i][j] == 'X':
break
if lines[i][j] <= lines[i][k]:
for p in range(i+1,len(lines)): #check down
print('Range (down): ' + forrest[i][j] +'>> ' + str(i+1) + '-' + str(len(lines)) + ' - ' + str(lines[i][j]) + '(' + str(i) + ',' + str(j)+ ')' + ' <=> ' + str(lines[p][j]) + '(' + str(p) + ',' + str(j)+ ') ')
if forrest[i][j] == 'X':
break
if lines[i][j] <= lines[p][j]:
for q in range(j+1,len(lines[0])): #check right
print('Range (right): ' + forrest[i][j] + '>> ' + str(j+1) + '-' + str(len(lines[0])) + ' - ' + str(lines[i][j]) + '(' + str(i) + ',' + str(j)+ ')' + ' <=> ' + str(lines[i][q]) + '(' + str(i) + ',' + str(q)+ ') ')
if forrest[i][j] == 'X':
break
if lines[i][j] <= lines[i][q]:
forrest[i][j] = 'X'
#print('adding X')
count=0
for i in forrest:
for j in i:
if j == 'O':
count+=1
print(count)