From 2ac18fcb7ed821d790d5dfe3e71c3626f5c04461 Mon Sep 17 00:00:00 2001 From: Steph Date: Thu, 1 Dec 2022 21:02:00 +0100 Subject: [PATCH] Day 7 done in python --- 2021/7/.gitignore | 1 + 2021/7/crabs.py | 12 ++++++++++++ 2021/7/crabs2.py | 13 +++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 2021/7/.gitignore create mode 100644 2021/7/crabs.py create mode 100644 2021/7/crabs2.py diff --git a/2021/7/.gitignore b/2021/7/.gitignore new file mode 100644 index 0000000..3f9177e --- /dev/null +++ b/2021/7/.gitignore @@ -0,0 +1 @@ +input diff --git a/2021/7/crabs.py b/2021/7/crabs.py new file mode 100644 index 0000000..5055f6f --- /dev/null +++ b/2021/7/crabs.py @@ -0,0 +1,12 @@ +with open("input", "r") as input: + input = list(map(int, input.read().strip().split(","))) + fuel = [] + + for i in range(min(input), max(input)): + total_fuel = 0 + for x in input: + total_fuel += abs(i - x) + fuel.append(total_fuel) + + fuel.sort() + print(fuel[0]) diff --git a/2021/7/crabs2.py b/2021/7/crabs2.py new file mode 100644 index 0000000..105cf2a --- /dev/null +++ b/2021/7/crabs2.py @@ -0,0 +1,13 @@ +with open("input", "r") as input: + input = list(map(int, input.read().strip().split(","))) + fuel = [] + + for i in range(min(input), max(input)): + total_fuel = 0 + for x in input: + for y in range(1, abs(i - x) + 1): + total_fuel += y + fuel.append(total_fuel) + + fuel.sort() + print(fuel[0])