From ad6001e57206757b15b8033321d1a60a2f0840f9 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Sun, 6 Dec 2020 11:41:25 +0100 Subject: [PATCH] 6 --- README.md | 2 ++ solutions/6/README.md | 13 +++++++++++++ solutions/6/python/main.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 solutions/6/README.md create mode 100644 solutions/6/python/main.py diff --git a/README.md b/README.md index 80cb2b3..f4f5367 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,6 @@ Here are my solutions for the advent of code 2020 🎄🎅 Note that the exact solutions are different for some people. +The main ones are solved in python. 1-4 I also did in Go, but I quickly [did not like it](./learning/Go.md). + `/solutions/:day/*` diff --git a/solutions/6/README.md b/solutions/6/README.md new file mode 100644 index 0000000..6450ba6 --- /dev/null +++ b/solutions/6/README.md @@ -0,0 +1,13 @@ +# 6 + +Basically I own this one to the built in `set()` of python. + +The first is an union, the second an intersection. Did not know they existed + +
+ Solutions +
    +
  1. 6809
  2. +
  3. 3394
  4. +
+
diff --git a/solutions/6/python/main.py b/solutions/6/python/main.py new file mode 100644 index 0000000..7942ae7 --- /dev/null +++ b/solutions/6/python/main.py @@ -0,0 +1,28 @@ +from os.path import join, dirname +from itertools import product +from typing import List, Set, Tuple +from functools import reduce + + +data = join(dirname(__file__), '../data.txt') +with open(data) as f: + groups = f.read().strip().split('\n\n') + + at_least_one: List[int] = [] + everyone: List[int] = [] + for group in groups: + answers: Set[str] = set() + combined = None + for answer in group.split('\n'): + answer = answer.strip() + as_set = set(list(answer)) + answers = answers.union(as_set) + combined = as_set if combined == None else combined.intersection( + as_set) + at_least_one.append(len(answers)) + everyone.append(len(combined)) + # print(single) + # print(reduce(lambda a, b: a.intersection(b), single)) + + print(f'At least one person: {sum(at_least_one)}') + print(f'Everyone: {sum(everyone)}')