day1: clean up, use generator
This commit is contained in:
1 file changed
+16
-9
+16
-9
@@ -1,10 +1,17 @@
|
|||||||
use aoc_runner_derive::aoc;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use aoc_runner_derive::{aoc, aoc_generator};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
#[aoc_generator(day1)]
|
||||||
|
fn parse(input: &str) -> HashMap<u64, usize> {
|
||||||
|
input.lines().map(|l| l.parse::<u64>().unwrap()).counts()
|
||||||
|
}
|
||||||
|
|
||||||
#[aoc(day1, part1)]
|
#[aoc(day1, part1)]
|
||||||
pub fn part1(input: &str) -> u64 {
|
pub fn part1(input: &HashMap<u64, usize>) -> u64 {
|
||||||
let ids = input.lines().map(|l| l.parse::<u64>().unwrap()).counts();
|
input
|
||||||
ids.iter()
|
.iter()
|
||||||
.sorted_by_key(|(_k, v)| *v)
|
.sorted_by_key(|(_k, v)| *v)
|
||||||
.rev()
|
.rev()
|
||||||
.take(3)
|
.take(3)
|
||||||
@@ -12,9 +19,9 @@ pub fn part1(input: &str) -> u64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[aoc(day1, part2)]
|
#[aoc(day1, part2)]
|
||||||
pub fn part2(input: &str) -> u64 {
|
pub fn part2(input: &HashMap<u64, usize>) -> u64 {
|
||||||
let ids = input.lines().map(|l| l.parse::<u64>().unwrap()).counts();
|
input
|
||||||
ids.iter()
|
.iter()
|
||||||
.sorted_by_key(|(_k, v)| *v)
|
.sorted_by_key(|(_k, v)| *v)
|
||||||
.take(3)
|
.take(3)
|
||||||
.fold(1, |acc, (k, _v)| acc * k)
|
.fold(1, |acc, (k, _v)| acc * k)
|
||||||
@@ -48,11 +55,11 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn problem1_example() {
|
fn problem1_example() {
|
||||||
assert_eq!(part1(EXAMPLE), 186276);
|
assert_eq!(part1(&parse(EXAMPLE)), 186276);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn problem2_example() {
|
fn problem2_example() {
|
||||||
assert_eq!(part2(EXAMPLE), 193050);
|
assert_eq!(part2(&parse(EXAMPLE)), 193050);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user