-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathakichiwake.rs
More file actions
117 lines (110 loc) · 5.88 KB
/
Copy pathakichiwake.rs
File metadata and controls
117 lines (110 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use crate::board::{Board, BoardKind, Item, ItemKind};
use crate::uniqueness::{is_unique, Uniqueness};
use cspuz_rs::graph;
use cspuz_rs_puzzles::puzzles::akichiwake;
pub fn solve(url: &str) -> Result<Board, &'static str> {
let (borders, clues) = akichiwake::deserialize_problem(url).ok_or("invalid url")?;
let is_black = akichiwake::solve_akichiwake(&borders, &clues);
// Get dimensions from borders: vertical has height rows, horizontal[0] has width elements
let height = borders.vertical.len();
let width = if height > 0 {
borders.horizontal[0].len()
} else {
0
};
let mut board = Board::new(
BoardKind::Grid,
height,
width,
is_black
.as_ref()
.map_or(Uniqueness::NoAnswer, |b| is_unique(b)),
);
board.add_borders(&borders, "black");
if let Some(is_black) = &is_black {
board.add_block_dot_answer(is_black, "green");
}
let rooms = graph::borders_to_rooms(&borders);
assert_eq!(rooms.len(), clues.len());
for i in 0..rooms.len() {
if let Some(n) = clues[i] {
let (y, x) = rooms[i][0];
board.push(Item::cell(y, x, "black", ItemKind::Num(n)));
}
}
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?akichi/6/5/455993g7o03213g5"),
Board {
kind: BoardKind::Grid,
height: 5,
width: 6,
data: vec![
Item { y: 1, x: 6, color: "black", kind: ItemKind::BoldWall },
Item { y: 2, x: 7, color: "black", kind: ItemKind::BoldWall },
Item { y: 2, x: 9, color: "black", kind: ItemKind::BoldWall },
Item { y: 2, x: 11, color: "black", kind: ItemKind::BoldWall },
Item { y: 3, x: 6, color: "black", kind: ItemKind::BoldWall },
Item { y: 3, x: 10, 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: 6, x: 5, color: "black", kind: ItemKind::BoldWall },
Item { y: 5, x: 6, color: "black", kind: ItemKind::BoldWall },
Item { y: 6, x: 7, color: "black", kind: ItemKind::BoldWall },
Item { y: 6, x: 9, color: "black", kind: ItemKind::BoldWall },
Item { y: 5, x: 10, color: "black", kind: ItemKind::BoldWall },
Item { y: 7, x: 4, color: "black", kind: ItemKind::BoldWall },
Item { y: 7, x: 10, color: "black", kind: ItemKind::BoldWall },
Item { y: 9, x: 4, color: "black", kind: ItemKind::BoldWall },
Item { y: 9, x: 10, color: "black", kind: ItemKind::BoldWall },
Item { y: 1, x: 1, color: "green", kind: ItemKind::Block },
Item { y: 1, x: 3, color: "green", kind: ItemKind::Dot },
Item { y: 1, x: 5, color: "green", kind: ItemKind::Dot },
Item { y: 1, x: 7, color: "green", kind: ItemKind::Dot },
Item { y: 1, x: 9, color: "green", kind: ItemKind::Dot },
Item { y: 1, x: 11, color: "green", kind: ItemKind::Block },
Item { y: 3, x: 1, color: "green", kind: ItemKind::Dot },
Item { y: 3, x: 3, color: "green", kind: ItemKind::Block },
Item { y: 3, x: 5, color: "green", kind: ItemKind::Dot },
Item { y: 3, x: 7, color: "green", kind: ItemKind::Block },
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::Dot },
Item { y: 7, x: 7, color: "green", kind: ItemKind::Dot },
Item { y: 7, x: 9, color: "green", kind: ItemKind::Dot },
Item { y: 7, x: 11, color: "green", kind: ItemKind::Dot },
Item { y: 9, x: 1, color: "green", kind: ItemKind::Dot },
Item { y: 9, x: 3, color: "green", kind: ItemKind::Dot },
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: "green", kind: ItemKind::Dot },
Item { y: 9, x: 11, color: "green", kind: ItemKind::Block },
Item { y: 1, x: 1, color: "black", kind: ItemKind::Num(3) },
Item { y: 1, x: 7, color: "black", kind: ItemKind::Num(2) },
Item { y: 3, x: 7, color: "black", kind: ItemKind::Num(1) },
Item { y: 3, x: 11, color: "black", kind: ItemKind::Num(3) },
Item { y: 7, x: 5, color: "black", kind: ItemKind::Num(5) },
],
uniqueness: Uniqueness::Unique,
},
);
}
}