From 4c9b296e5585b6194936fc3c3ff396a6409a4709 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Wed, 11 Dec 2024 13:55:40 +0100 Subject: [PATCH] add 2023/day4.1 --- 2023/04/main.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 2023/04/main.py diff --git a/2023/04/main.py b/2023/04/main.py new file mode 100644 index 0000000..e4e87fb --- /dev/null +++ b/2023/04/main.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +# -*- encoding: utf-8; py-indent-offset: 4 -*- + +# Author: Sebastian Mark +# CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/deed.de) + +# pylint: disable=missing-module-docstring,missing-function-docstring,consider-using-f-string + + +def readinput(): + cards = {} + with open("input", "r", encoding="utf-8") as file: + i = 1 + for line in file.readlines(): + line = line.split(":")[1] + winners = line.strip("\n").split("|")[0].split() + mine = line.strip("\n").split("|")[1].split() + cards[i] = {"winners": winners, "mine": mine} + i += 1 + return cards + + +def main(): + cards = readinput() + + # part 1 + win_total = 0 + for _, numbers in cards.items(): + cardwin = len(set(numbers["winners"]) & set(numbers["mine"])) + win_total += int(2 ** (cardwin - 1)) + print("Scratchpad worth %d points" % win_total) + + +if __name__ == "__main__": + main()