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/doubleback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
use cspuz_rs::graph;
use cspuz_rs::serializer::{
problem_to_url_with_context, url_to_problem, Combinator, Context, Rooms, Size,
};
use cspuz_rs::solver::{count_true, Solver};

pub fn solve_doubleback(
borders: &graph::InnerGridEdges<Vec<Vec<bool>>>,
) -> Option<graph::BoolGridEdgesIrrefutableFacts> {
let (h, w) = borders.base_shape();

let mut solver = Solver::new();
let rooms = graph::borders_to_rooms(borders);
let is_line = &graph::BoolGridEdges::new(&mut solver, (h - 1, w - 1));
solver.add_answer_key_bool(&is_line.horizontal);
solver.add_answer_key_bool(&is_line.vertical);

let is_passed = &graph::single_cycle_grid_edges(&mut solver, is_line);
let mut room_id = vec![vec![0; w]; h];

for i in 0..rooms.len() {
for &(y, x) in &rooms[i] {
room_id[y][x] = i;
}
}

let mut room_entrance = vec![vec![]; rooms.len()];
for y in 0..h {
for x in 0..w {
if y < h - 1 && room_id[y][x] != room_id[y + 1][x] {
room_entrance[room_id[y][x]].push(is_line.vertical.at((y, x)));
room_entrance[room_id[y + 1][x]].push(is_line.vertical.at((y, x)));
}
if x < w - 1 && room_id[y][x] != room_id[y][x + 1] {
room_entrance[room_id[y][x]].push(is_line.horizontal.at((y, x)));
room_entrance[room_id[y][x + 1]].push(is_line.horizontal.at((y, x)));
}
}
}

for i in 0..rooms.len() {
// Check every room is entered twice
solver.add_expr(count_true(&room_entrance[i]).eq(4));
}

for i in 0..rooms.len() {
let mut cells = vec![];
for &pt in &rooms[i] {
cells.push(is_passed.at(pt));
}
}
Comment thread
ReverM marked this conversation as resolved.

solver.add_expr(is_passed);

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

type Problem = graph::InnerGridEdges<Vec<Vec<bool>>>;

fn combinator() -> impl Combinator<Problem> {
Size::new(Rooms)
}

pub fn serialize_problem(problem: &Problem) -> Option<String> {
let height = problem.vertical.len();
let width = problem.vertical[0].len() + 1;
problem_to_url_with_context(
combinator(),
"doubleback",
problem.clone(),
&Context::sized(height, width),
)
}

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

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

fn problem_for_tests() -> Problem {
let borders = graph::InnerGridEdges {
horizontal: crate::util::tests::to_bool_2d([[0, 0, 1, 1], [0, 0, 0, 0], [1, 1, 1, 1]]),
vertical: crate::util::tests::to_bool_2d([[0, 0, 0], [0, 1, 0], [0, 1, 0], [0, 0, 0]]),
};
borders
}

#[test]
fn test_doubleback_problem() {
let borders = problem_for_tests();
let ans = solve_doubleback(&borders);
assert!(ans.is_some());
let ans = ans.unwrap();

#[rustfmt::skip]
let expected = graph::BoolGridEdgesIrrefutableFacts {
horizontal: crate::util::tests::to_option_bool_2d([
[1, 1, 1],
[0, 1, 0],
[0, 0, 0],
[1, 0, 1],
]),
vertical: crate::util::tests::to_option_bool_2d([
[1, 0, 0, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
]),
};
assert_eq!(ans, expected);
}
#[test]
fn test_doubleback_serializer() {
let problem = problem_for_tests();
let url = "https://puzz.link/p?doubleback/4/4/14063o"; // puzz.link example puzzle
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 @@ -35,6 +35,7 @@ pub mod dbchoco;
pub mod dominion;
pub mod doppelblock;
pub mod double_lits;
pub mod doubleback;
pub mod energywalk;
pub mod evolmino;
pub mod exercise;
Expand Down
75 changes: 75 additions & 0 deletions cspuz_solver_backend/src/puzzle/doubleback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use crate::board::{Board, BoardKind};
use crate::uniqueness::check_uniqueness;
use cspuz_rs_puzzles::puzzles::doubleback;

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

let height = borders.vertical.len();
let width = borders.vertical[0].len() + 1;
let mut board = Board::new(BoardKind::Grid, height, width, check_uniqueness(&ans));
board.add_borders(&borders, "black");

if let Some(is_line) = &ans {
board.add_lines_irrefutable_facts(is_line, "green", None);
}

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?doubleback/4/4/14063o"),
Board {
kind: BoardKind::Grid,
height: 4,
width: 4,
data: vec![
Item { y: 2, x: 5, color: "black", kind: ItemKind::BoldWall },
Item { y: 2, x: 7, color: "black", kind: ItemKind::BoldWall },
Item { y: 3, x: 4, color: "black", kind: ItemKind::BoldWall },
Item { y: 6, x: 1, color: "black", kind: ItemKind::BoldWall },
Item { y: 6, x: 3, color: "black", kind: ItemKind::BoldWall },
Item { y: 5, x: 4, color: "black", kind: ItemKind::BoldWall },
Item { y: 6, x: 5, color: "black", kind: ItemKind::BoldWall },
Item { y: 6, x: 7, color: "black", kind: ItemKind::BoldWall },
Item { y: 2, x: 1, color: "green", kind: ItemKind::Line },
Item { y: 2, x: 3, color: "green", kind: ItemKind::Cross },
Item { y: 2, x: 5, color: "green", kind: ItemKind::Cross },
Item { y: 2, x: 7, color: "green", kind: ItemKind::Line },
Item { y: 4, x: 1, color: "green", kind: ItemKind::Line },
Item { y: 4, x: 3, color: "green", kind: ItemKind::Line },
Item { y: 4, x: 5, color: "green", kind: ItemKind::Line },
Item { y: 4, x: 7, color: "green", kind: ItemKind::Line },
Item { y: 6, x: 1, color: "green", kind: ItemKind::Line },
Item { y: 6, x: 3, color: "green", kind: ItemKind::Line },
Item { y: 6, x: 5, color: "green", kind: ItemKind::Line },
Item { y: 6, x: 7, color: "green", kind: ItemKind::Line },
Item { y: 1, x: 2, color: "green", kind: ItemKind::Line },
Item { y: 1, x: 4, color: "green", kind: ItemKind::Line },
Item { y: 1, x: 6, color: "green", kind: ItemKind::Line },
Item { y: 3, x: 2, color: "green", kind: ItemKind::Cross },
Item { y: 3, x: 4, color: "green", kind: ItemKind::Line },
Item { y: 3, x: 6, color: "green", kind: ItemKind::Cross },
Item { y: 5, x: 2, color: "green", kind: ItemKind::Cross },
Item { y: 5, x: 4, color: "green", kind: ItemKind::Cross },
Item { y: 5, x: 6, color: "green", kind: ItemKind::Cross },
Item { y: 7, x: 2, color: "green", kind: ItemKind::Line },
Item { y: 7, x: 4, color: "green", kind: ItemKind::Cross },
Item { y: 7, x: 6, color: "green", kind: ItemKind::Line },
],
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 @@ -117,6 +117,7 @@ puzzle_list!(puzz_link,
(dbchoco, ["dbchoco"], "Double Choco", "ダブルチョコ"),
(dominion, ["dominion"], "Dominion", "ドミニオン"),
(doppelblock, ["doppelblock"], "Doppelblock", "ビトゥイーン・サム"),
(doubleback, ["doubleback"], "Double Back", "Double Back"),
(energywalk, ["energywalk"], "Energy Walk", "Energy Walk"),
(evolmino, ["evolmino"], "Evolmino", "シンカミノ"),
(fillomino, ["fillomino"], "Fillomino", "フィルオミノ"),
Expand Down
Loading