mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Parameterize implementation to support test
This commit is contained in:
@@ -10,10 +10,6 @@ use crate::common::parse_input;
|
|||||||
|
|
||||||
const EPSILON: f64 = 1e-6;
|
const EPSILON: f64 = 1e-6;
|
||||||
|
|
||||||
fn is_in_range(n: f64) -> bool {
|
|
||||||
n >= 200000000000000.0 && n <= 400000000000000.0
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Hail {
|
struct Hail {
|
||||||
position: [i64; 3],
|
position: [i64; 3],
|
||||||
speed: [i64; 3],
|
speed: [i64; 3],
|
||||||
@@ -30,7 +26,7 @@ impl Hail {
|
|||||||
(slope, offset)
|
(slope, offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn intersect(&self, other: &Self) -> bool {
|
fn intersect(&self, other: &Self, min: f64, max: f64) -> bool {
|
||||||
let (a1, b1) = self.to_yab();
|
let (a1, b1) = self.to_yab();
|
||||||
let (a2, b2) = other.to_yab();
|
let (a2, b2) = other.to_yab();
|
||||||
|
|
||||||
@@ -52,7 +48,7 @@ impl Hail {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// use the formula for X
|
// use the formula for X
|
||||||
is_in_range(x) && is_in_range(y)
|
x >= min && x <= max && y >= min && y <= max
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse(i: &[u8]) -> IResult<&[u8], Self> {
|
fn parse(i: &[u8]) -> IResult<&[u8], Self> {
|
||||||
@@ -78,7 +74,7 @@ fn parse_hail(i: &[u8]) -> IResult<&[u8], Vec<Hail>> {
|
|||||||
many1(Hail::parse)(i)
|
many1(Hail::parse)(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part1(input: &[u8]) -> anyhow::Result<String> {
|
fn part1_parametrized(input: &[u8], min: f64, max: f64) -> anyhow::Result<String> {
|
||||||
let hail = parse_input(input, parse_hail)?;
|
let hail = parse_input(input, parse_hail)?;
|
||||||
|
|
||||||
let intersections = hail
|
let intersections = hail
|
||||||
@@ -88,13 +84,17 @@ pub fn part1(input: &[u8]) -> anyhow::Result<String> {
|
|||||||
hail[i + 1..]
|
hail[i + 1..]
|
||||||
.iter()
|
.iter()
|
||||||
.map(move |b| (a, b))
|
.map(move |b| (a, b))
|
||||||
.filter(|(a, b)| a.intersect(b))
|
.filter(|(a, b)| a.intersect(b, min, max))
|
||||||
})
|
})
|
||||||
.count();
|
.count();
|
||||||
|
|
||||||
Ok(intersections.to_string())
|
Ok(intersections.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn part1(input: &[u8]) -> anyhow::Result<String> {
|
||||||
|
part1_parametrized(input, 200000000000000.0, 400000000000000.0)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn part2(_input: &[u8]) -> anyhow::Result<String> {
|
pub fn part2(_input: &[u8]) -> anyhow::Result<String> {
|
||||||
anyhow::bail!("Not implemented")
|
anyhow::bail!("Not implemented")
|
||||||
}
|
}
|
||||||
@@ -105,8 +105,8 @@ mod tests {
|
|||||||
|
|
||||||
const SAMPLE: &[u8] = include_bytes!("samples/24.txt");
|
const SAMPLE: &[u8] = include_bytes!("samples/24.txt");
|
||||||
|
|
||||||
// #[test]
|
#[test]
|
||||||
// fn sample_part1() {
|
fn sample_part1() {
|
||||||
// assert_eq!("2", part1(SAMPLE).unwrap());
|
assert_eq!("2", part1_parametrized(SAMPLE, 7.0, 27.0).unwrap());
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user