Skip to content

Commit 5f9bf08

Browse files
committed
2022 day3
1 parent 9298892 commit 5f9bf08

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

2022/3.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python3
2+
import re
3+
import sys
4+
5+
def score_item(item):
6+
if ord(item) >= ord('a') and ord(item) <= ord('z'):
7+
return ord(item) - ord('a') + 1
8+
return ord(item) - ord('A') + 27
9+
10+
def part1(lines):
11+
def compute(line):
12+
n = len(line)
13+
p1, p2 = line[:(n//2)], line[(n//2):]
14+
items = set(p1).intersection(set(p2))
15+
return score_item(list(items)[0])
16+
return sum(map(compute, lines))
17+
18+
def part2(lines):
19+
i, s = 0, 0
20+
while i < len(lines):
21+
items = set(lines[i])
22+
for j in range(2):
23+
items.intersection_update(lines[i+1+j])
24+
s += score_item(list(items)[0])
25+
i += 3
26+
return s
27+
28+
if __name__ == "__main__":
29+
day = re.match("(?:./)?([0-9]+).py", sys.argv[0]).groups()[0]
30+
filename = sys.argv[1] if len(sys.argv) > 1 else f"{day}.txt"
31+
lines = open(filename).read().strip().split('\n')
32+
print(part1(lines))
33+
print(part2(lines))

0 commit comments

Comments
 (0)