summaryrefslogtreecommitdiffstats
path: root/src/day_1.rs
blob: 8fcc6ce8cea916abc45f64c68b1baf25e0b7db9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use anyhow::{Context, Result};

use crate::{Problem, Solution};

pub struct Day1;

impl Problem for Day1 {
    const DAY: u8 = 1;

    const INPUT: &'static str = include_str!("../input/day_1.txt");
}

impl Solution for Day1 {
    type Answer1 = usize;

    type Answer2 = usize;

    fn part_1(input: &str) -> Result<Self::Answer1, anyhow::Error> {
        input
            .split("\n\n")
            .map(|e| e.split('\n').flat_map(|l| l.parse::<usize>()).sum())
            .max()
            .context("Failed to find max")
    }

    fn part_2(input: &str) -> Result<Self::Answer2, anyhow::Error> {
        let mut vec = input
            .split("\n\n")
            .map(|e| e.split('\n').flat_map(|l| l.parse::<usize>()).sum())
            .collect::<Vec<usize>>();

        vec.sort_unstable();

        Ok(vec.iter().rev().take(3).sum())
    }
}