Optimized day 15 part 2
1. Added a solution which rotates the grid by 45 CCW. 2. Looks for rectangles with 1 overlapping and 1 non overlapping side And that's the answer! It doesn't _strictly_ follow the requirements and does not work on the sample input
This commit is contained in:
parent
f1ab5a68e4
commit
6c4faba2f5
|
@ -6,12 +6,12 @@ const INPUTS: [&[u8]; 2] = [
|
||||||
include_bytes!("./input.txt"),
|
include_bytes!("./input.txt"),
|
||||||
];
|
];
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
struct Sensor {
|
struct Sensor {
|
||||||
sx: i64,
|
left: i64,
|
||||||
sy: i64,
|
right: i64,
|
||||||
|
top: i64,
|
||||||
area: i64,
|
bottom: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse(input: &[u8]) -> Vec<Sensor> {
|
fn parse(input: &[u8]) -> Vec<Sensor> {
|
||||||
|
@ -41,55 +41,70 @@ fn parse(input: &[u8]) -> Vec<Sensor> {
|
||||||
}
|
}
|
||||||
out[i] = if is_neg { -num } else { num };
|
out[i] = if is_neg { -num } else { num };
|
||||||
|
|
||||||
|
let distance = (out[0] - out[2]).abs() + (out[1] - out[3]).abs();
|
||||||
|
|
||||||
|
let (s, r) = rotneg45cw(out[0], out[1]);
|
||||||
|
|
||||||
|
let (left, right) = (s - distance - 1, s + distance + 1);
|
||||||
|
let (top, bottom) = (r - distance - 1, r + distance + 1);
|
||||||
Sensor {
|
Sensor {
|
||||||
sx: out[0],
|
left,
|
||||||
sy: out[1],
|
right,
|
||||||
area: (out[0] - out[2]).abs() + (out[1] - out[3]).abs(),
|
top,
|
||||||
|
bottom,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn solution(input: Vec<Sensor>, line: i64) -> i64 {
|
fn solution(input: Vec<Sensor>) -> i64 {
|
||||||
for j in 0..=line {
|
let mut left = 0;
|
||||||
let mut i = 0;
|
let mut right = 0;
|
||||||
|
|
||||||
'out: while i <= line {
|
'outer: for x in input.iter() {
|
||||||
for beacon in input.iter() {
|
for y in input.iter() {
|
||||||
if inside((beacon.sx, beacon.sy), (i, j)) <= beacon.area {
|
if y.left == x.right && (x.top < y.bottom || y.top < x.bottom) {
|
||||||
let vgap = (j - beacon.sy).abs();
|
left = y.left;
|
||||||
let dx = (beacon.sx - i) + (beacon.area - vgap) + 1;
|
break 'outer;
|
||||||
i += dx;
|
}
|
||||||
|
}
|
||||||
continue 'out;
|
}
|
||||||
}
|
'outer: for x in input.iter() {
|
||||||
|
for y in input.iter() {
|
||||||
|
if y.top == x.bottom && (x.left < y.right || y.left < x.right) {
|
||||||
|
right = y.top;
|
||||||
|
break 'outer;
|
||||||
}
|
}
|
||||||
|
|
||||||
return i * 4000000 + j;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
0
|
let (fx, fy) = rotneg45ccw(left, right);
|
||||||
|
4000000 * fx + fy
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
const fn inside((sx, sy): (i64, i64), (px, py): (i64, i64)) -> i64 {
|
const fn rotneg45ccw(x: i64, y: i64) -> (i64, i64) {
|
||||||
(sx - px).abs() + (sy - py).abs()
|
let a = (x + y) / 2;
|
||||||
|
(y - a, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
const fn rotneg45cw(x: i64, y: i64) -> (i64, i64) {
|
||||||
|
(-x + y, x + y)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let output = parse(INPUTS[0]);
|
for input in INPUTS.iter() {
|
||||||
let score = solution(output, 20);
|
let output = parse(input);
|
||||||
println!("{}", score);
|
let score = solution(output);
|
||||||
let output = parse(INPUTS[1]);
|
println!("{}", score);
|
||||||
let score = solution(output, 4000000);
|
}
|
||||||
println!("{}", score);
|
|
||||||
}
|
}
|
||||||
#[bench]
|
#[bench]
|
||||||
fn solution_bench(b: &mut test::Bencher) {
|
fn solution_bench(b: &mut test::Bencher) {
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
let input = parse(INPUTS[1]);
|
let input = parse(INPUTS[1]);
|
||||||
let result = solution(input, 4000000);
|
let result = solution(input);
|
||||||
test::black_box(result);
|
test::black_box(result);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user