Day 17 part 1

This commit is contained in:
Bill Thiede 2021-12-17 19:40:54 -08:00
parent 232b2687ca
commit 2848a9ae1f
3 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1 @@
target area: x=88..125, y=-157..-103

76
2021/src/day17.rs Normal file
View File

@ -0,0 +1,76 @@
use advent::prelude::*;
use aoc_runner_derive::{aoc, aoc_generator};
#[derive(Debug)]
struct Target {
x_min: isize,
x_max: isize,
y_min: isize,
y_max: isize,
}
impl FromStr for Target {
type Err = Infallible;
fn from_str(input: &str) -> std::result::Result<Target, Infallible> {
let parts: Vec<_> = input.split(' ').collect();
let x = &parts[2][2..].strip_suffix(',').unwrap();
let y = &parts[3][2..];
let (x_min, x_max) = x
.split_once("..")
.and_then(|(min, max)| Some((min.parse().unwrap(), max.parse().unwrap())))
.unwrap();
let (y_min, y_max) = y
.split_once("..")
.and_then(|(min, max)| Some((min.parse().unwrap(), max.parse().unwrap())))
.unwrap();
Ok(Target {
x_min,
x_max,
y_min,
y_max,
})
}
}
#[aoc(day17, part1)]
fn part1(input: &str) -> Result<usize> {
let tgt: Target = input.parse()?;
dbg!(&tgt);
Ok((0..(tgt.y_min).abs() as usize).sum())
}
/*
#[aoc(day17, part2)]
fn part2(input: &str) -> Result<usize> {
todo!("part2");
Ok(0)
}
*/
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part1() -> Result<()> {
let input = r#"
target area: x=20..30, y=-10..-5
"#
.trim();
assert_eq!(part1(input)?, 45);
Ok(())
}
/*
#[test]
fn test_part2()->Result<()> {
let input = r#"
"#
.trim();
assert_eq!(part2(input)?, usize::MAX);
Ok(())
}
*/
}

View File

@ -6,6 +6,7 @@ pub mod day13;
//pub mod day14; //pub mod day14;
pub mod day15; pub mod day15;
pub mod day16; pub mod day16;
pub mod day17;
pub mod day2; pub mod day2;
pub mod day3; pub mod day3;
pub mod day4; pub mod day4;