advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git

day-01.rs (802B)

    1 use std::fs::File;
    2 use std::io::prelude::*;
    3 use std::io::BufReader;
    4 
    5 fn main() {
    6     let input = BufReader::new(File::open("input.txt").unwrap())
    7         .lines()
    8         .map(|line| line.unwrap().parse::<i32>().unwrap())
    9         .collect::<Vec<i32>>();
   10     println!("Rust:");
   11     println!("Part 1: {}", part_1(&input));
   12     println!("Part 2: {}", part_2(&input));
   13 }
   14 
   15 fn part_1(module_masses: &Vec<i32>) -> i32 {
   16     module_masses
   17         .iter()
   18         .fold(0, |acc, module_mass| acc + module_mass / 3 - 2)
   19 }
   20 
   21 fn part_2(module_masses: &Vec<i32>) -> i32 {
   22     module_masses.iter().fold(0, |mut acc, &module_mass| {
   23         let mut remain_mass = module_mass;
   24         while remain_mass > 0 {
   25             remain_mass = remain_mass / 3 - 2;
   26             acc += remain_mass;
   27         }
   28         acc
   29     })
   30 }