diff --git a/2022/day3.py b/2022/day3.py new file mode 100644 index 0000000..da6bc3f --- /dev/null +++ b/2022/day3.py @@ -0,0 +1,29 @@ +with open('day3-input', 'r') as file: + input = file.read().strip().split('\n') + +def prio(character): + if 'a' <= character <= 'z': + return ord(character) - ord('a') + 1 + return ord(character) - ord('A') + 27 + + +s = 0 +for line in input: + l = len(line)//2 + left = set(line[:l]) + right = set(line[l:]) + common = left & right + character = list(common)[0] + s += prio(character) +print(s) # Part 1 answer + + +s = 0 +for group in range(len(input)//3): + e1 = set(input[group*3]) + e2 = set(input[group*3+1]) + e3 = set(input[group*3+2]) + common = e1 & e2 & e3 + character = list(common)[0] + s += prio(character) +print(s) # Part 2 answer \ No newline at end of file