From 1d1cacb755e0c7f5268f8c0c247e743f56f5e97c Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Wed, 1 Dec 2021 14:46:26 +0100 Subject: [PATCH] 2021/01 --- 2021/01/main.py | 6 +++++ 2021/01/python/main.py | 52 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 2021/01/main.py create mode 100644 2021/01/python/main.py diff --git a/2021/01/main.py b/2021/01/main.py new file mode 100644 index 0000000..fe5c0e0 --- /dev/null +++ b/2021/01/main.py @@ -0,0 +1,6 @@ + + +# 1 +with open('./input.txt', 'r') as f: + for line in f.readlines(): + print(line.strip()) diff --git a/2021/01/python/main.py b/2021/01/python/main.py new file mode 100644 index 0000000..c78ded5 --- /dev/null +++ b/2021/01/python/main.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python + +from os.path import join, dirname +from typing import List + +# Day 01 + +# Common + + +def read_input(filename) -> str: + data = join(dirname(__file__), '..', filename) + with open(data) as f: + return f.read() + + +def parse(raw): + return [int(x) for x in raw.strip().split('\n')] + + +test = parse(read_input('test.txt')) +test2 = parse(read_input('test_2.txt')) +data = parse(read_input('input.txt')) + +# 1 + + +def count_increasing(data: List[int]) -> int: + total = 0 + for x in range(1, len(data)): + if data[x] > data[x - 1]: + total += 1 + return total + + +print('1. ') +print(f"Test: {count_increasing(test)}") +print(f"Result: {count_increasing(data)}") + +# 2 + + +def data_to_windows(data: List, size: int) -> List: + windows = [] + for x in range(0, len(data) - size + 1): + windows.append(sum(data[x:x + size])) + return windows + + +print('2.') +print(f"Test: {count_increasing(data_to_windows(test2, 3))}") +print(f"Result: {count_increasing(data_to_windows(data, 3))}")