Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions cspuz_rs_puzzles/src/puzzles/canalview.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use crate::util;
use cspuz_rs::graph;
use cspuz_rs::serializer::{
problem_to_url, url_to_problem, Choice, Combinator, Dict, Grid, HexInt, Optionalize, Spaces,
};
use cspuz_rs::solver::Solver;

pub fn solve_canalview(clues: &[Vec<Option<i32>>]) -> Option<Vec<Vec<Option<bool>>>> {
let (h, w) = util::infer_shape(clues);

let mut solver = Solver::new();
let is_black = &solver.bool_var_2d((h, w));
solver.add_answer_key_bool(is_black);

graph::active_vertices_connected_2d(&mut solver, is_black);
solver.add_expr(!is_black.conv2d_and((2, 2)));

for y in 0..h {
for x in 0..w {
if let Some(n) = clues[y][x] {
solver.add_expr(!is_black.at((y, x)));
if n < 0 {
continue;
}
let up = is_black.slice_fixed_x((..y, x)).reverse();
let down = is_black.slice_fixed_x(((y + 1).., x));
let left = is_black.slice_fixed_y((y, ..x)).reverse();
let right = is_black.slice_fixed_y((y, (x + 1)..));
solver.add_expr(
(up.consecutive_prefix_true()
+ down.consecutive_prefix_true()
+ left.consecutive_prefix_true()
+ right.consecutive_prefix_true())
.eq(n),
);
}
}
}

solver.irrefutable_facts().map(|f| f.get(is_black))
}

type Problem = Vec<Vec<Option<i32>>>;

fn combinator() -> impl Combinator<Problem> {
Grid::new(Choice::new(vec![
Box::new(Optionalize::new(HexInt)),
Box::new(Spaces::new(None, 'g')),
Box::new(Dict::new(Some(-1), ".")),
]))
}

pub fn serialize_problem(problem: &Problem) -> Option<String> {
problem_to_url(combinator(), "canal", problem.clone())
}

pub fn deserialize_problem(url: &str) -> Option<Problem> {
url_to_problem(combinator(), &["canal"], url)
}

#[cfg(test)]
mod tests {
use super::*;

#[rustfmt::skip]
fn problem_for_tests() -> Problem {
vec![
vec![Some(3), None, None, None, Some(3), None],
vec![None, Some(2), None, None, None, None],
vec![None, None, None, None, None, None],
vec![None, None, None, None, None, None],
vec![None, None, None, None, Some(4), None],
vec![None, Some(5), None, None, None, Some(2)],
]
}

#[test]
fn test_canalview_problem() {
let problem = problem_for_tests();
let ans = solve_canalview(&problem);
assert!(ans.is_some());
let ans = ans.unwrap();
let expected = crate::util::tests::to_option_bool_2d([
[0, 1, 1, 1, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 1, 0],
[0, 1, 1, 1, 1, 1],
[1, 1, 0, 1, 0, 1],
[1, 0, 1, 1, 0, 0],
]);
assert_eq!(ans, expected);
}

#[test]
fn test_canalview_serializer() {
let problem = problem_for_tests();
let url = "https://puzz.link/p?canal/6/6/3i3h2z4h5i2";
util::tests::serializer_test(problem, url, serialize_problem, deserialize_problem);
}
}
1 change: 1 addition & 0 deletions cspuz_rs_puzzles/src/puzzles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod barns;
pub mod battleship;
pub mod bdwalk;
pub mod bosanowa;
pub mod canalview;
pub mod castle_walker;
pub mod castle_wall;
pub mod cave;
Expand Down
94 changes: 94 additions & 0 deletions cspuz_solver_backend/src/puzzle/canalview.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use crate::board::{Board, BoardKind, Item, ItemKind};
use crate::uniqueness::check_uniqueness;
use cspuz_rs_puzzles::puzzles::canalview;

pub fn solve(url: &str) -> Result<Board, &'static str> {
let problem = canalview::deserialize_problem(url).ok_or("invalid url")?;
let ans = canalview::solve_canalview(&problem);

let height = problem.len();
let width = problem[0].len();
let mut board = Board::new(BoardKind::Grid, height, width, check_uniqueness(&ans));
for y in 0..height {
for x in 0..width {
if let Some(clue) = problem[y][x] {
if clue > 0 {
board.push(Item::cell(y, x, "black", ItemKind::Num(clue)));
} else {
board.push(Item::cell(y, x, "black", ItemKind::Text("?")));
}
} else if let Some(ans) = &ans {
if let Some(a) = ans[y][x] {
board.push(Item::cell(
y,
x,
"green",
if a { ItemKind::Block } else { ItemKind::Dot },
));
}
}
}
}

Ok(board)
}

#[cfg(test)]
mod tests {
use super::solve;
use crate::board::*;
use crate::compare_board_and_check_no_solution_case;
use crate::uniqueness::Uniqueness;

#[test]
#[rustfmt::skip]
fn test_solve() {
compare_board_and_check_no_solution_case!(
solve("https://puzz.link/p?canal/6/6/3i3h2z4h5i2"),
Board {
kind: BoardKind::Grid,
height: 6,
width: 6,
data: vec![
Item { y: 1, x: 1, color: "black", kind: ItemKind::Num(3) },
Item { y: 1, x: 3, color: "green", kind: ItemKind::Block },
Item { y: 1, x: 5, color: "green", kind: ItemKind::Block },
Item { y: 1, x: 7, color: "green", kind: ItemKind::Block },
Item { y: 1, x: 9, color: "black", kind: ItemKind::Num(3) },
Item { y: 1, x: 11, color: "green", kind: ItemKind::Dot },
Item { y: 3, x: 1, color: "green", kind: ItemKind::Dot },
Item { y: 3, x: 3, color: "black", kind: ItemKind::Num(2) },
Item { y: 3, x: 5, color: "green", kind: ItemKind::Block },
Item { y: 3, x: 7, color: "green", kind: ItemKind::Dot },
Item { y: 3, x: 9, color: "green", kind: ItemKind::Dot },
Item { y: 3, x: 11, color: "green", kind: ItemKind::Dot },
Item { y: 5, x: 1, color: "green", kind: ItemKind::Dot },
Item { y: 5, x: 3, color: "green", kind: ItemKind::Dot },
Item { y: 5, x: 5, color: "green", kind: ItemKind::Block },
Item { y: 5, x: 7, color: "green", kind: ItemKind::Dot },
Item { y: 5, x: 9, color: "green", kind: ItemKind::Block },
Item { y: 5, x: 11, color: "green", kind: ItemKind::Dot },
Item { y: 7, x: 1, color: "green", kind: ItemKind::Dot },
Item { y: 7, x: 3, color: "green", kind: ItemKind::Block },
Item { y: 7, x: 5, color: "green", kind: ItemKind::Block },
Item { y: 7, x: 7, color: "green", kind: ItemKind::Block },
Item { y: 7, x: 9, color: "green", kind: ItemKind::Block },
Item { y: 7, x: 11, color: "green", kind: ItemKind::Block },
Item { y: 9, x: 1, color: "green", kind: ItemKind::Block },
Item { y: 9, x: 3, color: "green", kind: ItemKind::Block },
Item { y: 9, x: 5, color: "green", kind: ItemKind::Dot },
Item { y: 9, x: 7, color: "green", kind: ItemKind::Block },
Item { y: 9, x: 9, color: "black", kind: ItemKind::Num(4) },
Item { y: 9, x: 11, color: "green", kind: ItemKind::Block },
Item { y: 11, x: 1, color: "green", kind: ItemKind::Block },
Item { y: 11, x: 3, color: "black", kind: ItemKind::Num(5) },
Item { y: 11, x: 5, color: "green", kind: ItemKind::Block },
Item { y: 11, x: 7, color: "green", kind: ItemKind::Block },
Item { y: 11, x: 9, color: "green", kind: ItemKind::Dot },
Item { y: 11, x: 11, color: "black", kind: ItemKind::Num(2) },
],
uniqueness: Uniqueness::Unique,
},
);
}
}
1 change: 1 addition & 0 deletions cspuz_solver_backend/src/puzzle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ puzzle_list!(puzz_link,
(battleship, ["battleship"], "Battleship", "Battleship"),
(bdwalk, ["bdwalk"], "Building Walk", "ビルウォーク"),
(bosanowa, ["bosanowa"], "Bosanowa", "ボサノワ"),
(canalview, ["canal"], "Canal View", "Canal View"),
(castle_wall, ["castle"], "Castle Wall", "Castle Wall"),
(cave, ["cave"], "Cave", "バッグ"),
(chainedb, ["chainedb"], "Chained Block", "チェンブロ"),
Expand Down