From 698346e1c67c73e31b1f90b48c5be14a3d68a99e Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Thu, 24 Jan 2019 19:13:19 +0100 Subject: [PATCH] Read input from stdin --- lisarennt1.txt | 3 +++ src/display.rs | 6 +++--- src/input.rs | 31 +++++++++++++++++++++++++++++++ src/main.rs | 29 +++++++++++++++++++++++++++-- 4 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 lisarennt1.txt diff --git a/lisarennt1.txt b/lisarennt1.txt new file mode 100644 index 0000000..f137dab --- /dev/null +++ b/lisarennt1.txt @@ -0,0 +1,3 @@ +1 +3 535 410 610 70 460 70 +633 189 diff --git a/src/display.rs b/src/display.rs index ce91e28..edc8149 100644 --- a/src/display.rs +++ b/src/display.rs @@ -11,7 +11,7 @@ use super::*; pub(crate) fn dump_svg(points: &[(Point, &'static str)], lines: &[(Line, &'static str)], polys: &[Polygon]) { let mut document = Document::new() // view box at (0, -200), w*h (200, 200) - .set("viewBox", (0, -200, 200, 200)); + .set("viewBox", (0, -1100, 2100, 2100)); for circle in points.iter().map(|(p, color)| { Circle::new() @@ -31,7 +31,7 @@ pub(crate) fn dump_svg(points: &[(Point, &'static str)], lines: &[(Line, &'stati Path::new() .set("fill", "none") .set("stroke", *color) - .set("stroke-width", 0.5) + .set("stroke-width", 0.25) .set("d", data) }) { document.append(path); @@ -62,4 +62,4 @@ pub(crate) fn dump_svg(points: &[(Point, &'static str)], lines: &[(Line, &'stati } svg::write(io::stdout(), &document).unwrap(); -} \ No newline at end of file +} diff --git a/src/input.rs b/src/input.rs index 93b3f7a..2b48993 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,11 +1,42 @@ use super::*; +use std::io; +use std::io::prelude::*; + pub(crate) struct InputData { pub start: Point, pub polys: Vec } pub(crate) fn read_input() -> InputData { + //custom_input() + read_stdin() +} + +fn read_stdin() -> InputData { + let stdin = io::stdin(); + let stdin = stdin.lock(); + + let mut lines = stdin.lines().map(Result::unwrap); + let polygon_count = lines.next().unwrap().parse().unwrap(); + let mut polygons = Vec::with_capacity(polygon_count); + + for _ in 0..polygon_count { + let line = lines.next().unwrap(); + let numbers = line.trim().split(' ').map(|x| x.parse::().unwrap()).collect::>(); + let corner_count = numbers[0]; + polygons.push(Polygon::new((0..corner_count).map(|idx| ((numbers[idx*2+1] as f64).into(), (numbers[idx*2+2] as f64).into())).collect::>().into(), vec![])); + } + + let home = lines.next().unwrap().trim().split(' ').map(|x| x.parse::().unwrap().into()).collect::>(); + + InputData { + start: Point::new(home[0], home[1]), + polys: polygons + } +} + +fn custom_input() -> InputData { let mut polys = vec![]; polys.push(Polygon::new(vec![[2.0.into(), 0.0.into()], [6.0.into(), 0.0.into()], [6.0.into(), 6.0.into()], [2.0.into(), 5.0.into()]].into(), vec![])); polys.push(Polygon::new(vec![[1.0.into(), 20.0.into()], [2.0.into(), 20.0.into()], [2.0.into(), 23.0.into()], [1.0.into(), 23.0.into()]].into(), vec![])); diff --git a/src/main.rs b/src/main.rs index ad209f7..0f0b729 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,11 +39,17 @@ impl cmp::PartialOrd for RunState { } fn main() { - let bus = Point::new(0.0.into(), 0.0.into()); + let bus = Point::new(0.0.into(), (-1300.0).into()); let data = input::read_input(); let house = data.start; let polys = data.polys; + eprintln!("{:?}", house); + eprintln!("{:?}", polys); + for p in &polys { + assert!(p.interiors.is_empty()); + //assert!(p.is_convex()); + } let points = polys.iter().map(|x| x.exterior.0.clone()).flatten().collect::>(); let start = RunState { @@ -125,6 +131,10 @@ fn main() { } eprintln!("d = {:?}", best_delay); let route = best; + eprintln!("Route:"); + for s in &route { + eprintln!("{:.02},{:.02}", s.pos.x(), s.pos.y()); + } let first = route.first().unwrap(); let last = route.last().unwrap(); let points = vec![(house, "red"), (first.bus.translate(0.0.into(), best_delay), "yellow"), (last.bus, "orange")]; @@ -143,11 +153,26 @@ fn float_range(a: f64, b: f64) -> impl Iterator { } fn none_intersect(polys: &[Polygon], line: &Line) -> bool { + /* + if line.end.x > 0.0 { + eprintln!("{:?}", line); + } + */ for p in polys { - for l in p.exterior.lines() { + for l in p.exterior.lines().chain(vec![Line::new(p.exterior.0[0], *p.exterior.0.last().unwrap())]) { if l.start == line.start || l.end == line.end || l.start == line.end || l.end == line.start { + /* + if line.end.x > 0.0 { + eprintln!("skipping {:?}", l); + } + */ continue; // would always intersect with itself } + /* + if line.end.x > 0.0 { + eprintln!("intersect with {:?}", l); + } + */ if l.intersects(line) { return false; }