From 5c997bb15894f1221c138e7710c95f0c56c7c0d6 Mon Sep 17 00:00:00 2001 From: Sijmen Date: Sat, 3 Dec 2022 19:22:35 +0100 Subject: [PATCH] day 3 --- .gitignore | 1 + day03.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 day03.py diff --git a/.gitignore b/.gitignore index 14140f4..be85439 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.in +*.ex diff --git a/day03.py b/day03.py new file mode 100644 index 0000000..d821be8 --- /dev/null +++ b/day03.py @@ -0,0 +1,28 @@ +import fileinput + +input = [s.strip() for s in fileinput.input()] +part1, part2 = 0, 0 + + +def prio(x): + if x.islower(): + return ord(x) - ord("a") + 1 + else: + return ord(x) - ord("A") + 27 + + +for line in input: + first = set(line[: len(line) // 2]) + second = set(line[len(line) // 2 :]) + both = (first & second).pop() + part1 += prio(both) + + +for i in range(0, len(input), 3): + x = set(input[i]) + y = set(input[i + 1]) + z = set(input[i + 2]) + overlap = (x & y & z).pop() + part2 += prio(overlap) + +print(part1, part2)