Impl: Get possible values from row

This commit is contained in:
Jonas Zeunert
2024-02-19 18:55:27 +01:00
parent 4199084e6b
commit beaa3cfd61
2 changed files with 25 additions and 5 deletions

View File

@@ -2,7 +2,7 @@ use std::fmt;
#[derive(PartialEq, Debug, Clone)] #[derive(PartialEq, Debug, Clone)]
pub struct Playfield { pub struct Playfield {
size: usize, pub size: usize,
pub fields: Vec<Vec<Field>>, pub fields: Vec<Vec<Field>>,
pub open_fields: Vec<usize>, pub open_fields: Vec<usize>,
} }
@@ -20,7 +20,7 @@ impl Playfield {
playfield.parse(field); playfield.parse(field);
return playfield; playfield
} }
fn parse(&mut self, field: &String) { fn parse(&mut self, field: &String) {

View File

@@ -1,3 +1,5 @@
use std::collections::HashSet;
use crate::playfield::Field; use crate::playfield::Field;
use crate::playfield::Playfield; use crate::playfield::Playfield;
@@ -5,7 +7,7 @@ pub struct SudokuSolver {
playfield: Playfield, playfield: Playfield,
} }
impl<'a> SudokuSolver { impl SudokuSolver {
pub fn new(playfield: &Playfield) -> SudokuSolver { pub fn new(playfield: &Playfield) -> SudokuSolver {
Self { Self {
playfield: playfield.clone(), playfield: playfield.clone(),
@@ -18,17 +20,33 @@ impl<'a> SudokuSolver {
fn populate_possible_values(&self) { fn populate_possible_values(&self) {
for open_field in &self.playfield.open_fields {} for open_field in &self.playfield.open_fields {}
} }
fn possible_values_from_row(&self, row: Vec<Field>) -> Vec<u32> {} fn possible_values_from_row(&self, row: &Vec<Field>) -> Vec<u32> {
let max_value = self.playfield.size as u32;
let possible_values: HashSet<u32> = (1..max_value + 1).collect();
let populated_values: HashSet<u32> = row
.iter()
.filter(|field| field.value.is_some())
.map(|v| v.value.unwrap())
.collect();
possible_values
.symmetric_difference(&populated_values)
.map(|v| *v)
.collect()
}
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::sudoku_solver::SudokuSolver; use crate::sudoku_solver::SudokuSolver;
use crate::Playfield; use crate::Playfield;
fn create_test_playfield(input: &str, size: usize) -> Playfield { fn create_test_playfield(input: &str, size: usize) -> Playfield {
Playfield::new(&input.to_string(), size) Playfield::new(&input.to_string(), size)
} }
mod solve { mod solve {
use super::*; use super::*;
#[test] #[test]
fn simple() { fn simple() {
let playfield = create_test_playfield("1 2 3 2 3 1 3 1 0", 3); let playfield = create_test_playfield("1 2 3 2 3 1 3 1 0", 3);
@@ -42,13 +60,15 @@ mod tests {
mod populate_possible_values { mod populate_possible_values {
mod possible_values_from_row { mod possible_values_from_row {
use super::super::*; use super::super::*;
#[test] #[test]
fn simple() { fn simple() {
let playfield = create_test_playfield("1 2 0 1 2 0 1 2 0", 3); let playfield = create_test_playfield("1 2 0 1 2 0 1 2 0", 3);
let expected = vec![3]; let expected = vec![3];
let possible_values = let possible_values =
SudokuSolver::new(&playfield).possible_values_from_row(playfield.fields[0]); SudokuSolver::new(&playfield).possible_values_from_row(&playfield.fields[0]);
assert_eq!(possible_values, expected); assert_eq!(possible_values, expected);
} }
} }