advent-of-code

Perserverance, or the lack thereof

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

main.rs (864B)

    1 use std::cmp::Reverse;
    2 use std::collections::BinaryHeap;
    3 
    4 fn main() {
    5     let input = std::fs::read_to_string("input.txt")
    6         .unwrap()
    7         .trim()
    8         .split("\n\n")
    9         .map(|x| x.split('\n').map(|x| x.parse::<i32>().unwrap()).collect())
   10         .collect::<Vec<Vec<i32>>>();
   11     // 71934
   12     println!("Part 1: {}", part_1(&input));
   13     // 211447
   14     println!("Part 2: {}", part_2(&input, 3));
   15 }
   16 
   17 fn part_1(input: &Vec<Vec<i32>>) -> i32 {
   18     (0..input.len())
   19         .map(|i| input[i].iter().sum())
   20         .max()
   21         .unwrap()
   22 }
   23 
   24 fn part_2(input: &Vec<Vec<i32>>, top_count: usize) -> i32 {
   25     let mut top = BinaryHeap::new();
   26     for s in (0..input.len()).map(|i| input[i].iter().sum::<i32>()) {
   27         top.push(Reverse(s));
   28         while top.len() > top_count {
   29             top.pop();
   30         }
   31     }
   32     top.iter().map(|r| r.0).sum()
   33 }