Compare commits

...

4 Commits

Author SHA1 Message Date
8a524236cc Remove unused import 2025-05-28 17:10:48 -05:00
d8136c2ff4 Format code 2025-05-28 17:03:20 -05:00
ce37b21d3d Remove bad whitespace 2025-05-28 17:02:33 -05:00
465d18ea67 Implement taking strings from crossword 2025-05-28 17:01:56 -05:00
2 changed files with 94 additions and 0 deletions

View File

@@ -10,6 +10,27 @@ pub struct Crossword {
height: usize, height: usize,
} }
pub struct WordPuller<'a> {
x: usize,
y: usize,
direction: (isize, isize),
crossword: &'a Crossword,
}
impl<'a> WordPuller<'a> {
pub fn take(&self, num_of_letters: usize) -> String {
let mut word = String::new();
for i in 0..num_of_letters {
let x = self.x as isize + (i as isize * self.direction.0);
let y = self.y as isize + (i as isize * self.direction.1);
word.push(self.crossword.data[x as usize][y as usize]);
}
word
}
}
impl Crossword { impl Crossword {
pub fn new(file: File) -> Result<Crossword, Box<dyn Error>> { pub fn new(file: File) -> Result<Crossword, Box<dyn Error>> {
let mut input_buffer = BufReader::new(file); let mut input_buffer = BufReader::new(file);
@@ -44,6 +65,71 @@ impl Crossword {
pub fn height(&self) -> usize { pub fn height(&self) -> usize {
self.height self.height
} }
pub fn n(&self, x: usize, y: usize) -> WordPuller {
WordPuller {
x,
y,
direction: (-1, 0),
crossword: self,
}
}
pub fn ne(&self, x: usize, y: usize) -> WordPuller {
WordPuller {
x,
y,
direction: (-1, 1),
crossword: self,
}
}
pub fn e(&self, x: usize, y: usize) -> WordPuller {
WordPuller {
x,
y,
direction: (0, 1),
crossword: self,
}
}
pub fn se(&self, x: usize, y: usize) -> WordPuller {
WordPuller {
x,
y,
direction: (1, 1),
crossword: self,
}
}
pub fn s(&self, x: usize, y: usize) -> WordPuller {
WordPuller {
x,
y,
direction: (1, 0),
crossword: self,
}
}
pub fn sw(&self, x: usize, y: usize) -> WordPuller {
WordPuller {
x,
y,
direction: (1, -1),
crossword: self,
}
}
pub fn w(&self, x: usize, y: usize) -> WordPuller {
WordPuller {
x,
y,
direction: (0, -1),
crossword: self,
}
}
pub fn nw(&self, x: usize, y: usize) -> WordPuller {
WordPuller {
x,
y,
direction: (-1, -1),
crossword: self,
}
}
} }
impl Display for Crossword { impl Display for Crossword {

View File

@@ -24,6 +24,14 @@ fn main() -> Result<(), Box<dyn Error>> {
let crossword = Crossword::new(File::open(input_file_string)?)?; let crossword = Crossword::new(File::open(input_file_string)?)?;
println!("{}", crossword); println!("{}", crossword);
println!("n: {}", crossword.n(3, 3).take(4));
println!("ne: {}", crossword.ne(3, 3).take(4));
println!("e: {}", crossword.e(3, 3).take(4));
println!("se: {}", crossword.se(3, 3).take(4));
println!("s: {}", crossword.s(3, 3).take(4));
println!("sw: {}", crossword.sw(3, 3).take(4));
println!("w: {}", crossword.w(3, 3).take(4));
println!("nw: {}", crossword.nw(3, 3).take(4));
Ok(()) Ok(())
} }