-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcocktail.rs
More file actions
129 lines (121 loc) · 6.77 KB
/
Copy pathcocktail.rs
File metadata and controls
129 lines (121 loc) · 6.77 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
118
119
120
121
122
123
124
125
126
127
128
129
use crate::board::{Board, BoardKind, Item, ItemKind};
use crate::uniqueness::{is_unique, Uniqueness};
use cspuz_rs::graph;
use cspuz_rs_puzzles::puzzles::cocktail;
pub fn solve(url: &str) -> Result<Board, &'static str> {
let (borders, clues) = cocktail::deserialize_problem(url).ok_or("invalid url")?;
let is_black = cocktail::solve_cocktail(&borders, &clues);
let height = borders.vertical.len();
let width = borders.vertical[0].len() + 1;
let mut board = Board::new(
BoardKind::Grid,
height,
width,
is_black
.as_ref()
.map(is_unique)
.unwrap_or(Uniqueness::NoAnswer),
);
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];
if n > 0 {
board.push(Item::cell(y, x, "black", ItemKind::Num(n)));
} else {
board.push(Item::cell(y, x, "black", ItemKind::Text("?")));
}
}
}
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?cocktail/6/6/4iihh4u03o0u34233"),
Board {
kind: BoardKind::Grid,
height: 6,
width: 6,
data: vec![
Item { y: 2, x: 1, color: "black", kind: ItemKind::BoldWall },
Item { y: 2, x: 3, color: "black", kind: ItemKind::BoldWall },
Item { y: 2, x: 5, color: "black", kind: ItemKind::BoldWall },
Item { y: 1, x: 6, color: "black", kind: ItemKind::BoldWall },
Item { y: 2, x: 7, color: "black", kind: ItemKind::BoldWall },
Item { y: 3, x: 2, color: "black", kind: ItemKind::BoldWall },
Item { y: 3, x: 8, color: "black", kind: ItemKind::BoldWall },
Item { y: 5, x: 2, 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: 6, x: 7, color: "black", kind: ItemKind::BoldWall },
Item { y: 5, x: 8, color: "black", kind: ItemKind::BoldWall },
Item { y: 6, x: 9, color: "black", kind: ItemKind::BoldWall },
Item { y: 7, x: 2, color: "black", kind: ItemKind::BoldWall },
Item { y: 7, x: 10, color: "black", kind: ItemKind::BoldWall },
Item { y: 9, x: 2, color: "black", kind: ItemKind::BoldWall },
Item { y: 10, x: 3, color: "black", kind: ItemKind::BoldWall },
Item { y: 10, x: 5, color: "black", kind: ItemKind::BoldWall },
Item { y: 10, x: 7, color: "black", kind: ItemKind::BoldWall },
Item { y: 10, x: 9, color: "black", kind: ItemKind::BoldWall },
Item { y: 9, x: 10, color: "black", kind: ItemKind::BoldWall },
Item { y: 11, x: 6, color: "black", kind: ItemKind::BoldWall },
Item { y: 1, x: 1, color: "green", kind: ItemKind::Block },
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::Dot },
Item { y: 1, x: 9, color: "green", kind: ItemKind::Block },
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::Dot },
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::Block },
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::Block },
Item { y: 5, x: 9, color: "green", kind: ItemKind::Dot },
Item { y: 5, x: 11, color: "green", kind: ItemKind::Block },
Item { y: 7, x: 1, color: "green", kind: ItemKind::Dot },
Item { y: 7, x: 3, color: "green", kind: ItemKind::Dot },
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::Block },
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::Block },
Item { y: 9, x: 11, color: "green", kind: ItemKind::Dot },
Item { y: 11, x: 1, color: "green", kind: ItemKind::Dot },
Item { y: 11, x: 3, color: "green", kind: ItemKind::Block },
Item { y: 11, x: 5, color: "green", kind: ItemKind::Block },
Item { y: 11, x: 7, color: "green", kind: ItemKind::Dot },
Item { y: 11, x: 9, color: "green", kind: ItemKind::Dot },
Item { y: 11, x: 11, color: "green", kind: ItemKind::Dot },
Item { y: 1, x: 1, color: "black", kind: ItemKind::Num(3) },
Item { y: 1, x: 7, color: "black", kind: ItemKind::Num(4) },
Item { y: 3, x: 1, color: "black", kind: ItemKind::Num(2) },
Item { y: 3, x: 3, color: "black", kind: ItemKind::Num(3) },
Item { y: 7, x: 3, color: "black", kind: ItemKind::Num(3) },
],
uniqueness: Uniqueness::Unique,
},
);
}
}