From 7dd992b93a375c86b31cef0097405b93477b47ac Mon Sep 17 00:00:00 2001 From: Johannes Date: Sun, 1 Dec 2019 09:23:56 +0100 Subject: [PATCH] day 1 full --- .gitignore | 2 + Cargo.lock | 6 +++ Cargo.toml | 9 ++++ input/day01.txt | 100 +++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 5 +++ src/tasks/day01.rs | 36 ++++++++++++++++ src/tasks/mod.rs | 1 + 7 files changed, 159 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 input/day01.txt create mode 100644 src/main.rs create mode 100644 src/tasks/day01.rs create mode 100644 src/tasks/mod.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..fec8700 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "aoc_2019" +version = "0.1.0" + diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..07bd15b --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "aoc_2019" +version = "0.1.0" +authors = ["Johannes "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/input/day01.txt b/input/day01.txt new file mode 100644 index 0000000..f2ddfa9 --- /dev/null +++ b/input/day01.txt @@ -0,0 +1,100 @@ +70102 +60688 +105331 +127196 +141253 +118164 +67481 +75634 +60715 +84116 +51389 +52440 +84992 +87519 +85765 +124479 +97873 +85437 +94902 +124969 +70561 +144601 +128042 +67596 +136905 +111849 +100389 +135608 +91006 +77385 +52100 +64728 +127796 +114893 +82414 +66565 +73704 +110396 +142722 +107813 +149628 +131729 +118421 +56566 +84962 +108120 +108438 +81536 +55238 +77072 +132575 +82716 +50641 +57320 +89661 +97094 +134713 +142451 +128541 +53527 +116088 +101909 +124349 +103812 +108324 +72981 +114488 +78738 +78523 +129146 +103007 +68506 +102227 +93507 +94557 +105867 +125514 +109130 +146102 +100876 +143549 +85753 +97589 +90892 +104287 +70930 +53847 +94687 +135370 +76024 +76156 +101006 +128349 +58134 +110849 +149176 +136728 +79054 +136740 +131081 \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..a100fe5 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,5 @@ +mod tasks; + +fn main() { + tasks::day01::run(); +} diff --git a/src/tasks/day01.rs b/src/tasks/day01.rs new file mode 100644 index 0000000..e79781a --- /dev/null +++ b/src/tasks/day01.rs @@ -0,0 +1,36 @@ +use std::fs; + +pub fn run() { + let content = fs::read_to_string("input/day01.txt").unwrap(); + task1(&content); + task2(&content); +} + +fn task1(content: &String) { + let fuel: i32 = content + .lines() + .map(|line| line.parse::().unwrap()) + .map(|mass| mass / 3 - 2) + .sum(); + + println!("Task 1: {}", fuel); +} + +fn task2(content: &String) { + fn while_fun(mass: i32) -> i32 { + let to_add = mass / 3 - 2; + if to_add < 1 { + 0 + } else { + to_add + while_fun(to_add) + } + }; + + let fuel: i32 = content + .lines() + .map(|line| line.parse::().unwrap()) + .map(while_fun) + .sum(); + + println!("Task 2: {}", fuel); +} diff --git a/src/tasks/mod.rs b/src/tasks/mod.rs new file mode 100644 index 0000000..12b8f18 --- /dev/null +++ b/src/tasks/mod.rs @@ -0,0 +1 @@ +pub mod day01;