diff options
Diffstat (limited to 'src/day_1.rs')
-rw-r--r-- | src/day_1.rs | 49 |
1 files changed, 28 insertions, 21 deletions
diff --git a/src/day_1.rs b/src/day_1.rs index b786999..8fcc6ce 100644 --- a/src/day_1.rs +++ b/src/day_1.rs @@ -1,29 +1,36 @@ -use anyhow::Result; +use anyhow::{Context, Result}; -pub fn solve() -> Result<()> { - let input = include_str!("../input/day_1.txt"); - println!("day 1"); - println!("part 1: {}", part_1(input)); - println!("part 2: {}", part_2(input)); +use crate::{Problem, Solution}; - Ok(()) -} +pub struct Day1; + +impl Problem for Day1 { + const DAY: u8 = 1; -pub fn part_1(input: &str) -> usize { - input - .split("\n\n") - .map(|e| e.split('\n').flat_map(|l| l.parse::<usize>()).sum()) - .max() - .unwrap() + const INPUT: &'static str = include_str!("../input/day_1.txt"); } -pub fn part_2(input: &str) -> usize { - let mut vec = input - .split("\n\n") - .map(|e| e.split('\n').flat_map(|l| l.parse::<usize>()).sum()) - .collect::<Vec<usize>>(); +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(); + vec.sort_unstable(); - vec.iter().rev().take(3).sum() + Ok(vec.iter().rev().take(3).sum()) + } } |