-
Notifications
You must be signed in to change notification settings - Fork 9
Added solver for Army Ants #202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
20f2ceb
Added generic movement puzzle handling constraints
ReverM 96292d8
Added argument for puzzles with straight constraints
ReverM d7688b9
Added solver for armyants
ReverM 7b7d831
Added and finalized solver for army ants
ReverM 374c4cf
Fixed issue with empty spaces
ReverM c379d9b
Fixed issue with self touching army
ReverM c8c6d66
Fixed issue with question marks
ReverM 3bdfa86
Simplified constraints
ReverM 16a2a07
Adressed comments
ReverM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| use crate::puzzles::move_common::add_movement_constraints; | ||
| use cspuz_rs::graph; | ||
| use cspuz_rs::serializer::{ | ||
| problem_to_url_with_context, url_to_problem, Choice, Combinator, Context, ContextBasedGrid, | ||
| Dict, HexInt, Optionalize, Rooms, Size, Spaces, Tuple2, | ||
| }; | ||
| use cspuz_rs::solver::Solver; | ||
|
|
||
| pub fn solve_armyants( | ||
| borders: &graph::InnerGridEdges<Vec<Vec<bool>>>, | ||
| clues: &[Vec<Option<i32>>], | ||
| ) -> Option<(graph::BoolGridEdgesIrrefutableFacts, Vec<Vec<Option<i32>>>)> { | ||
| let (h, w) = borders.base_shape(); | ||
| let mut solver = Solver::new(); | ||
|
|
||
| let mut clue_max = 0; | ||
| let mut num_qmark = 0; | ||
| for y in 0..h { | ||
| for x in 0..w { | ||
| if let Some(n) = clues[y][x] { | ||
| clue_max = clue_max.max(n); | ||
| if n < 0 { | ||
| num_qmark += 1; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let end_state = &solver.int_var_2d((h, w), -1, clue_max + num_qmark); | ||
| solver.add_answer_key_int(end_state); | ||
| let movement = &graph::BoolGridEdges::new(&mut solver, (h - 1, w - 1)); | ||
| solver.add_answer_key_bool(&movement.horizontal); | ||
| solver.add_answer_key_bool(&movement.vertical); | ||
|
|
||
| for y in 0..h { | ||
| for x in 0..(w - 1) { | ||
| if borders.vertical[y][x] { | ||
| solver.add_expr(!movement.horizontal.at((y, x))); | ||
| } | ||
| } | ||
| } | ||
| for y in 0..(h - 1) { | ||
| for x in 0..w { | ||
| if borders.horizontal[y][x] { | ||
| solver.add_expr(!movement.vertical.at((y, x))); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| add_movement_constraints( | ||
| &mut solver, | ||
| clue_max + num_qmark, | ||
| movement, | ||
| clues, | ||
| end_state, | ||
| h, | ||
| w, | ||
| false, | ||
| ); | ||
|
|
||
| for y in 0..h { | ||
| for x in 0..w { | ||
| let connected = &solver.bool_var_2d((h, w)); | ||
| solver.add_expr( | ||
| end_state | ||
| .four_neighbors((y, x)) | ||
| .ge(end_state.at((y, x)) + 1) | ||
| .count_true() | ||
| .eq(0) | ||
| .imp(connected.imp(end_state.ge(1))), | ||
| ); | ||
| solver.add_expr( | ||
| ((end_state.at((y, x)).ge(1)) | ||
| & end_state | ||
| .four_neighbors((y, x)) | ||
| .ge(end_state.at((y, x)) + 1) | ||
| .count_true() | ||
| .eq(0)) | ||
| .imp(connected.count_true().eq(end_state.at((y, x)))), | ||
| ); | ||
| graph::active_vertices_connected_2d(&mut solver, connected); | ||
|
|
||
| for nb in connected.four_neighbor_indices((y, x)) { | ||
| solver.add_expr( | ||
| end_state | ||
| .four_neighbors((y, x)) | ||
| .ge(end_state.at((y, x)) + 1) | ||
| .count_true() | ||
| .eq(0) | ||
| .imp(end_state.ge(1).at(nb).imp(connected.at(nb))), | ||
| ); | ||
| } | ||
| solver.add_expr( | ||
| end_state | ||
| .four_neighbors((y, x)) | ||
| .ge(end_state.at((y, x)) + 1) | ||
| .count_true() | ||
| .eq(0) | ||
| .imp( | ||
| (end_state.ge(1).slice((1.., ..)) & end_state.ge(1).slice((..(h - 1), ..))) | ||
| .imp( | ||
| connected | ||
| .slice((1.., ..)) | ||
| .iff(connected.slice((..(h - 1), ..))), | ||
| ), | ||
| ), | ||
| ); | ||
| solver.add_expr( | ||
| end_state | ||
| .four_neighbors((y, x)) | ||
| .ge(end_state.at((y, x)) + 1) | ||
| .count_true() | ||
| .eq(0) | ||
| .imp( | ||
| (end_state.ge(1).slice((.., 1..)) & end_state.ge(1).slice((.., ..(w - 1)))) | ||
| .imp( | ||
| connected | ||
| .slice((.., 1..)) | ||
| .iff(connected.slice((.., ..(w - 1)))), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| solver.add_expr( | ||
| end_state.at((y, x)).ge(2).imp( | ||
| end_state | ||
| .four_neighbors((y, x)) | ||
| .eq(end_state.at((y, x)) - 1) | ||
| .count_true() | ||
| .eq(1), | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| solver.add_expr(end_state.ne(0)); | ||
|
|
||
| solver | ||
| .irrefutable_facts() | ||
| .map(|f| (f.get(movement), f.get(end_state))) | ||
| } | ||
|
|
||
| pub type Problem = (graph::InnerGridEdges<Vec<Vec<bool>>>, Vec<Vec<Option<i32>>>); | ||
|
|
||
| fn combinator() -> impl Combinator<Problem> { | ||
| Size::new(Tuple2::new( | ||
| Rooms, | ||
| ContextBasedGrid::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> { | ||
| let height = problem.0.vertical.len(); | ||
| let width = problem.0.vertical[0].len() + 1; | ||
| problem_to_url_with_context( | ||
| combinator(), | ||
| "armyants", | ||
| problem.clone(), | ||
| &Context::sized(height, width), | ||
| ) | ||
| } | ||
|
|
||
| pub fn deserialize_problem(url: &str) -> Option<Problem> { | ||
| url_to_problem(combinator(), &["armyants"], url) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| fn problem_for_tests() -> Problem { | ||
| let borders = graph::InnerGridEdges { | ||
| horizontal: crate::util::tests::to_bool_2d([[0, 1, 1, 0], [1, 1, 1, 1], [0, 1, 1, 0]]), | ||
| vertical: crate::util::tests::to_bool_2d([[1, 0, 1], [1, 0, 0], [0, 0, 1], [1, 0, 1]]), | ||
| }; | ||
|
|
||
| let clues = vec![ | ||
| vec![None, None, Some(1), None], | ||
| vec![Some(2), None, None, Some(4)], | ||
| vec![Some(3), None, None, Some(1)], | ||
| vec![None, Some(2), None, None], | ||
| ]; | ||
|
|
||
| (borders, clues) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_armyants_problem() { | ||
| let (borders, clues) = problem_for_tests(); | ||
| let ans = solve_armyants(&borders, &clues); | ||
| assert!(ans.is_some()); | ||
| let (movement, final_state) = ans.unwrap(); | ||
|
|
||
| let expected_nums = crate::util::tests::to_option_2d([ | ||
| [2, 1, -1, -1], | ||
| [-1, -1, 4, -1], | ||
| [-1, -1, 3, -1], | ||
| [-1, -1, 2, 1], | ||
| ]); | ||
|
|
||
| let expected_paths = graph::BoolGridEdgesIrrefutableFacts { | ||
| horizontal: crate::util::tests::to_option_bool_2d([ | ||
| [0, 1, 0], | ||
| [0, 0, 1], | ||
| [1, 1, 0], | ||
| [0, 1, 0], | ||
| ]), | ||
| vertical: crate::util::tests::to_option_bool_2d([ | ||
| [1, 0, 0, 0], | ||
| [0, 0, 0, 0], | ||
| [0, 0, 0, 1], | ||
| ]), | ||
| }; | ||
| assert_eq!(movement, expected_paths); | ||
| assert_eq!(final_state, expected_nums); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_armyants_serializer() { | ||
| let problem = problem_for_tests(); | ||
| let url = "https://puzz.link/p?armyants/4/4/m38dtgh1g2h43h1g2h"; // Example puzzle on puzz.link | ||
| crate::util::tests::serializer_test(problem, url, serialize_problem, deserialize_problem); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.