diff --git a/src/common/geometry.rs b/src/common/geometry.rs index 386d772..4c1cc34 100644 --- a/src/common/geometry.rs +++ b/src/common/geometry.rs @@ -2,8 +2,16 @@ pub struct Point2(pub u64, pub u64); impl Point2 { - pub fn from_line(line: &str, separator: char) -> Self { - let p: Vec = line.split(separator).map(|s| s.parse().unwrap()).collect(); + const POINT_SPLIT_CHAR: char = ','; + + pub fn from_line(line: &str) -> Self { + let p: Vec = line + .split(Self::POINT_SPLIT_CHAR) + .map(|s| { + s.parse() + .expect(&format!("could not create Point2 from line '{line}'")) + }) + .collect(); Self(p[0], p[1]) } @@ -16,8 +24,16 @@ impl Point2 { pub struct Point3(pub u64, pub u64, pub u64); impl Point3 { - pub fn from_line(line: &str, separator: char) -> Self { - let p: Vec = line.split(separator).map(|s| s.parse().unwrap()).collect(); + const POINT_SPLIT_CHAR: char = ','; + + pub fn from_line(line: &str) -> Self { + let p: Vec = line + .split(Self::POINT_SPLIT_CHAR) + .map(|s| { + s.parse() + .expect(&format!("could not create Point3 from line '{line}'")) + }) + .collect(); Self(p[0], p[1], p[2]) } diff --git a/src/solvers/movie_theater.rs b/src/solvers/movie_theater.rs index 8f8df77..9da11b9 100644 --- a/src/solvers/movie_theater.rs +++ b/src/solvers/movie_theater.rs @@ -5,13 +5,11 @@ use std::io::BufRead; pub struct MovieTheater {} impl MovieTheater { - const POINT_SPLIT_CHAR: char = ','; - // Collects all red tile coordinates. fn red_tiles(reader: R) -> Vec { let mut red_tiles = Vec::new(); for line in reader.lines().map_while(Result::ok) { - red_tiles.push(Point2::from_line(&line, Self::POINT_SPLIT_CHAR)); + red_tiles.push(Point2::from_line(&line)); } red_tiles } diff --git a/src/solvers/playground.rs b/src/solvers/playground.rs index 82bc775..8df16ff 100644 --- a/src/solvers/playground.rs +++ b/src/solvers/playground.rs @@ -8,11 +8,10 @@ pub struct Playground { } impl Playground { - const POINT_SPLIT_CHAR: char = ','; const CIRCUITS_TAKE: usize = 3; - pub fn new(connections: Option) -> Playground { - Playground { + pub fn new(connections: Option) -> Self { + Self { connections: connections.unwrap_or(1000), } } @@ -21,7 +20,7 @@ impl Playground { fn junction_boxes(reader: R) -> Vec { let mut junction_boxes = Vec::new(); for line in reader.lines().map_while(Result::ok) { - junction_boxes.push(Point3::from_line(&line, Self::POINT_SPLIT_CHAR)); + junction_boxes.push(Point3::from_line(&line)); } junction_boxes }