some more problems solved
This commit is contained in:
31
valid-sudoku/sol.go
Normal file
31
valid-sudoku/sol.go
Normal file
@@ -0,0 +1,31 @@
|
||||
func isValidSudoku(board [][]byte) bool {
|
||||
rows := make(map[int]map[byte]bool)
|
||||
cols := make(map[int]map[byte]bool)
|
||||
sqrs := make(map[int]map[byte]bool)
|
||||
var cell byte
|
||||
|
||||
for rowIdx := 0; rowIdx < 9; rowIdx++ {
|
||||
rows[rowIdx] = make(map[byte]bool)
|
||||
for colIdx := 0; colIdx < 9; colIdx++ {
|
||||
if cols[colIdx] == nil{
|
||||
cols[colIdx] = make(map[byte]bool)
|
||||
}
|
||||
cell = board[rowIdx][colIdx]
|
||||
if cell != '.' {
|
||||
if rows[rowIdx][cell] || cols[colIdx][cell] {
|
||||
return false
|
||||
}
|
||||
sqr := rowIdx - (rowIdx % 3) + colIdx / 3
|
||||
if sqrs[sqr] == nil {
|
||||
sqrs[sqr] = make(map[byte]bool)
|
||||
}
|
||||
if sqrs[sqr][cell] {
|
||||
return false
|
||||
}
|
||||
rows[rowIdx][cell], cols[colIdx][cell], sqrs[sqr][cell] = true, true, true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
34
valid-sudoku/sol.rs
Normal file
34
valid-sudoku/sol.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
// TODO: Broken
|
||||
impl Solution {
|
||||
pub fn is_valid_sudoku(board: Vec<Vec<char>>) -> bool {
|
||||
let mut rows: HashMap<usize, HashSet<char>> = HashMap::new();
|
||||
let mut cols: HashMap<usize, HashSet<char>> = HashMap::new();
|
||||
let mut sqrs: HashMap<usize, HashSet<char>> = HashMap::new();
|
||||
|
||||
for row in (0..9usize) {
|
||||
rows.insert(row, HashSet::new());
|
||||
for col in (0..9usize) {
|
||||
if !cols.contains_key(&col) {
|
||||
cols.insert(col, HashSet::new());
|
||||
}
|
||||
let cell = *board.get(row).unwrap().get(col).unwrap();
|
||||
if cell != '.' {
|
||||
if !rows.get_mut(&row).unwrap().insert(cell) || !cols.get_mut(&row).unwrap().insert(cell) {
|
||||
return false;
|
||||
}
|
||||
let sqr = row - (row % 3) + col / 3;
|
||||
if !sqrs.contains_key(&sqr) {
|
||||
sqrs.insert(sqr, HashSet::new());
|
||||
}
|
||||
if !sqrs.get_mut(&sqr).unwrap().insert(cell) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
}
|
49
valid-sudoku/sol_functional.rs
Normal file
49
valid-sudoku/sol_functional.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
// A more functional (and much worse imo) solution
|
||||
impl Solution {
|
||||
#[inline]
|
||||
pub fn contains_duplicate(row: &Vec<char>) -> bool {
|
||||
let mut map = 0;
|
||||
row
|
||||
.iter()
|
||||
.filter_map(|chr| {
|
||||
if *chr != '.' {
|
||||
return Some(1 << (*chr as usize - 49));
|
||||
}
|
||||
None
|
||||
})
|
||||
.any(|chr| {
|
||||
if map & chr != 0 {
|
||||
return true;
|
||||
}
|
||||
map |= chr;
|
||||
false
|
||||
})
|
||||
}
|
||||
pub fn is_valid_sudoku(board: Vec<Vec<char>>) -> bool {
|
||||
!(
|
||||
board.iter() // Rows
|
||||
.any(|row| Solution::contains_duplicate(row)) ||
|
||||
(0..9).into_iter() // Columns
|
||||
.map(|idx| board.iter()
|
||||
.map(|row| row[idx])
|
||||
.collect::<Vec<char>>()
|
||||
)
|
||||
.any(|row| Solution::contains_duplicate(&row)) ||
|
||||
(0..9).into_iter() // 3 by 3 squares
|
||||
.map(|idx| {
|
||||
let (row_idx, col_idx) = (idx as usize / 3, idx as usize % 3);
|
||||
board.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(r, row)| {
|
||||
if r / 3 == row_idx {
|
||||
return Some(row[col_idx*3..(col_idx*3 + 3)].to_vec());
|
||||
}
|
||||
None
|
||||
})
|
||||
.flatten()
|
||||
.collect::<Vec<char>>()
|
||||
})
|
||||
.any(|row| Solution::contains_duplicate(&row))
|
||||
)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user