From 87a8ee10224e306cf582a1307f86df99d47b60e6 Mon Sep 17 00:00:00 2001 From: kleph Date: Sat, 3 Dec 2022 10:06:56 +0100 Subject: [PATCH] 2022 - Day 3 part 2 --- 2022/3/3_2.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2022/3/3_2.py diff --git a/2022/3/3_2.py b/2022/3/3_2.py new file mode 100644 index 0000000..6e36a57 --- /dev/null +++ b/2022/3/3_2.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +# 2022 - Advent Of Code 3 - part2 + +# file = 'input_example.txt' +file = 'input.txt' + +prio = 0 + + +def find_common(b1, b2, b3): + common1 = set(b1).intersection(b2) + common2 = set(b1).intersection(b3) + common_all = common1.intersection(common2) + print(f"c1: {common1} c2: {common2} -> {common_all}") + return list(common_all) + + +def convert_score(lnum): + num = ord(lnum[0]) + if num > 90: # lower case + return num - 96 + return num - 64 + 26 + + +with open(file, encoding="utf-8") as f: + while line := f.readline(): + bag1 = line + bag2 = f.readline().strip() + bag3 = f.readline().strip() + prio += convert_score(find_common(bag1, bag2, bag3)) + +print(prio)