Compare commits

...

2 Commits

Author SHA1 Message Date
YK
b6b7a43fba add array_windows feature 2024-12-02 08:44:56 +03:00
YK
bddc0e3ddd add BinaryHeap re-export 2024-12-02 08:44:42 +03:00
3 changed files with 77 additions and 1 deletions

70
src/days/d2.rs Normal file
View File

@ -0,0 +1,70 @@
use crate::prelude::*;
pub type I = Vec<Vec<i32>>;
pub type O = usize;
fn _parse (data: &str) -> I {
data.lines().map(|line| line.split_ascii_whitespace().map(|e| p!(e)).collect()).collect()
}
fn _report_safety (report: &[i32]) -> bool {
let dir = report[0] - report[1];
if dir.abs() < 1 || dir.abs() > 3 { return false; }
for &[a, b] in (&report[1..]).array_windows() {
let diff = a - b;
let abs = diff.abs();
if diff.signum() != dir.signum() { return false; }
if abs < 1 || abs > 3 { return false }
}
true
}
fn _silver (data: &I) -> O {
data.into_iter().filter(|s| _report_safety(s)).count()
}
fn _gold (data: &I) -> O {
data.into_iter().filter(|report| {
let mut s = vec![report.to_vec()];
for i in 0..report.len() {
s.push([&report[..i], &report[i+1..]].concat());
}
s.iter().any(|report| _report_safety(report))
}).count()
}
#[cfg(test)]
mod test {
use super::*;
fn read () -> I {
let data = inc!(2);
_parse(data)
}
#[test]
fn sample () {
let data = inc!("2_example");
let data = _parse(data);
assert_eq!(_gold(&data), 4)
}
#[test]
fn silver () {
let data = read();
let ans = _silver(&data);
assert_eq!(ans, 510)
}
#[test]
fn gold () {
let data = read();
let ans = _gold(&data);
assert_eq!(ans, 553)
}
}

View File

@ -1,4 +1,5 @@
#![feature(let_chains)]
#![feature(array_windows)]
#![feature(decl_macro)]
#![feature(iter_array_chunks)]

View File

@ -2,7 +2,12 @@ pub use crate::utils::*;
pub use anyhow::Error;
pub use itertools::Itertools;
pub use std::{
collections::{ HashMap as M, HashSet as S, VecDeque as D },
collections::{
HashMap as M,
HashSet as S,
VecDeque as D,
BinaryHeap as B,
},
cmp::Ordering as Or,
};