Skip to content

Commit 1ce02dc

Browse files
authored
Add files via upload
added previously done days
0 parents  commit 1ce02dc

File tree

5 files changed

+261
-0
lines changed

5 files changed

+261
-0
lines changed

Day 1.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
inp = [int(i) for i in open('Day 1.txt','r').read().split('\n')]
2+
test = '''199
3+
200
4+
208
5+
210
6+
200
7+
207
8+
240
9+
269
10+
260
11+
263'''
12+
test = [int(i) for i in test.split('\n')]
13+
14+
def part1(inp):
15+
inc = 0
16+
for i in range(len(inp)-1):
17+
if inp[i+1] > inp[i]:
18+
inc+=1
19+
20+
print(inc)
21+
22+
def part2(inp):
23+
inc = 0
24+
for i in range(len(inp)-3):
25+
if inp[i+1] + inp[i+2] + inp[i+3] > inp[i] +inp[i+1] + inp[i+2]:
26+
inc +=1
27+
print(inc)
28+
29+
part1(inp)
30+
part2(inp)
31+

Day 2.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
test='''forward 5
2+
down 5
3+
forward 8
4+
up 3
5+
down 8
6+
forward 2'''.split('\n')
7+
inp = open('Day 2.txt','r').read().split('\n')
8+
9+
def part1(inp):
10+
x = y = 0
11+
for s in inp:
12+
mag = int(s.split(' ')[1])
13+
match s.split(' ')[0]:
14+
case 'forward': x += mag
15+
case 'up': y -= mag
16+
case 'down': y += mag
17+
print(x*y)
18+
def part2(inp):
19+
x = y = aim = 0
20+
for s in inp:
21+
mag = int(s.split(' ')[1])
22+
match s.split(' ')[0]:
23+
case 'forward':
24+
x += mag
25+
y += aim * mag
26+
case 'up': aim -= mag
27+
case 'down': aim += mag
28+
print(x*y)
29+
30+
part1(inp)
31+
part2(inp)
32+

Day 3.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
test='''00100
2+
11110
3+
10110
4+
10111
5+
10101
6+
01111
7+
00111
8+
11100
9+
10000
10+
11001
11+
00010
12+
01010'''.split('\n')
13+
inp = open('Day 3.txt','r').read().split('\n')
14+
15+
def part1(inp):
16+
gamma = ''
17+
epsilon = ''
18+
for i in range(len(inp[0])):
19+
one = 0
20+
zero= 0
21+
for x in inp:
22+
if x[i] == '0': zero += 1
23+
else: one += 1
24+
if zero > one:
25+
gamma += '0'
26+
epsilon += '1'
27+
else:
28+
gamma += '1'
29+
epsilon += '0'
30+
print('Gamma:',gamma)
31+
print('Epsilon:',epsilon)
32+
print('Power Consumption:',int(gamma,2) * int(epsilon,2))
33+
print('')
34+
35+
36+
def part2(inp):
37+
oxlist = inp
38+
for i in range(len(inp[0])):
39+
oxone = 0
40+
oxzero= 0
41+
oxkeep= 0
42+
for x in oxlist:
43+
if x[i] == '0': oxzero += 1
44+
else: oxone += 1
45+
if oxzero <= oxone: oxkeep = 1
46+
oxlist = [x for x in oxlist if x[i] == str(oxkeep)]
47+
if len(oxlist) == 1: break
48+
colist = inp
49+
for i in range(len(inp[0])):
50+
coone = 0
51+
cozero= 0
52+
cokeep= 0
53+
for x in colist:
54+
if x[i] == '0': cozero += 1
55+
else: coone += 1
56+
if cozero > coone: cokeep = 1
57+
colist = [x for x in colist if x[i] == str(cokeep)]
58+
if len(colist) == 1: break
59+
print('Oxygen Rating:',oxlist[0])
60+
print('CO2 Rating:',colist[0])
61+
print('Life Support Rating:',int(oxlist[0],2) * int(colist[0],2))
62+
print('')
63+
64+
65+
part1(inp)
66+
part2(inp)

Day 4.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
test = '''7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
2+
3+
22 13 17 11 0
4+
8 2 23 4 24
5+
21 9 14 16 7
6+
6 10 3 18 5
7+
1 12 20 15 19
8+
9+
3 15 0 2 22
10+
9 18 13 17 5
11+
19 8 7 25 23
12+
20 11 10 24 4
13+
14 21 16 12 6
14+
15+
14 21 17 24 4
16+
10 16 15 9 19
17+
18 8 23 26 20
18+
22 11 13 6 5
19+
2 0 12 3 7'''
20+
inp = open('Day 4.txt','r').read()
21+
22+
def win(card,drawn):
23+
for i in range(5):
24+
if card[i] in drawn and card[i+5] in drawn and card[i+10] in drawn and card[i+15] in drawn and card[i+20] in drawn:
25+
return True
26+
27+
for i in range(0,25,5):
28+
if card[i] in drawn and card[i+1] in drawn and card[i+2] in drawn and card[i+3] in drawn and card[i+4] in drawn:
29+
return True
30+
return False
31+
32+
33+
def part1(inp):
34+
numbers = inp.split('\n')[0].split(',')
35+
cards = [i.split() for i in inp.split('\n\n')[1:]]
36+
drawn = []
37+
done = False
38+
for i in numbers:
39+
drawn.append(i)
40+
for card in cards:
41+
if win(card, drawn) == True:
42+
print(sum([int(i) for i in card if i not in drawn])*int(i))
43+
done = True
44+
break
45+
if done: break
46+
47+
def part2(inp):
48+
numbers = inp.split('\n')[0].split(',')
49+
cards = [i.split() for i in inp.split('\n\n')[1:]]
50+
drawn = []
51+
for i in numbers:
52+
drawn.append(i)
53+
for card in cards:
54+
if win(card, drawn) == True:
55+
latest = sum([int(i) for i in card if i not in drawn])*int(i)
56+
cards.remove(card)
57+
print(latest)
58+
59+
part1(inp)
60+
part2(inp)

Day 5.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
test = '''0,9 -> 5,9
2+
8,0 -> 0,8
3+
9,4 -> 3,4
4+
2,2 -> 2,1
5+
7,0 -> 7,4
6+
6,4 -> 2,0
7+
0,9 -> 2,9
8+
3,4 -> 1,4
9+
0,0 -> 8,8
10+
5,5 -> 8,2'''.split('\n')
11+
inp = open('Day 5.txt','r').read().split('\n')
12+
13+
def part1(inp):
14+
field = [[0 for y in range(1000)] for x in range(1000)]
15+
for i in inp:
16+
x1 = int(i.split(',')[0])
17+
x2 = int(i.split(',')[1].split(' ')[2])
18+
y1 = int(i.split(',')[1].split(' ')[0])
19+
y2 = int(i.split(',')[2])
20+
if x1 == x2:
21+
x = x1
22+
for y in range(y1,y2+(1 if y1 < y2 else -1),(1 if y1 < y2 else -1)):
23+
field[x][y] += 1
24+
elif y1 == y2:
25+
y = y1
26+
for x in range(x1,x2+(1 if x1 < x2 else -1),(1 if x1 < x2 else -1)):
27+
field[x][y] += 1
28+
intersect = 0
29+
for y in range(len(field)):
30+
for x in range(len(field)):
31+
if field[x][y] > 1:
32+
intersect += 1
33+
print(intersect)
34+
35+
36+
37+
def part2(inp):
38+
field = [[0 for y in range(1000)] for x in range(1000)]
39+
for i in inp:
40+
x1 = int(i.split(',')[0])
41+
x2 = int(i.split(',')[1].split(' ')[2])
42+
y1 = int(i.split(',')[1].split(' ')[0])
43+
y2 = int(i.split(',')[2])
44+
if x1 == x2:
45+
x = x1
46+
for y in range(y1,y2+(1 if y1 < y2 else -1),(1 if y1 < y2 else -1)):
47+
field[x][y] += 1
48+
elif y1 == y2:
49+
y = y1
50+
for x in range(x1,x2+(1 if x1 < x2 else -1),(1 if x1 < x2 else -1)):
51+
field[x][y] += 1
52+
elif x1 < x2 and y1 < y2:
53+
for n in range(abs(x2-x1)+1):
54+
field[x1+n][y1+n] += 1
55+
elif x1 < x2 and y1 > y2:
56+
for n in range(abs(x2-x1)+1):
57+
field[x1+n][y1-n] += 1
58+
elif x1 > x2 and y1 < y2:
59+
for n in range(abs(x2-x1)+1):
60+
field[x1-n][y1+n] += 1
61+
elif x1 > x2 and y1 > y2:
62+
for n in range(abs(x2-x1)+1):
63+
field[x1-n][y1-n] += 1
64+
intersect = 0
65+
for y in range(len(field)):
66+
for x in range(len(field)):
67+
if field[x][y] > 1:
68+
intersect += 1
69+
print(intersect)
70+
71+
part1(inp)
72+
part2(inp)

0 commit comments

Comments
 (0)