From a89b15584e6a98faecacb0acf51fc9bc83c6fe57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Sun, 1 Dec 2019 09:47:10 +0100 Subject: [PATCH] day 1 --- day1/.gitignore | 2 + day1/Cargo.lock | 6 +++ day1/Cargo.toml | 9 ++++ day1/src/input.txt | 100 +++++++++++++++++++++++++++++++++++++++++++++ day1/src/main.rs | 29 +++++++++++++ 5 files changed, 146 insertions(+) create mode 100644 day1/.gitignore create mode 100644 day1/Cargo.lock create mode 100644 day1/Cargo.toml create mode 100644 day1/src/input.txt create mode 100644 day1/src/main.rs diff --git a/day1/.gitignore b/day1/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/day1/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/day1/Cargo.lock b/day1/Cargo.lock new file mode 100644 index 0000000..da01ae3 --- /dev/null +++ b/day1/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "day1" +version = "0.1.0" + diff --git a/day1/Cargo.toml b/day1/Cargo.toml new file mode 100644 index 0000000..3c9f20a --- /dev/null +++ b/day1/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day1" +version = "0.1.0" +authors = ["Stefan Bühler "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day1/src/input.txt b/day1/src/input.txt new file mode 100644 index 0000000..611833c --- /dev/null +++ b/day1/src/input.txt @@ -0,0 +1,100 @@ +77680 +106826 +120225 +122031 +100287 +70402 +145496 +73956 +148205 +52065 +149237 +116195 +84309 +105327 +134606 +109489 +104474 +69536 +141469 +72321 +75154 +142565 +57104 +111925 +100109 +75178 +115130 +75586 +148587 +116029 +113969 +66072 +90521 +116324 +137009 +92880 +110895 +131046 +83273 +99576 +70466 +93602 +63435 +103569 +56610 +58392 +95060 +59101 +121838 +93494 +52243 +146982 +142260 +107232 +117600 +59715 +80284 +128223 +123676 +81060 +99425 +50621 +101184 +112136 +131262 +53150 +113522 +117802 +120251 +102322 +111247 +117719 +88873 +133541 +92695 +125445 +149048 +146928 +83909 +109466 +94063 +62338 +124098 +64769 +104722 +106612 +53882 +108847 +92739 +88496 +89773 +57471 +140532 +87308 +137247 +62816 +118893 +101446 +149208 +68267 diff --git a/day1/src/main.rs b/day1/src/main.rs new file mode 100644 index 0000000..a061ad2 --- /dev/null +++ b/day1/src/main.rs @@ -0,0 +1,29 @@ +fn fuel_for_modules1(input: &str) -> i64 { + let mut fuel: i64 = 0; + for module in input.split_whitespace() { + let module: i64 = module.parse().unwrap(); + fuel += (module / 3) - 2; + } + fuel +} + +fn fuel_for_mass2(mass: u64) -> u64 { + if mass <= 8 { return 0; } /* wish really hard */ + let fuel = mass / 3 - 2; + fuel + fuel_for_mass2(fuel) +} + +fn fuel_for_modules2(input: &str) -> u64 { + let mut fuel: u64 = 0; + for module in input.split_whitespace() { + let module: u64 = module.parse().unwrap(); + fuel += fuel_for_mass2(module); + } + fuel +} + +fn main() { + let input = include_str!("input.txt"); + println!("Fuel 1: {}", fuel_for_modules1(input)); + println!("Fuel 2: {}", fuel_for_modules2(input)); +}