diff options
Diffstat (limited to '2022/day3/day3.py')
| -rwxr-xr-x | 2022/day3/day3.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/2022/day3/day3.py b/2022/day3/day3.py new file mode 100755 index 0000000..2e08c21 --- /dev/null +++ b/2022/day3/day3.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 + +# filename = "input_test.txt" +filename = "input.txt" +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 + +filename = "input_test.txt" +# filename = "input.txt" +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) + |
