Skip to content

Commit 752518c

Browse files
committedDec 16, 2021
added days 1-5 of advent of code 2021
1 parent 750a32c commit 752518c

File tree

7 files changed

+325
-0
lines changed

7 files changed

+325
-0
lines changed
 

‎advent_of_code_2021/day1.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Task 1
2+
3+
nums = []
4+
5+
with open('day1input') as f:
6+
for line in f:
7+
nums.append(int(line))
8+
9+
greater_than_prev = 0
10+
11+
for i in range(1, len(nums)):
12+
if nums[i]>nums[i-1]:
13+
greater_than_prev+=1
14+
15+
print('===Part 1===')
16+
print(greater_than_prev)
17+
18+
## Task 2
19+
20+
greater_than_prev = 0
21+
22+
for i in range(2, len(nums)-1):
23+
if nums[i]+nums[i-1]+nums[i+1]>nums[i-1]+nums[i]+nums[i-2]:
24+
greater_than_prev+=1
25+
26+
print('===Part 2===')
27+
print(greater_than_prev)

‎advent_of_code_2021/day2.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
horizontal = 0
2+
depth = 0
3+
4+
with open('day2input') as f:
5+
for line in f:
6+
parse = line.split()
7+
if parse[0]=="forward":
8+
horizontal+=int(parse[1])
9+
elif parse[0]=="up":
10+
depth-=int(parse[1])
11+
else:
12+
depth+=int(parse[1])
13+
14+
print('===Part 1===')
15+
print(depth*horizontal)
16+
17+
horizontal = 0
18+
depth = 0
19+
aim = 0
20+
21+
with open('day2input') as f:
22+
for line in f:
23+
parse = line.split()
24+
if parse[0]=="forward":
25+
horizontal+=int(parse[1])
26+
depth += aim * int(parse[1])
27+
elif parse[0]=="up":
28+
aim-=int(parse[1])
29+
else:
30+
aim+=int(parse[1])
31+
32+
print('===Part 1===')
33+
print(depth*horizontal)

‎advent_of_code_2021/day3.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
input = []
2+
3+
with open('day3input') as f:
4+
for line in f:
5+
input.append(line.strip())
6+
7+
gamma = ""
8+
epsilon = ""
9+
10+
#most common goes in gamma
11+
12+
zero_count = [0]*len(input[0])
13+
14+
for s in input:
15+
for i, x in enumerate(s):
16+
if x=="0":
17+
zero_count[i]+=1
18+
19+
for i in range(len(zero_count)):
20+
if zero_count[i]>len(input)/2:
21+
gamma+="1"
22+
epsilon+="0"
23+
else:
24+
gamma+="0"
25+
epsilon+="1"
26+
27+
print('===Part 1===')
28+
print(int(gamma, 2)*int(epsilon, 2))
29+
30+
#Part 2 Start!
31+
32+
iteration = 0
33+
oxygen_vals = input
34+
while len(oxygen_vals)!=1:
35+
one_count = 0
36+
#find most common bit
37+
for val in oxygen_vals:
38+
if val[iteration]=="1":
39+
one_count+=1
40+
if one_count>= len(oxygen_vals) /2:
41+
#keep numbers where val[iteration]=1
42+
oxygen_vals = list(filter(lambda x: x[iteration] == "1", oxygen_vals))
43+
else:
44+
oxygen_vals = list(filter(lambda x: x[iteration] == "0", oxygen_vals))
45+
iteration+=1
46+
oxygen_val = oxygen_vals[0]
47+
48+
iteration = 0
49+
co_vals = input
50+
while len(co_vals)!=1:
51+
one_count = 0
52+
#find most common bit
53+
for val in co_vals:
54+
if val[iteration]=="1":
55+
one_count+=1
56+
if one_count < len(co_vals) /2:
57+
#keep numbers where val[iteration]=1
58+
co_vals = list(filter(lambda x: x[iteration] == "1", co_vals))
59+
else:
60+
co_vals = list(filter(lambda x: x[iteration] == "0", co_vals))
61+
iteration+=1
62+
63+
co_val = co_vals[0]
64+
65+
print('===Part 2===')
66+
print( int(oxygen_val, 2)*int(co_val, 2) )

‎advent_of_code_2021/day4_part1.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
draws = []
2+
boards = []
3+
current_draws = []
4+
5+
def checkBoard(b):
6+
#check rows
7+
for r in b:
8+
row_set = set(r)
9+
draw_set = set(current_draws)
10+
if len(row_set.intersection(draw_set))==5:
11+
return r
12+
13+
#check columns
14+
#generate array of column
15+
for c in range(5):
16+
col = []
17+
for i in range(5):
18+
col.append(b[i][c])
19+
col_set = set(col)
20+
draw_set = set(current_draws)
21+
if len(col_set.intersection(draw_set))==5:
22+
return col
23+
return []
24+
25+
def sumOfUnmarked(b):
26+
sum = 0
27+
for r in b:
28+
for num in r:
29+
if num not in current_draws:
30+
sum+=num
31+
return sum
32+
33+
def partOne():
34+
with open('day4input') as f:
35+
lines = f.readlines()
36+
draw_input = lines[0].split(',')
37+
draws = [int(x.strip()) for x in draw_input]
38+
for i in range(2, len(lines), 6):
39+
b = []
40+
for j in range(i, i+5):
41+
b.append( [int(s.strip()) for s in lines[j].split()] )
42+
boards.append(b)
43+
for draw in draws:
44+
current_draws.append(draw)
45+
for b in boards:
46+
a = checkBoard(b)
47+
if a!=[]:
48+
return sumOfUnmarked(b)*draw
49+
50+
def main():
51+
print('===Part One===')
52+
print(partOne())
53+
54+
main()

‎advent_of_code_2021/day4_part2.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
draws = []
2+
boards = []
3+
current_draws = []
4+
seen = []
5+
6+
def checkBoard(b):
7+
#check rows
8+
for r in b:
9+
row_set = set(r)
10+
draw_set = set(current_draws)
11+
if len(row_set.intersection(draw_set))==5:
12+
return r
13+
14+
#check columns
15+
#generate array of column
16+
for c in range(5):
17+
col = []
18+
for i in range(5):
19+
col.append(b[i][c])
20+
col_set = set(col)
21+
draw_set = set(current_draws)
22+
if len(col_set.intersection(draw_set))==5:
23+
return col
24+
return []
25+
26+
def sumOfUnmarked(b):
27+
sum = 0
28+
for r in b:
29+
for num in r:
30+
if num not in current_draws:
31+
sum+=num
32+
return sum
33+
34+
def partTwo():
35+
with open('day4input') as f:
36+
lines = f.readlines()
37+
draw_input = lines[0].split(',')
38+
draws = [int(x.strip()) for x in draw_input]
39+
for i in range(2, len(lines), 6):
40+
b = []
41+
for j in range(i, i+5):
42+
b.append( [int(s.strip()) for s in lines[j].split()] )
43+
boards.append(b)
44+
for draw in draws:
45+
current_draws.append(draw)
46+
for b in boards:
47+
a = checkBoard(b)
48+
if a!=[]:
49+
if b not in seen:
50+
last = sumOfUnmarked(b)*draw
51+
seen.append(b)
52+
return last
53+
54+
def main():
55+
print('===Part Two===')
56+
print(partTwo())
57+
58+
main()

‎advent_of_code_2021/day5_part1.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
input = []
2+
maxX = 0
3+
maxY = 0
4+
5+
with open('day5input') as f:
6+
for line in f:
7+
p = line.strip().split(' -> ')
8+
x1 = int(p[0].split(',')[0])
9+
y1 = int(p[0].split(',')[1])
10+
x2 = int(p[1].split(',')[0])
11+
y2 = int(p[1].split(',')[1])
12+
13+
if x1==x2 or y1==y2:
14+
input.append( ((x1, y1), (x2, y2)) )
15+
maxX = max(maxX, x1, x2)
16+
maxY = max(maxY, y1, y2)
17+
18+
grid = []
19+
for x in range(maxY+1):
20+
grid.append( [0]*(maxX+1))
21+
22+
23+
for op in input:
24+
x1 = op[0][0]
25+
y1 = op[0][1]
26+
x2 = op[1][0]
27+
y2 = op[1][1]
28+
if x1==x2:
29+
for i in range(min(y1, y2), max(y1, y2)+1):
30+
grid[i][x1]+=1
31+
else:
32+
for i in range( min(x1, x2), max(x1, x2)+1):
33+
grid[y1][i]+=1
34+
count = 0
35+
36+
for row in grid:
37+
for elem in row:
38+
if elem>=2:
39+
count+=1
40+
41+
print(count)

‎advent_of_code_2021/day5_part2.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
input = []
2+
maxX = 0
3+
maxY = 0
4+
5+
with open('day5input') as f:
6+
for line in f:
7+
p = line.strip().split(' -> ')
8+
x1 = int(p[0].split(',')[0])
9+
y1 = int(p[0].split(',')[1])
10+
x2 = int(p[1].split(',')[0])
11+
y2 = int(p[1].split(',')[1])
12+
13+
input.append( ((x1, y1), (x2, y2)) )
14+
maxX = max(maxX, x1, x2)
15+
maxY = max(maxY, y1, y2)
16+
17+
grid = []
18+
for x in range(maxY+1):
19+
grid.append( [0]*(maxX+1))
20+
21+
22+
for op in input:
23+
x1 = op[0][0]
24+
y1 = op[0][1]
25+
x2 = op[1][0]
26+
y2 = op[1][1]
27+
if x1==x2:
28+
for i in range(min(y1, y2), max(y1, y2)+1):
29+
grid[i][x1]+=1
30+
elif y1==y2:
31+
for i in range( min(x1, x2), max(x1, x2)+1):
32+
grid[y1][i]+=1
33+
elif x1 < x2 and y1 < y2 or x2<x1 and y2<y1:
34+
for i in range(0, max(x1, x2) - min(x1, x2)+1):
35+
grid[min(y2, y1)+i][min(x2, x1)+i]+=1
36+
else:
37+
for i in range(0, max(x1, x2) - min(x1, x2)+1):
38+
grid[max(y1, y2)-i][min(x1, x2)+i]+=1
39+
40+
41+
count = 0
42+
for row in grid:
43+
for elem in row:
44+
if elem>=2:
45+
count+=1
46+
print(count)

0 commit comments

Comments
 (0)