solution: day3
This commit is contained in:
62
src/bin/03.rs
Normal file
62
src/bin/03.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use itertools::Itertools;
|
||||
|
||||
pub fn part_one(input: &str) -> Option<usize> {
|
||||
Some(
|
||||
input
|
||||
.lines()
|
||||
.map(|x| {
|
||||
x.split_whitespace()
|
||||
.map(|x| x.parse::<usize>().unwrap())
|
||||
.collect_vec()
|
||||
})
|
||||
.map(|x| [x[0], x[1], x[2]])
|
||||
.filter(|&[a, b, c]| a + b > c && a + c > b && c + b > a)
|
||||
.count(),
|
||||
)
|
||||
}
|
||||
pub fn part_two(input: &str) -> Option<usize> {
|
||||
let mut triangles = Vec::new();
|
||||
|
||||
for chunk in input
|
||||
.lines()
|
||||
.map(|x| {
|
||||
x.split_whitespace()
|
||||
.map(|x| x.parse::<usize>().unwrap())
|
||||
.collect_vec()
|
||||
})
|
||||
.chunks(3)
|
||||
.into_iter()
|
||||
{
|
||||
let (l1, l2, l3) = chunk.collect_tuple().unwrap();
|
||||
|
||||
for i in 0..=2 {
|
||||
triangles.push([l1[i], l2[i], l3[i]]);
|
||||
}
|
||||
}
|
||||
|
||||
Some(
|
||||
triangles
|
||||
.into_iter()
|
||||
.filter(|&[a, b, c]| a + b > c && a + c > b && c + b > a)
|
||||
.count(),
|
||||
)
|
||||
}
|
||||
fn main() {
|
||||
let input = &aoc::read_file("inputs", 3);
|
||||
aoc::solve!(1, part_one, input);
|
||||
aoc::solve!(2, part_two, input);
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_part_one() {
|
||||
let input = aoc::read_file("examples", 3);
|
||||
assert_eq!(part_one(&input), Some(2));
|
||||
}
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
let input = aoc::read_file("examples", 3);
|
||||
assert_eq!(part_two(&input), Some(1));
|
||||
}
|
||||
}
|
||||
3
src/examples/03.txt
Normal file
3
src/examples/03.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
5 10 25
|
||||
2 2 3
|
||||
6 13 15
|
||||
Reference in New Issue
Block a user