aboutsummaryrefslogtreecommitdiff
path: root/2022/03
diff options
context:
space:
mode:
authorMarvin Borner2022-12-03 06:23:32 +0100
committerMarvin Borner2022-12-03 06:25:13 +0100
commit745ee0c0f856ea730ac5bdbbc48de54fd4f05ee9 (patch)
treeb9143b6218c8c5ad63766de4214365dece1ba33c /2022/03
parentfc738bf428da8381136855b8de478349ea239e13 (diff)
cleanup
Diffstat (limited to '2022/03')
-rw-r--r--2022/03/solve.py22
1 files changed, 10 insertions, 12 deletions
diff --git a/2022/03/solve.py b/2022/03/solve.py
index 68b58c2..5f633c6 100644
--- a/2022/03/solve.py
+++ b/2022/03/solve.py
@@ -1,16 +1,17 @@
data = [dat.strip() for dat in open("input").read().split("\n") if dat != '']
+def calc(a):
+ return ord(a) - (ord('a') - 1 if a.islower() else ord('A') - 27)
+
def part1():
res = 0
for line in data:
- comp1 = list(line[len(line)//2:])
- comp2 = list(line[:len(line)//2])
- inter = list(set(comp1).intersection(comp2))[0]
- if inter.islower():
- res += ord(inter) - ord('a') + 1
- elif inter.isupper():
- res += ord(inter) - ord('A') + 27
+ h = len(line)//2
+ comp1 = set(line[h:])
+ comp2 = set(line[:h])
+ inter = comp1.intersection(comp2).pop()
+ res += calc(inter)
return res
@@ -18,11 +19,8 @@ def part2():
res = 0
for i in range(0, len(data), 3):
- common = list(set(data[i]).intersection(data[i+1]).intersection(data[i+2]))[0]
- if common.islower():
- res += ord(common) - ord('a') + 1
- elif common.isupper():
- res += ord(common) - ord('A') + 27
+ common = set(data[i]).intersection(data[i+1]).intersection(data[i+2]).pop()
+ res += calc(common)
return res