main.rs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. use serde_derive::{Deserialize, Serialize};
  2. use serde_xml_rs::{from_reader, from_str, to_string};
  3. use clap::Parser;
  4. use std::fmt;
  5. use std::fs::File;
  6. use std::collections::HashSet;
  7. #[derive(Debug, Serialize, Deserialize, PartialEq)]
  8. struct Graph {
  9. order: i32,
  10. #[serde(rename = "type")]
  11. game_type: String,
  12. #[serde(rename = "specific-type")]
  13. specific_type: String,
  14. }
  15. #[derive(Debug, Serialize, Deserialize, PartialEq)]
  16. struct Puzzle {
  17. graph: Graph,
  18. values: String,
  19. solution: String,
  20. }
  21. #[derive(Debug, Serialize, Deserialize, PartialEq)]
  22. struct Game {
  23. #[serde(rename = "had-help")]
  24. help: i16,
  25. #[serde(rename = "msecs-elapsed")]
  26. elapsed: u32,
  27. puzzle: Puzzle,
  28. }
  29. #[derive(Debug, Serialize, Deserialize, PartialEq)]
  30. struct Ksudoku {
  31. game: Game,
  32. }
  33. const SIZE: u8 = 9;
  34. const MAX_SIZE: u8 = 81;
  35. #[derive(Debug)]
  36. struct Sudoku {
  37. board: [u8; MAX_SIZE as usize],
  38. possible: [HashSet<u8>; MAX_SIZE as usize],
  39. }
  40. // Translate x,y to position in board.
  41. const fn pos(x: u8, y: u8) -> u8 {
  42. x + (y * SIZE as u8)
  43. }
  44. // Translate post to x,y in board.
  45. const fn xy(pos: u8) -> (u8, u8) {
  46. ((pos % SIZE), (pos / SIZE))
  47. }
  48. /*
  49. (0 .. 10)
  50. .map(|_| HashSet::<usize>::new())
  51. .collect();
  52. let arr: [Vec<u32>; 10] = [(); 10].map(|_| Vec::with_capacity(100));
  53. */
  54. impl Sudoku {
  55. fn new() -> Self {
  56. // let b : HashSet<u8> = HashSet::from_iter(1..=9);
  57. let s = Sudoku {
  58. board: [0; MAX_SIZE as usize],
  59. possible: [(); MAX_SIZE as usize].map(|_| HashSet::from_iter(1..=9)),
  60. // possible: [HashSet::from_iter(1..=9); MAX_SIZE as usize],
  61. // possible: [[0; SIZE as usize]; MAX_SIZE as usize],
  62. // possible: [(0..MAX_SIZE).map( |_| (1..=9).collect())],
  63. // possible: [(1..=9).map(|_| HashSet::new()).collect(); MAX_SIZE as usize],
  64. };
  65. s
  66. }
  67. fn clear(&mut self) {
  68. for x in 0..MAX_SIZE {
  69. self.board[x as usize] = 0;
  70. self.possible = [(); MAX_SIZE as usize].map(|_| HashSet::from_iter(1..=9));
  71. /*
  72. self.possible[x as usize].clear();
  73. for i in 1..=9 {
  74. self.possible[x as usize].insert(i);
  75. }
  76. */
  77. // self.possible[x as usize] = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  78. }
  79. }
  80. fn load_xsudoku(&mut self, s: &str) {
  81. self.clear();
  82. let mut x: u8 = 0;
  83. let mut y: u8 = 0;
  84. for ch in s.chars() {
  85. if ch >= 'b' {
  86. self.set(x, y, ch as u8 - 'a' as u8);
  87. // self.board[pos(x, y) as usize] = ch as u8 - 'a' as u8;
  88. };
  89. y += 1;
  90. if y >= SIZE {
  91. y = 0;
  92. x += 1;
  93. }
  94. }
  95. }
  96. fn set(&mut self, x: u8, y: u8, value: u8) {
  97. self.board[pos(x, y) as usize] = value;
  98. // Ok, update the possible
  99. let mut g = Group::new();
  100. g.for_row(x, y);
  101. for g in g.items {
  102. // remove value from these sets.
  103. self.possible[g as usize].take(&value);
  104. }
  105. g.for_column(x, y);
  106. for g in g.items {
  107. // remove value from these sets.
  108. self.possible[g as usize].take(&value);
  109. }
  110. g.for_block(x, y);
  111. for g in g.items {
  112. // remove value from these sets.
  113. self.possible[g as usize].take(&value);
  114. }
  115. self.possible[pos(x, y) as usize].clear();
  116. }
  117. fn display(&self) {
  118. println!("╔═══╦═══╦═══╗");
  119. for y in 0..SIZE {
  120. print!("║");
  121. for x in 0..SIZE {
  122. let item = self.board[pos(x as u8, y as u8) as usize];
  123. if item == 0 {
  124. print!(" ");
  125. } else if (item >= 1) && (item <= 9) {
  126. print!("{}", item);
  127. }
  128. if x % 3 == 2 {
  129. print!("║");
  130. }
  131. }
  132. println!("");
  133. if y % 3 == 2 {
  134. if y + 1 == SIZE {
  135. println!("╚═══╩═══╩═══╝");
  136. } else {
  137. println!("╠═══╬═══╬═══╣");
  138. }
  139. }
  140. }
  141. }
  142. fn display_possible(&self) {
  143. for y in 0..SIZE {
  144. for x in 0..SIZE {
  145. let mut possible = String::new();
  146. for p in self.possible[pos(x, y) as usize].iter() {
  147. possible += format!("{}", p).as_str();
  148. }
  149. // for i in 0..SIZE {
  150. // let &pos = self.possible[pos(x, y) as usize];
  151. print!("({},{}):", x, y);
  152. // print!("{:20}", format!("{:?}", self.possible[pos(x, y) as usize]));
  153. print!("{:9}", possible);
  154. /*
  155. if pos == 0 {
  156. print!(" ");
  157. } else {
  158. print!("{}", pos);
  159. }
  160. */
  161. // }
  162. // print!(" ");
  163. }
  164. println!("");
  165. }
  166. }
  167. fn solve(&mut self) -> bool {
  168. // Pass 1: Look for singles in the possible sets.
  169. let mut found_something = false;
  170. for i in 0..MAX_SIZE {
  171. if self.possible[i as usize].len() == 1 {
  172. // Get the value
  173. let value = self.possible[i as usize].iter().next().cloned().unwrap();
  174. // Found one!
  175. println!("Set1 {:?} to {}", xy(i), value);
  176. self.set(xy(i).0, xy(i).1, value);
  177. found_something = true;
  178. }
  179. }
  180. let mut g = Group::new();
  181. let mut values: HashSet<u8> = HashSet::new();
  182. let mut group_process = |this: &mut Self, grp: &Group| {
  183. // Collect all the possible values within the group.
  184. values.clear();
  185. for gidx in 0..SIZE {
  186. // println!("possible: {:?}", this.possible[grp.items[gidx as usize] as usize]);
  187. values.extend(&this.possible[grp.items[gidx as usize] as usize]);
  188. // println!("now : {:?}", this.possible[grp.items[gidx as usize] as usize]);
  189. }
  190. // println!("values {:?}", values);
  191. // Now, check for singles.
  192. for v in values.iter() {
  193. let mut count = 0;
  194. let mut pos = 0;
  195. for gidx in 0..SIZE {
  196. if this.possible[grp.items[gidx as usize] as usize].contains(&v) {
  197. count += 1;
  198. pos = grp.items[gidx as usize];
  199. if count > 1 {
  200. break;
  201. }
  202. }
  203. }
  204. if count == 1 {
  205. // don't need this, it was v!
  206. // let value = this.possible[pos as usize].iter().next().cloned().unwrap();
  207. println!("Set2 {:?} to {}", xy(pos), v);
  208. this.set(xy(pos).0, xy(pos).1, *v);
  209. found_something = true;
  210. }
  211. }
  212. };
  213. // Change to 0..SIZE ... Keep it simple.
  214. for i in 0..SIZE {
  215. g.for_column(i, 1);
  216. group_process(self, &g);
  217. g.for_row(1, i);
  218. group_process(self, &g);
  219. g.for_iter(i);
  220. group_process(self, &g);
  221. }
  222. println!("Looking for pairs...");
  223. // PAIR processing.
  224. for i in 0..SIZE {
  225. g.for_iter(i);
  226. for gidx in 0..SIZE - 1 {
  227. let gpos = g.items[gidx as usize];
  228. if self.possible[gpos as usize].len() == 2 {
  229. // Found a pair
  230. for fidx in gidx + 1..SIZE {
  231. let fpos = g.items[fidx as usize];
  232. if self.possible[fpos as usize].len() == 2 {
  233. // Ok, there's another pair
  234. if self.possible[gpos as usize].is_subset(&self.possible[fpos as usize])
  235. {
  236. // Ok, they have the same values!
  237. println!(
  238. "Pair found! {} {}: {} {:?} and {} {:?}",
  239. gidx,
  240. fidx,
  241. gpos,
  242. xy(gpos),
  243. fpos,
  244. xy(fpos)
  245. );
  246. // Ok, remove the items in the pair from the cell.
  247. // Don't touch the gpos/fpos records. Keep those!
  248. let mut values: [u8; 2] = [0,0];
  249. let mut vpos = 0;
  250. for z in self.possible[gpos as usize].iter() {
  251. values[vpos] = *z;
  252. vpos +=1;
  253. }
  254. for remove in 0..SIZE {
  255. if (gidx == remove) || (fidx == remove) {
  256. continue;
  257. }
  258. // Ok, these aren't the ones to save, so:
  259. let rpos = g.items[remove as usize];
  260. if self.possible[rpos as usize].take(&values[0]).is_some() {
  261. found_something = true;
  262. };
  263. if self.possible[rpos as usize].take(&values[1]).is_some() {
  264. found_something = true;
  265. };
  266. }
  267. // Check the x's and y's to see if we can also process a row/column too.
  268. if xy(gpos).0 == xy(fpos).0 {
  269. // Matching X - process column
  270. let column = xy(gpos).0;
  271. vpos = 0;
  272. for z in self.possible[gpos as usize].iter() {
  273. values[vpos] = *z;
  274. vpos +=1;
  275. }
  276. for remove in 0..SIZE {
  277. if (remove == xy(gpos).1) || (remove == xy(fpos).1) {
  278. continue;
  279. }
  280. if self.possible[pos(column, remove) as usize].take(&values[0]).is_some() {
  281. found_something = true;
  282. };
  283. if self.possible[pos(column, remove) as usize].take(&values[1]).is_some() {
  284. found_something = true;
  285. };
  286. }
  287. }
  288. if xy(gpos).1 == xy(fpos).1 {
  289. // Matching Y - process row
  290. let row = xy(gpos).1;
  291. vpos = 0;
  292. for z in self.possible[gpos as usize].iter() {
  293. values[vpos] = *z;
  294. vpos +=1;
  295. }
  296. for remove in 0..SIZE {
  297. if (remove == xy(gpos).0) || (remove == xy(fpos).0) {
  298. continue;
  299. }
  300. if self.possible[pos(remove, row) as usize].take(&values[0]).is_some() {
  301. found_something = true;
  302. };
  303. if self.possible[pos(remove, row) as usize].take(&values[1]).is_some() {
  304. found_something = true;
  305. };
  306. }
  307. }
  308. }
  309. }
  310. }
  311. }
  312. }
  313. }
  314. found_something
  315. }
  316. }
  317. // #[derive(Debug)]
  318. struct Group {
  319. items: [u8; SIZE as usize],
  320. }
  321. impl fmt::Debug for Group {
  322. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  323. if f.alternate() {
  324. write!(f, "Group {{ {}[{},{}], {}[{},{}], {}[{},{}], {}[{},{}], {}[{},{}], {}[{},{}], {}[{},{}], {}[{},{}], {}[{},{}] }}",
  325. self.items[0], xy(self.items[0]).0, xy(self.items[0]).1,
  326. self.items[1], xy(self.items[1]).0, xy(self.items[1]).1,
  327. self.items[2], xy(self.items[2]).0, xy(self.items[2]).1,
  328. self.items[3], xy(self.items[3]).0, xy(self.items[3]).1,
  329. self.items[4], xy(self.items[4]).0, xy(self.items[4]).1,
  330. self.items[5], xy(self.items[5]).0, xy(self.items[5]).1,
  331. self.items[6], xy(self.items[6]).0, xy(self.items[6]).1,
  332. self.items[7], xy(self.items[7]).0, xy(self.items[7]).1,
  333. self.items[8], xy(self.items[8]).0, xy(self.items[8]).1,
  334. )
  335. } else {
  336. f.debug_struct("Group").field("items", &self.items).finish()
  337. }
  338. }
  339. }
  340. impl Group {
  341. fn new() -> Self {
  342. Group {
  343. items: [0; SIZE as usize],
  344. }
  345. }
  346. fn for_column(&mut self, x: u8, _y: u8) {
  347. for y in 0..SIZE {
  348. self.items[y as usize] = pos(x, y);
  349. }
  350. }
  351. fn for_row(&mut self, _x: u8, y: u8) {
  352. for x in 0..SIZE {
  353. self.items[x as usize] = pos(x, y);
  354. }
  355. }
  356. fn for_block(&mut self, x: u8, y: u8) {
  357. // Find starting block positions
  358. let sb_x = x - (x % 3);
  359. let sb_y = y - (y % 3);
  360. for i in 0..SIZE {
  361. let ix = i % 3;
  362. let iy = i / 3;
  363. // println!("i = {}, sb.x = {} sb.y = {}, ix = {} iy = {}", i, sb_x, sb_y, ix, iy);
  364. self.items[i as usize] = pos(sb_x + ix, sb_y + iy);
  365. }
  366. }
  367. fn for_iter(&mut self, i: u8) {
  368. let sb_x = (i % 3) * 3;
  369. let sb_y = (i / 3) * 3;
  370. self.for_block(sb_x, sb_y);
  371. }
  372. fn display(&self) {
  373. for i in 0..SIZE {
  374. let v = self.items[i as usize];
  375. print!("{} [{},{}] ", v, xy(v).0, xy(v).1);
  376. }
  377. println!("");
  378. }
  379. }
  380. // Command line arguments - via clap
  381. #[derive(Parser)]
  382. struct Cli {
  383. /// The path to the file to read
  384. filename: std::path::PathBuf,
  385. }
  386. fn main() {
  387. let args = Cli::parse();
  388. let filename = args.filename; // "../puzzle1";
  389. let fh = File::open(filename).unwrap();
  390. let puzzle: Ksudoku = from_reader(fh).unwrap();
  391. println!("Puzzle: {:?}", puzzle);
  392. let mut s = Sudoku::new();
  393. s.load_xsudoku(&puzzle.game.puzzle.values);
  394. s.display();
  395. s.display_possible();
  396. while s.solve() {
  397. println!("Try it again...");
  398. s.display();
  399. s.display_possible();
  400. }
  401. /*
  402. s.display();
  403. s.display_possible();
  404. */
  405. /*
  406. let mut g = Group::new();
  407. g.for_row(1, 1);
  408. print!("Row: ");
  409. println!("{:?}", g);
  410. println!("{:#?}", g);
  411. // g.display();
  412. print!("Col: ");
  413. g.for_column(1, 1);
  414. println!("{:?}", g);
  415. // g.display();
  416. println!("Blk testing output: ");
  417. for i in 0..SIZE {
  418. g.for_block(i, i);
  419. println!("({},{} {:#?})", i, i, g);
  420. // g.display();
  421. }
  422. */
  423. }