Initial commit

This commit is contained in:
Tobias Eidelpes 2019-12-01 17:58:57 +01:00
parent 57e9a6e7d7
commit 16fdb976d9
4 changed files with 150 additions and 0 deletions

11
day1/haskell/day1.hs Normal file
View File

@ -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: "

100
day1/input Normal file
View File

@ -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

9
day1/rust/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "day1"
version = "0.1.0"
authors = ["Tobias Eidelpes <tobias@eidelpes.info>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

30
day1/rust/src/main.rs Normal file
View File

@ -0,0 +1,30 @@
use std::fs::File;
use std::env;
use std::io::{Error, BufRead, BufReader};
fn main() -> Result<(), Error> {
let args: Vec<String> = 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::<i64>().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);
}
}