1
0

Remove split char parameter from Point2 and Point3 constructors

This commit is contained in:
Stefan Müller
2026-01-26 21:59:12 +01:00
parent d28f2ff79e
commit 8f9356e81e
3 changed files with 24 additions and 11 deletions
+20 -4
View File
@@ -2,8 +2,16 @@
pub struct Point2(pub u64, pub u64); pub struct Point2(pub u64, pub u64);
impl Point2 { impl Point2 {
pub fn from_line(line: &str, separator: char) -> Self { const POINT_SPLIT_CHAR: char = ',';
let p: Vec<u64> = line.split(separator).map(|s| s.parse().unwrap()).collect();
pub fn from_line(line: &str) -> Self {
let p: Vec<u64> = 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]) Self(p[0], p[1])
} }
@@ -16,8 +24,16 @@ impl Point2 {
pub struct Point3(pub u64, pub u64, pub u64); pub struct Point3(pub u64, pub u64, pub u64);
impl Point3 { impl Point3 {
pub fn from_line(line: &str, separator: char) -> Self { const POINT_SPLIT_CHAR: char = ',';
let p: Vec<u64> = line.split(separator).map(|s| s.parse().unwrap()).collect();
pub fn from_line(line: &str) -> Self {
let p: Vec<u64> = 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]) Self(p[0], p[1], p[2])
} }
+1 -3
View File
@@ -5,13 +5,11 @@ use std::io::BufRead;
pub struct MovieTheater {} pub struct MovieTheater {}
impl MovieTheater { impl MovieTheater {
const POINT_SPLIT_CHAR: char = ',';
// Collects all red tile coordinates. // Collects all red tile coordinates.
fn red_tiles<R: BufRead>(reader: R) -> Vec<Point2> { fn red_tiles<R: BufRead>(reader: R) -> Vec<Point2> {
let mut red_tiles = Vec::new(); let mut red_tiles = Vec::new();
for line in reader.lines().map_while(Result::ok) { 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 red_tiles
} }
+3 -4
View File
@@ -8,11 +8,10 @@ pub struct Playground {
} }
impl Playground { impl Playground {
const POINT_SPLIT_CHAR: char = ',';
const CIRCUITS_TAKE: usize = 3; const CIRCUITS_TAKE: usize = 3;
pub fn new(connections: Option<usize>) -> Playground { pub fn new(connections: Option<usize>) -> Self {
Playground { Self {
connections: connections.unwrap_or(1000), connections: connections.unwrap_or(1000),
} }
} }
@@ -21,7 +20,7 @@ impl Playground {
fn junction_boxes<R: BufRead>(reader: R) -> Vec<Point3> { fn junction_boxes<R: BufRead>(reader: R) -> Vec<Point3> {
let mut junction_boxes = Vec::new(); let mut junction_boxes = Vec::new();
for line in reader.lines().map_while(Result::ok) { 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 junction_boxes
} }