Day 9 - Cleanup
This commit is contained in:
128
src/day9/main.rs
128
src/day9/main.rs
@@ -1,9 +1,5 @@
|
||||
use anyhow::{Result, bail};
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
fs,
|
||||
ops::Range,
|
||||
};
|
||||
use anyhow::Result;
|
||||
use std::{fs, ops::Range};
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
struct TilePos {
|
||||
@@ -58,28 +54,13 @@ impl<'a> Pair<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
// This will only work if the pairs are inline
|
||||
fn colide(&'a self, other: &'a Pair) -> Result<bool> {
|
||||
if self.b1.x == self.b2.x {
|
||||
if other.b1.x == other.b2.x {
|
||||
return Ok(false);
|
||||
} else if other.b1.y == other.b2.y {
|
||||
return Ok(self.get_y_range().contains(&other.b1.y)
|
||||
&& other.get_x_range().contains(&self.b1.x));
|
||||
fn colide(&'a self, other: &'a Pair) -> bool {
|
||||
if self.b1.x == self.b2.x && other.b1.y == other.b2.y {
|
||||
self.get_y_range().contains(&other.b1.y) && other.get_x_range().contains(&self.b1.x)
|
||||
} else if self.b1.y == self.b2.y && other.b1.x == other.b2.x {
|
||||
self.get_x_range().contains(&other.b1.x) && other.get_y_range().contains(&self.b1.y)
|
||||
} else {
|
||||
bail!("Did not follow instructions 1 {:?}", other);
|
||||
}
|
||||
} else if self.b1.y == self.b2.y {
|
||||
if other.b1.y == other.b2.y {
|
||||
return Ok(false);
|
||||
} else if other.b1.x == other.b2.x {
|
||||
return Ok(self.get_x_range().contains(&other.b1.x)
|
||||
&& other.get_y_range().contains(&self.b1.y));
|
||||
} else {
|
||||
bail!("Did not follow instructions 2 {:?}", other);
|
||||
}
|
||||
} else {
|
||||
bail!("Did not follow instructions 3 {:?}", self);
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,12 +79,13 @@ impl<'a> Pair<'a> {
|
||||
);
|
||||
}
|
||||
|
||||
fn as_line(&self) {
|
||||
println!(
|
||||
"<line x1=\"{}\" y1=\"{}\" x2=\"{}\" y2=\"{}\" style=\"stroke:yellow;stroke-width:100\" />",
|
||||
self.b1.x, self.b1.y, self.b2.x, self.b2.y,
|
||||
);
|
||||
}
|
||||
// Usefull for debugging
|
||||
// fn as_line(&self) {
|
||||
// println!(
|
||||
// "<line x1=\"{}\" y1=\"{}\" x2=\"{}\" y2=\"{}\" style=\"stroke:yellow;stroke-width:100\" />",
|
||||
// self.b1.x, self.b1.y, self.b2.x, self.b2.y,
|
||||
// );
|
||||
// }
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
@@ -138,21 +120,17 @@ fn main() -> Result<()> {
|
||||
|
||||
pairs.sort_by(|a, b| a.area.cmp(&b.area));
|
||||
|
||||
let b = pairs.into_iter().rev().take(1).next().unwrap();
|
||||
let b = pairs.iter().rev().take(1).next().unwrap();
|
||||
|
||||
println!("sol1: {} {:?} {:?}", b.area, b.b1, b.b2);
|
||||
|
||||
let mut pairs: Vec<Pair> = Vec::with_capacity(boxes.len() * boxes.len());
|
||||
|
||||
for (i, b1) in boxes.iter().enumerate() {
|
||||
for b2 in boxes.iter().skip(i) {
|
||||
pairs.push(Pair::new(b1, b2))
|
||||
}
|
||||
}
|
||||
|
||||
pairs.sort_by(|a, b| a.area.cmp(&b.area));
|
||||
|
||||
'b2: for b in pairs.iter().rev() {
|
||||
// This check sees if there is a point inside the target square
|
||||
// this will probably fail if you have a case like this
|
||||
// #XXXX#
|
||||
// X ## X
|
||||
// #X##X#
|
||||
// Lucky we don't tho this could be saved by detecting those and just removing them...
|
||||
let max_x = b.b1.x.max(b.b2.x);
|
||||
let min_x = b.b1.x.min(b.b2.x);
|
||||
let max_y = b.b1.y.max(b.b2.y);
|
||||
@@ -163,11 +141,13 @@ fn main() -> Result<()> {
|
||||
continue 'b2;
|
||||
}
|
||||
}
|
||||
// Raycasting to detect when there is no square but we go outside bounds
|
||||
// this would also fail for the above case
|
||||
//
|
||||
// 1 p
|
||||
//
|
||||
//
|
||||
// t 2
|
||||
// 1 -> p
|
||||
// ↓
|
||||
// ↑
|
||||
// t <- 2
|
||||
//
|
||||
let p = TilePos {
|
||||
x: b.b2.x,
|
||||
@@ -199,65 +179,15 @@ fn main() -> Result<()> {
|
||||
};
|
||||
|
||||
for i in order_pair.iter() {
|
||||
let ray1c = i.colide(&ray1)?;
|
||||
let ray2c = i.colide(&ray2)?;
|
||||
let ray3c = i.colide(&ray3)?;
|
||||
let ray4c = i.colide(&ray4)?;
|
||||
if ray1c || ray2c || ray3c || ray4c {
|
||||
if b.area == 1550760868 {
|
||||
println!(
|
||||
"\n\n\np {} = {:?}\n\n{:?}\n{:?}\n\n{:?}\n{:?}",
|
||||
b.area, i, ray1, ray2, ray3, ray4
|
||||
);
|
||||
b.as_rect();
|
||||
i.as_line();
|
||||
}
|
||||
if i.colide(&ray1) || i.colide(&ray2) || i.colide(&ray3) || i.colide(&ray4) {
|
||||
continue 'b2;
|
||||
}
|
||||
}
|
||||
|
||||
println!("sol2 = {} {:?}", b.area, b);
|
||||
b.as_rect();
|
||||
ray1.as_line();
|
||||
ray2.as_line();
|
||||
ray3.as_line();
|
||||
ray4.as_line();
|
||||
break;
|
||||
}
|
||||
|
||||
// let boxes: Vec<TilePos> = text
|
||||
// .split("\n")
|
||||
// .filter(|s| s.len() > 0)
|
||||
// .map(|s| s.into())
|
||||
// .collect();
|
||||
|
||||
// let mut pairs: Vec<Pair> = Vec::with_capacity(boxes.len() * boxes.len());
|
||||
|
||||
// for (i, b1) in boxes.iter().enumerate() {
|
||||
// for b2 in boxes.iter().skip(i) {
|
||||
// if b1.y >= 50265 && b2.y >= 50265 {
|
||||
// pairs.push(Pair::new(b1, b2))
|
||||
// }
|
||||
// if b1.y <= 48488 && b2.y <= 48488 {
|
||||
// pairs.push(Pair::new(b1, b2))
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// pairs.sort_by(|a, b| a.area.cmp(&b.area));
|
||||
|
||||
// let pairs = pairs.iter().rev().take(20);
|
||||
|
||||
// for i in pairs {
|
||||
// // 1 p
|
||||
// //
|
||||
// //
|
||||
// // t 2
|
||||
// println!(
|
||||
// "<path style=\"fill:blue\" d=\"M{} {} L{} {} L{} {} L{} {}\" count=\"{}\">",
|
||||
// i.b1.x, i.b1.y, i.b2.x, i.b1.y, i.b2.x, i.b2.y, i.b1.x, i.b2.y, i.area
|
||||
// )
|
||||
// }
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user