Compare commits
2 Commits
b6b7a43fba
...
0b2dc35cad
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0b2dc35cad | ||
|
|
5921b577c0 |
@ -1,5 +1,5 @@
|
||||
pub mod d1;
|
||||
// pub mod d2;
|
||||
pub mod d2;
|
||||
// pub mod d3;
|
||||
// pub mod d4;
|
||||
// pub mod d5;
|
||||
|
||||
70
src/days/d2.rs
Normal file
70
src/days/d2.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
#![feature(let_chains)]
|
||||
#![feature(array_windows)]
|
||||
#![feature(decl_macro)]
|
||||
#![feature(iter_array_chunks)]
|
||||
|
||||
|
||||
@ -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,
|
||||
};
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user