From be08eff4edc9cb0032c8144fc9b199b0e95c4fe2 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Sat, 9 Dec 2023 22:01:20 +0100 Subject: [PATCH] Added old 2021 AoC solves --- 2021/day1/day1.py | 19 +++ 2021/day2/day2.py | 38 +++++ 2021/day2/day2_part2.py | 43 +++++ 2021/day3/day3_part1.py | 152 ++++++++++++++++++ 2021/day3/day3_part2.py | 320 +++++++++++++++++++++++++++++++++++++ 2021/day4/day4_part1.py | 75 +++++++++ 2021/day4/day4_part2_v2.py | 13 ++ 2021/day5/day5_part1_v2.py | 46 ++++++ 2021/day6/day6_part1.py | 23 +++ 9 files changed, 729 insertions(+) create mode 100644 2021/day1/day1.py create mode 100644 2021/day2/day2.py create mode 100644 2021/day2/day2_part2.py create mode 100644 2021/day3/day3_part1.py create mode 100644 2021/day3/day3_part2.py create mode 100644 2021/day4/day4_part1.py create mode 100644 2021/day4/day4_part2_v2.py create mode 100644 2021/day5/day5_part1_v2.py create mode 100644 2021/day6/day6_part1.py diff --git a/2021/day1/day1.py b/2021/day1/day1.py new file mode 100644 index 0000000..4a0d92b --- /dev/null +++ b/2021/day1/day1.py @@ -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") + diff --git a/2021/day2/day2.py b/2021/day2/day2.py new file mode 100644 index 0000000..d45f085 --- /dev/null +++ b/2021/day2/day2.py @@ -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=" ") diff --git a/2021/day2/day2_part2.py b/2021/day2/day2_part2.py new file mode 100644 index 0000000..64a4afa --- /dev/null +++ b/2021/day2/day2_part2.py @@ -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=" ") diff --git a/2021/day3/day3_part1.py b/2021/day3/day3_part1.py new file mode 100644 index 0000000..c685819 --- /dev/null +++ b/2021/day3/day3_part1.py @@ -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=" ") diff --git a/2021/day3/day3_part2.py b/2021/day3/day3_part2.py new file mode 100644 index 0000000..0453149 --- /dev/null +++ b/2021/day3/day3_part2.py @@ -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))) diff --git a/2021/day4/day4_part1.py b/2021/day4/day4_part1.py new file mode 100644 index 0000000..256c673 --- /dev/null +++ b/2021/day4/day4_part1.py @@ -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]) + diff --git a/2021/day4/day4_part2_v2.py b/2021/day4/day4_part2_v2.py new file mode 100644 index 0000000..0fedd1c --- /dev/null +++ b/2021/day4/day4_part2_v2.py @@ -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] diff --git a/2021/day5/day5_part1_v2.py b/2021/day5/day5_part1_v2.py new file mode 100644 index 0000000..5062d35 --- /dev/null +++ b/2021/day5/day5_part1_v2.py @@ -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) diff --git a/2021/day6/day6_part1.py b/2021/day6/day6_part1.py new file mode 100644 index 0000000..a2f06fd --- /dev/null +++ b/2021/day6/day6_part1.py @@ -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') + +