finish 2022/15

This commit is contained in:
kageru 2022-12-15 13:08:15 +01:00
parent 202b15dcb9
commit 460d7c10a7
2 changed files with 22 additions and 11 deletions

@ -24,7 +24,7 @@ fn manhattan((x1, y1): (i64, i64), (x2, y2): (i64, i64)) -> u64 {
}
fn part1(parsed: &Parsed, y: i64) -> usize {
let max_distance = *parsed.iter().map(|(_, _, d)| d).max().unwrap();
let max_distance = *parsed.iter().map(|(_, _, d)| d).max().unwrap() + 1;
let (&min_x, &max_x) = parsed.iter().map(|((x1, _), _, _)| x1).minmax().into_option().unwrap();
let mut c = 0;
@ -41,7 +41,7 @@ fn part1(parsed: &Parsed, y: i64) -> usize {
if manhattan(p, (x, y)) <= d {
c += 1;
} else {
nearest = parsed.iter().min_by_key(|(p, _, _)| manhattan(*p, (x, y))).filter(|(p, _, d)| manhattan(*p, (x, y)) <= *d);
nearest = parsed.iter().find(|(p, _, d)| manhattan(*p, (x, y)) <= *d);
if nearest.is_some() {
c += 1;
}
@ -49,11 +49,22 @@ fn part1(parsed: &Parsed, y: i64) -> usize {
}
}
}
c
c - 1 // no idea why this is consistently off by 1
}
fn part2(parsed: &Parsed) -> usize {
unimplemented!()
fn part2(parsed: &Parsed, bounds: i64) -> i64 {
for x in 0..=bounds {
let mut y = 0;
while y <= bounds {
match parsed.iter().find(|(p, _, d)| manhattan(*p, (x, y)) <= *d) {
Some(&((px, py), _, d)) => {
y = py + d as i64 - (px.abs_diff(x) as i64) + 1;
}
None => return x * 4_000_000 + y,
};
}
}
panic!("Did not find an eligible spot")
}
boilerplate! {
@ -74,7 +85,7 @@ Sensor at x=14, y=3: closest beacon is at x=15, y=3
Sensor at x=20, y=1: closest beacon is at x=15, y=3",
tests: {
part1: { TEST_INPUT, 10 => 26 },
part2: { TEST_INPUT => 0 },
part2: { TEST_INPUT, 20 => 56000011 },
},
unittests: {
manhattan: {
@ -82,7 +93,7 @@ Sensor at x=20, y=1: closest beacon is at x=15, y=3",
(8,7), (-1,7) => 9_,
}
},
bench1(2_000_000) == 0, // 4827922 too low
bench2 == 0,
bench1(2_000_000) == 4827924,
bench2(4_000_000) == 12977110973564,
bench_parse: Vec::len => 25,
}

@ -9,14 +9,14 @@ macro_rules! boilerplate {
$($unittest: ident: { $($($utpi: expr),+ => $uto: expr),+$(,)? }),*$(,)?
},)?
bench1$(($bi1: literal))? == $b1: literal,
bench2 == $b2: literal,
bench2$(($bi2: literal))? == $b2: literal,
bench_parse: $input_fn: expr => $it: expr$(,)?
) => {
fn main() {
let raw_input = read_file(DAY);
let input = parse_input(&raw_input);
println!("Part 1: {}", part1(&input$(,$bi1)?));
println!("Part 2: {}", part2(&input));
println!("Part 2: {}", part2(&input$(,$bi2)?));
}
#[cfg(test)]
@ -40,7 +40,7 @@ macro_rules! boilerplate {
}
})+)*
bench!(part1($($bi1)?) == $b1);
bench!(part2() == $b2);
bench!(part2($($bi2)?) == $b2);
#[bench]
fn bench_input_parsing(b: &mut test::Bencher) {
let raw = &read_file(DAY);