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
121 changes: 121 additions & 0 deletions cspuz_rs_puzzles/src/puzzles/lightandshadow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
use crate::util;
use cspuz_rs::graph;
use cspuz_rs::serializer::{
problem_to_url_pzprxs, url_to_problem, Choice, Combinator, Grid, HexInt, Map, Optionalize,
Spaces,
};
use cspuz_rs::solver::Solver;

pub fn solve_lightandshadow(clues: &[Vec<Option<(i32, bool)>>]) -> 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);

let mut clue_pos = vec![];
for y in 0..h {
for x in 0..w {
if let Some((n, c)) = clues[y][x] {
clue_pos.push((y, x, n));
solver.add_expr(is_black.at((y, x)) ^ !c);
}
}
}

let group_id = solver.int_var_2d((h, w), 1, clue_pos.len() as i32);

for i in 1..=clue_pos.len() {
graph::active_vertices_connected_2d(&mut solver, group_id.eq(i as i32));
}

solver.add_expr(
(is_black.slice((.., ..(w - 1))) ^ !is_black.slice((.., 1..))).iff(
group_id
.slice((.., ..(w - 1)))
.eq(group_id.slice((.., 1..))),
),
);
solver.add_expr(
(is_black.slice((..(h - 1), ..)) ^ !is_black.slice((1.., ..))).iff(
group_id
.slice((..(h - 1), ..))
.eq(group_id.slice((1.., ..))),
),
);

for (i, &(y, x, n)) in clue_pos.iter().enumerate() {
solver.add_expr(group_id.at((y, x)).eq((i + 1) as i32));
if n > 0 {
solver.add_expr(group_id.eq((i + 1) as i32).count_true().eq(n));
}
}

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

fn clue_combinator() -> impl Combinator<(i32, bool)> {
Map::new(
HexInt,
|(x, y): (i32, bool)| match y {
false => Some(2 * x),
true => Some(2 * x + 1),
},
|n: i32| match n {
i => Some((i / 2, i % 2 == 1)),
},
)
}

type Problem = Vec<Vec<Option<(i32, bool)>>>;

fn combinator() -> impl Combinator<Problem> {
Grid::new(Choice::new(vec![
Box::new(Optionalize::new(clue_combinator())),
Box::new(Spaces::new(None, 'g')),
]))
}

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

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

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

fn problem_for_tests() -> Problem {
vec![
vec![Some((2, false)), None, Some((2, false)), None],
vec![None, None, None, None],
vec![None, None, Some((3, false)), None],
vec![None, Some((3, true)), None, Some((0, true))],
]
}

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

#[test]
fn test_lightandshadow_serializer() {
let problem = problem_for_tests();
let url = "https://pzprxs.vercel.app/p?lightshadow/4/4/4g4m6h7g1";
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 @@ -69,6 +69,7 @@ pub mod kurodoko;
pub mod kurotto;
pub mod lapaz;
pub mod letter_weights;
pub mod lightandshadow;
pub mod litherslink;
pub mod lits;
pub mod lohkous;
Expand Down
72 changes: 72 additions & 0 deletions cspuz_solver_backend/src/puzzle/lightandshadow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use crate::board::{Board, BoardKind, Item, ItemKind};
use crate::uniqueness::check_uniqueness;
use cspuz_rs_puzzles::puzzles::lightandshadow;

pub fn solve(url: &str) -> Result<Board, &'static str> {
let problem = lightandshadow::deserialize_problem(url).ok_or("invalid url")?;
let ans = lightandshadow::solve_lightandshadow(&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, state)) = problem[y][x] {
if !state {
if clue > 0 {
board.push(Item::cell(y, x, "black", ItemKind::Num(clue)));
} else {
board.push(Item::cell(y, x, "black", ItemKind::Text("?")));
}
} else {
board.push(Item::cell(y, x, "black", ItemKind::Fill));
if clue > 0 {
board.push(Item::cell(y, x, "white", ItemKind::Num(clue)));
} else {
board.push(Item::cell(y, x, "white", 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://pzprxs.vercel.app/p?lightshadow/2/2/05h"),
Board {
kind: BoardKind::Grid,
height: 2,
width: 2,
data: vec![
Item { y: 1, x: 1, color: "black", kind: ItemKind::Text("?") },
Item { y: 1, x: 3, color: "black", kind: ItemKind::Fill },
Item { y: 1, x: 3, color: "white", kind: ItemKind::Num(2) },
Item { y: 3, x: 1, color: "green", kind: ItemKind::Dot },
Item { y: 3, x: 3, color: "green", kind: ItemKind::Block },
],
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 @@ -148,6 +148,7 @@ puzzle_list!(puzz_link,
(kurodoko, ["kurodoko"], "Kurodoko", "黒どこ"),
(kurotto, ["kurotto"], "Kurotto", "クロット"),
(lapaz, ["lapaz"], "La Paz", "La Paz"),
(lightandshadow, ["lightshadow"], "Light and Shadow", "Light and Shadow"),
(litherslink, ["lither"], "Litherslink", "Litherslink"),
(lits, ["lits"], "LITS", "LITS"),
(lohkous, ["lohkous"], "Lohkous", "Lohkous"),
Expand Down
Loading