summaryrefslogtreecommitdiff
path: root/2022/day03.py
diff options
context:
space:
mode:
authornekineki <nekineki@nekineki.net>2022-12-11 14:47:27 +0100
committernekineki <nekineki@nekineki.net>2022-12-11 14:50:32 +0100
commitfe75c10e350743a1c078f065d69556fecf825ca5 (patch)
tree956556a564c9329346dc9d8b1535dc5e365f3d55 /2022/day03.py
parenta74d2dc54aef546664bcc8c81eb8e01a93e94391 (diff)
move files around, update paths
Diffstat (limited to '2022/day03.py')
-rwxr-xr-x2022/day03.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/2022/day03.py b/2022/day03.py
new file mode 100755
index 0000000..22a79be
--- /dev/null
+++ b/2022/day03.py
@@ -0,0 +1,70 @@
+#!/usr/bin/env python3
+
+# filename = "in/day03.ref"
+filename = "in/day03.pzl"
+res1 = 0
+with open(filename) as f:
+ for line in f:
+ line = line.strip()
+ middle = int(len(line)/2)
+ first = line[:middle]
+ last = line[middle:]
+
+ print(first)
+ print(last)
+ d = dict()
+ for c in first:
+ d[c] = True
+
+ for c in last:
+ if c in d:
+ print(c)
+ if 'a' <= c <= 'z':
+ add = ord(c) - ord('a') + 1
+ print(add)
+ res1 += add
+ elif 'A' <= c <= 'Z':
+ add = ord(c) - ord('A') + 27
+ print(add)
+ res1 += add
+ break
+
+res2 = 0
+with open(filename) as f:
+ for line in f:
+ first = line.strip()
+ second = next(f).strip()
+ third = next(f).strip()
+ print(first)
+ print(second)
+ print(third)
+
+ s1 = set()
+ for c in first:
+ s1.add(c)
+
+ s2 = set()
+ for c in second:
+ s2.add(c)
+
+ s3 = set()
+ for c in third:
+ s3.add(c)
+
+ si = s1.intersection(s2)
+ sii = si.intersection(s3)
+ print(sii)
+
+ c = list(sii)[0]
+ if 'a' <= c <= 'z':
+ add = ord(c) - ord('a') + 1
+ print(add)
+ res2 += add
+ elif 'A' <= c <= 'Z':
+ add = ord(c) - ord('A') + 27
+ print(add)
+ res2 += add
+
+print(res1)
+print(res2)
+