Added old 2022 AoC solves

This commit is contained in:
FrederikBaerentsen 2023-12-09 22:06:15 +01:00
parent be08eff4ed
commit d3bd335e2b
14 changed files with 502 additions and 0 deletions

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)