Remove cargo stuff for rust and add python implementation
This commit is contained in:
parent
16fdb976d9
commit
c507a894e0
@ -1,11 +0,0 @@
|
|||||||
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: "
|
|
||||||
21
day1/python/day1.py
Normal file
21
day1/python/day1.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
def calculate_fuel(mass):
|
||||||
|
return mass // 3 - 2
|
||||||
|
|
||||||
|
def calculate_fuel2(mass):
|
||||||
|
fuel = mass // 3 - 2
|
||||||
|
if fuel < 0:
|
||||||
|
return 0
|
||||||
|
else:
|
||||||
|
return fuel + calculate_fuel2(fuel)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
with open('../input') as f:
|
||||||
|
content = f.readlines()
|
||||||
|
|
||||||
|
modules = [int(x.strip()) for x in content]
|
||||||
|
|
||||||
|
fuel1 = [calculate_fuel(x) for x in modules]
|
||||||
|
fuel2 = [calculate_fuel2(x) for x in modules]
|
||||||
|
|
||||||
|
print("Solution for Part One: ", sum(fuel1))
|
||||||
|
print("Solution for Part Two: ", sum(fuel2))
|
||||||
@ -1,9 +0,0 @@
|
|||||||
[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]
|
|
||||||
31
day1/rust/main.rs
Normal file
31
day1/rust/main.rs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
use std::fs::File;
|
||||||
|
use std::io::{BufRead, BufReader};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let reader = BufReader::new(File::open("../input").unwrap());
|
||||||
|
|
||||||
|
let mut result_part_one: i64 = 0;
|
||||||
|
let mut result_part_two: i64 = 0;
|
||||||
|
|
||||||
|
for line in reader.lines() {
|
||||||
|
let n = line.unwrap().parse::<i64>().unwrap();
|
||||||
|
result_part_one += calculate_fuel1(n);
|
||||||
|
result_part_two += calculate_fuel2(n);
|
||||||
|
}
|
||||||
|
println!("Solution for Part One: {}", result_part_one);
|
||||||
|
println!("Solution for Part Two: {}", result_part_two);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calculate_fuel1(mass: i64) -> i64 {
|
||||||
|
let fuel = mass / 3 - 2;
|
||||||
|
return fuel
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calculate_fuel2(mass: i64) -> i64 {
|
||||||
|
let fuel = mass / 3 - 2;
|
||||||
|
if fuel < 0 {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return (fuel) + calculate_fuel2(fuel);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,30 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user