From d57a850d01a1c643ccc0d21203d0cbc7bb022b89 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Thu, 1 Dec 2022 14:08:24 +0100 Subject: [PATCH] lets go bois --- 2022/01/README.md | 11 +++++++++++ 2022/01/python/main.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 2022/01/README.md create mode 100644 2022/01/python/main.py diff --git a/2022/01/README.md b/2022/01/README.md new file mode 100644 index 0000000..0795852 --- /dev/null +++ b/2022/01/README.md @@ -0,0 +1,11 @@ +# 01 + +Another year, here we go again! + +
+ Solutions +
    +
  1. 68292
  2. +
  3. 203203
  4. +
+
diff --git a/2022/01/python/main.py b/2022/01/python/main.py new file mode 100644 index 0000000..b99142d --- /dev/null +++ b/2022/01/python/main.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python + +from os.path import dirname, join + +# Day 01 + +# Common + + +def read_input(filename): + data = join(dirname(__file__), '..', filename) + with open(data) as f: + return f.read().strip() + + +test = read_input('test.txt') +data = read_input('input.txt') + + +def count_calories(data: str, top_three=False) -> int: + calories = [ + sum([int(calories) for calories in elve.split('\n')]) + for elve in data.split('\n\n') + ] + calories = sorted(calories, reverse=True) + if not top_three: + return calories[0] + else: + return sum(calories[0:3]) + + +# 1 +print('1.') +print(count_calories(test)) +print(count_calories(data)) + +# 2 +print('\n2.') +print('1.') +print(count_calories(test, True)) +print(count_calories(data, True))