advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git
commit 93d505b108c52ec78824ff88c60c4960ed715e65
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date:   Sun,  1 Dec 2019 23:16:08 -0500

Add day 01

Diffstat:
AREADME.org | 6++++++
Aday-01/day-01.jl | 21+++++++++++++++++++++
Aday-01/day-01.rs | 29+++++++++++++++++++++++++++++
Aday-01/input.txt | 100+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 156 insertions(+), 0 deletions(-)
diff --git a/README.org b/README.org
@@ -0,0 +1,6 @@
+Each folder contains
+- =input.txt=, which has the inputs.
+- =day-xx.lang=, which are solution files in different languages that prints out the answers to standard out.
+
+To run =.rs= files, compile with =rustc= and run the resulting binary.
+To run =.jl= files, run =julia day-xx.jl=.
diff --git a/day-01/day-01.jl b/day-01/day-01.jl
@@ -0,0 +1,21 @@
+input = map(x -> parse(Int32, x), readlines(open("input.txt")));
+
+function part_1(module_masses)
+    sum(module_masses) do mass
+        div(mass, 3) - 2
+    end
+end
+
+function part_2(module_masses)
+    sum(module_masses) do mass
+        fuel_cost = 0
+        while mass > 0
+            mass = div(mass, 3) - 2
+            fuel_cost += mass
+        end
+        fuel_cost
+    end
+end
+
+println("Part 1: ", part_1(input))
+println("Part 2: ", part_2(input))
diff --git a/day-01/day-01.rs b/day-01/day-01.rs
@@ -0,0 +1,29 @@
+use std::fs::File;
+use std::io::prelude::*;
+use std::io::BufReader;
+
+fn main() {
+    let input = BufReader::new(File::open("input.txt").unwrap())
+        .lines()
+        .map(|line| line.unwrap().parse::<i32>().unwrap())
+        .collect::<Vec<i32>>();
+    println!("Part 1: {}", part_1(&input));
+    println!("Part 2: {}", part_2(&input));
+}
+
+fn part_1(module_masses: &Vec<i32>) -> i32 {
+    module_masses
+        .iter()
+        .fold(0, |acc, module_mass| acc + module_mass / 3 - 2)
+}
+
+fn part_2(module_masses: &Vec<i32>) -> i32 {
+    module_masses.iter().fold(0, |mut acc, &module_mass| {
+        let mut remain_mass = module_mass;
+        while remain_mass > 0 {
+            remain_mass = remain_mass / 3 - 2;
+            acc += remain_mass;
+        }
+        acc
+    })
+}
diff --git a/day-01/input.txt b/day-01/input.txt
@@ -0,0 +1,100 @@
+51590
+53619
+101381
+81994
+139683
+53417
+124196
+127640
+99688
+116170
+127812
+95979
+73734
+105347
+130495
+89331
+116486
+65177
+143689
+130487
+57206
+74950
+141398
+100921
+114019
+137106
+137690
+70779
+61421
+121827
+122432
+108229
+65362
+70884
+56127
+83611
+126776
+91422
+51444
+52424
+69535
+110270
+115006
+97214
+85306
+77028
+102078
+82928
+101635
+91889
+58082
+72996
+74276
+135691
+113622
+118522
+56796
+115576
+138861
+63418
+64090
+131682
+93394
+61302
+98591
+67253
+69822
+121652
+133636
+106283
+83460
+53394
+65208
+66158
+113100
+52984
+126741
+75880
+124770
+54681
+69994
+138088
+83435
+75332
+114436
+141680
+68659
+111337
+56920
+74203
+96424
+86848
+69561
+53861
+118216
+79570
+136039
+120959
+122917
+122226