Skip to content

Commit fd6dd84

Browse files
committed
Solve 2022 Day 3 in python
1 parent 6007e94 commit fd6dd84

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

2022/Day3/python/solve.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
import string
3+
4+
5+
def part1(lines):
6+
prio_sum = 0
7+
for line in lines:
8+
items = [c for c in line if c != '\n']
9+
rucksack = {
10+
'left': items[0:int(len(line) / 2)],
11+
'right': items[int(len(line)/2):len(line)]
12+
}
13+
14+
duplicate = None
15+
16+
for il in rucksack['left']:
17+
if il in rucksack['right'] and not duplicate:
18+
duplicate = il
19+
prio_sum += string.ascii_letters.index(duplicate) + 1
20+
return prio_sum
21+
22+
23+
def part2(lines):
24+
prio_sum = 0
25+
for i in range(0, len(lines), 3):
26+
groups = [g.replace('\n', '') for g in lines[i: i + 3]]
27+
28+
badge = set(groups[0]).intersection(groups[1], groups[2]).pop()
29+
30+
prio_sum += string.ascii_letters.index(badge) + 1
31+
32+
return prio_sum
33+
34+
35+
36+
dirname = os.path.dirname(__file__)
37+
filename = os.path.join(dirname, '../input.txt')
38+
with open(filename) as f:
39+
lines = f.readlines()
40+
41+
print(part1(lines))
42+
print(part2(lines))

0 commit comments

Comments
 (0)