Pull Crossword out into it's own module
This commit is contained in:
57
day_04/src/crossword.rs
Normal file
57
day_04/src/crossword.rs
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
use std::error::Error;
|
||||||
|
use std::fmt::Display;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{BufReader, prelude::*};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Crossword {
|
||||||
|
data: Vec<Vec<char>>,
|
||||||
|
width: usize,
|
||||||
|
height: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Crossword {
|
||||||
|
pub fn new(file: File) -> Result<Crossword, Box<dyn Error>> {
|
||||||
|
let mut input_buffer = BufReader::new(file);
|
||||||
|
|
||||||
|
let mut data: Vec<Vec<char>> = Vec::new();
|
||||||
|
|
||||||
|
let mut first_line = String::new();
|
||||||
|
input_buffer.read_line(&mut first_line)?;
|
||||||
|
let first_line = first_line.trim_end().to_owned();
|
||||||
|
let width = first_line.len();
|
||||||
|
for line in input_buffer.lines() {
|
||||||
|
let line = line?;
|
||||||
|
if line.len() != width {
|
||||||
|
println!("{}", line);
|
||||||
|
Err("Line length of input are not consistent")?;
|
||||||
|
}
|
||||||
|
data.push(line.chars().collect());
|
||||||
|
}
|
||||||
|
let height = data.len();
|
||||||
|
|
||||||
|
Ok(Crossword {
|
||||||
|
data,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn width(&self) -> usize {
|
||||||
|
self.width
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn height(&self) -> usize {
|
||||||
|
self.height
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for Crossword {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
for row in &self.data {
|
||||||
|
let row = String::from_iter(row.iter());
|
||||||
|
writeln!(f, "{}", row)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,54 +4,11 @@
|
|||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fmt::Display;
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{BufReader, prelude::*};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
mod crossword;
|
||||||
struct Crossword {
|
|
||||||
data: Vec<Vec<char>>,
|
|
||||||
width: usize,
|
|
||||||
height: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Crossword {
|
use crate::crossword::Crossword;
|
||||||
fn new(file: File) -> Result<Crossword, Box<dyn Error>> {
|
|
||||||
let mut input_buffer = BufReader::new(file);
|
|
||||||
|
|
||||||
let mut data: Vec<Vec<char>> = Vec::new();
|
|
||||||
|
|
||||||
let mut first_line = String::new();
|
|
||||||
input_buffer.read_line(&mut first_line)?;
|
|
||||||
let first_line = first_line.trim_end().to_owned();
|
|
||||||
let width = first_line.len();
|
|
||||||
for line in input_buffer.lines() {
|
|
||||||
let line = line?;
|
|
||||||
if line.len() != width {
|
|
||||||
println!("{}", line);
|
|
||||||
Err("Line length of input are not consistent")?;
|
|
||||||
}
|
|
||||||
data.push(line.chars().collect());
|
|
||||||
}
|
|
||||||
let height = data.len();
|
|
||||||
|
|
||||||
Ok(Crossword {
|
|
||||||
data,
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for Crossword {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
for row in &self.data {
|
|
||||||
let row = String::from_iter(row.iter());
|
|
||||||
writeln!(f, "{}", row)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
// Handle command input
|
// Handle command input
|
||||||
|
|||||||
Reference in New Issue
Block a user