1
0

day 2 part 1

This commit is contained in:
2024-12-02 19:01:49 +01:00
parent 8884e46983
commit 1bcd38be17
2 changed files with 38 additions and 5 deletions

View File

@@ -1,17 +1,49 @@
use std::fs::read_to_string;
use itertools::Itertools;
pub fn day_main() {
let input = read_to_string("input/day01.txt").unwrap();
let input = read_to_string("input/day02.txt").unwrap();
println!("part1: {}", part1(&input));
// println!("part2: {}", part2(&input));
}
type Int = i32;
fn part1(input: &str) -> usize {
input
.lines()
.map(|line| {
line.split_whitespace()
.map(|s| s.parse::<i32>().unwrap())
.collect_vec()
})
.filter(|report: &Vec<i32>| {
let pairs = report
.windows(2)
.map(|window| window[1] - window[0])
.collect_vec();
let steepest_slope = pairs.iter().map(|x| x.abs()).max().unwrap();
let lowest_slope = pairs.iter().map(|x| x.abs()).min().unwrap();
let same_sign = pairs.iter().map(|x| x.signum()).unique().count() == 1;
println!("{:?} - {steepest_slope}/{same_sign}", report);
fn part1(input: &str) -> Int {
0
steepest_slope <= 3 && lowest_slope >= 1 && same_sign
})
.count()
}
// fn part2(input: &str) -> Int {
// 0
// }
#[cfg(test)]
mod test {
#[test]
fn part1() {
let input = r"7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9";
assert_eq!(2, super::part1(input));
}
}