From 6a96972bae26ff1368a029bd5f7e143188318651 Mon Sep 17 00:00:00 2001 From: Mika Suhonen Date: Sat, 3 Dec 2022 09:27:55 +0200 Subject: [PATCH] Use sets for both answers and some clean up --- 03/c.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/03/c.py b/03/c.py index 3b0cd65..05f102f 100644 --- a/03/c.py +++ b/03/c.py @@ -6,17 +6,14 @@ tot = 0 tot2 = 0 priorities = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" -commons = '' rucksacks = f.read().splitlines() -rucksacks2 = [] +f.close() for data in rucksacks: left = data[:len(data)//2] right = data[len(data)//2:] - - common = ''.join(set(left).intersection(right)) - + common = ''.join(set(left) & set(right)) tot = tot + priorities.rfind(common)+1 print("First answer: " + str(tot)) @@ -25,9 +22,8 @@ start = 0 end = len(rucksacks) step = 3 for i in range(start, end, step): - three = rucksacks[i:i+step] - common = set(three[0]) & set(three[1]) & set(three[2]) - tot2 = tot2 + priorities.rfind(''.join(common))+1 + common = ''.join(set(three[0]) & set(three[1]) & set(three[2])) + tot2 = tot2 + priorities.rfind(common)+1 print("Second answer: " + str(tot2))