From 16fdb976d946b4a2d2ae86fb69d9919daa819862 Mon Sep 17 00:00:00 2001 From: Tobias Eidelpes Date: Sun, 1 Dec 2019 17:58:57 +0100 Subject: [PATCH] Initial commit --- day1/haskell/day1.hs | 11 +++++ day1/input | 100 ++++++++++++++++++++++++++++++++++++++++++ day1/rust/Cargo.toml | 9 ++++ day1/rust/src/main.rs | 30 +++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 day1/haskell/day1.hs create mode 100644 day1/input create mode 100644 day1/rust/Cargo.toml create mode 100644 day1/rust/src/main.rs diff --git a/day1/haskell/day1.hs b/day1/haskell/day1.hs new file mode 100644 index 0000000..ad11338 --- /dev/null +++ b/day1/haskell/day1.hs @@ -0,0 +1,11 @@ +import System.IO +import Control.Monad + +getInts :: FilePath -> IO [Integer] +getInts path = do + contents <- readFile path + let ints = (map read . lines $ contents) :: [Integer] + return ints + +main = do + putStrLn "The solution is: " diff --git a/day1/input b/day1/input new file mode 100644 index 0000000..761f488 --- /dev/null +++ b/day1/input @@ -0,0 +1,100 @@ +54755 +96495 +111504 +53923 +118158 +118082 +137413 +135315 +87248 +127646 +79201 +52399 +77966 +129568 +63880 +128973 +55491 +111226 +126447 +87017 +112469 +83975 +51280 +60239 +120524 +57122 +136517 +117378 +93629 +55125 +68990 +70336 +115119 +68264 +148122 +70075 +106770 +54976 +123852 +61813 +113373 +53924 +59660 +67111 +52825 +81568 +110842 +134870 +135529 +78689 +129451 +96041 +91627 +70863 +100098 +121908 +96623 +143752 +149936 +116283 +149488 +126158 +106499 +124927 +109574 +70711 +139078 +67212 +124251 +123803 +73569 +145668 +96045 +59748 +123238 +68005 +121412 +97236 +104800 +86786 +141680 +123807 +82310 +76593 +146092 +82637 +92339 +93821 +56247 +58328 +90159 +105700 +57317 +69011 +125544 +102372 +63797 +92127 +111207 +77596 diff --git a/day1/rust/Cargo.toml b/day1/rust/Cargo.toml new file mode 100644 index 0000000..a147ffa --- /dev/null +++ b/day1/rust/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day1" +version = "0.1.0" +authors = ["Tobias Eidelpes "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day1/rust/src/main.rs b/day1/rust/src/main.rs new file mode 100644 index 0000000..ad5fdc8 --- /dev/null +++ b/day1/rust/src/main.rs @@ -0,0 +1,30 @@ +use std::fs::File; +use std::env; +use std::io::{Error, BufRead, BufReader}; + +fn main() -> Result<(), Error> { + let args: Vec = env::args().collect(); + + let filename = &args[1]; + + let reader = BufReader::new(File::open(filename).unwrap()); + + let mut result: i64 = 0; + + for line in reader.lines() { + let n = line.unwrap().parse::().unwrap(); + result += calculate_fuel(n); + } + println!("Result: {}", result); + + Ok(()) +} + +fn calculate_fuel(mass: i64) -> i64 { + let fuel = mass / 3 - 2; + if fuel < 0 { + return 0; + } else { + return (fuel) + calculate_fuel(fuel); + } +}