fork download
  1. type RowIndex = isize;
  2. type ColIndex = isize;
  3.  
  4. #[derive(Debug, Clone)]
  5. struct WordSearch {
  6. letters: FxHashMap<(RowIndex, ColIndex), char>,
  7. bounds: (RowIndex, ColIndex),
  8. }
  9.  
  10. impl WordSearch {
  11. fn from_str(input: &str) -> Self {
  12. let mut letters = FxHashMap::default();
  13. for (row, line) in input.lines().enumerate() {
  14. for (col, letter) in line.chars().enumerate() {
  15. letters.insert((row as RowIndex, col as ColIndex), letter);
  16. }
  17. }
  18. Self {
  19. letters,
  20. bounds: (
  21. input.lines().count() as isize,
  22. input.lines().next().unwrap().chars().count() as isize,
  23. ),
  24. }
  25. }
  26.  
  27. fn search_at(&self, row_ind: RowIndex, col_ind: ColIndex, word: &str) -> usize {
  28. let directions = vec![
  29. Direction::Up,
  30. Direction::Down,
  31. Direction::Left,
  32. Direction::Right,
  33. Direction::UpLeft,
  34. Direction::UpRight,
  35. Direction::DownLeft,
  36. Direction::DownRight,
  37. ];
  38.  
  39. directions
  40. .iter()
  41. .map(|dir| {
  42. dir.get_indices(row_ind, col_ind, word.len())
  43. .iter()
  44. .map(|&(row, col)| self.letters.get(&(row, col)))
  45. .collect::<Vec<Option<&char>>>()
  46. })
  47. .filter(|letters| {
  48. letters.iter().all(Option::is_some)
  49. && letters
  50. .into_iter()
  51. .flatten()
  52. .map(|c| **c)
  53. .collect::<String>()
  54. == word
  55. })
  56. .count()
  57. }
  58.  
  59. fn search(&self, word: &str) -> usize {
  60. (0..self.bounds.0)
  61. .flat_map(|row| (0..self.bounds.1).map(move |col| self.search_at(row, col, word)))
  62. .sum()
  63. }
  64.  
  65. fn convert_indices_to_vec(
  66. &self,
  67. indices: impl Iterator<Item = (i32, i32)>,
  68. ) -> Vec<Option<&char>> {
  69. indices
  70. .map(|(row, col)| self.letters.get(&(row as isize, col as isize)))
  71. .collect::<Vec<Option<&char>>>()
  72. }
  73.  
  74. fn cross_mas_search(&self) -> usize {
  75. let mut count = 0;
  76. for row in 0..self.bounds.0 {
  77. for col in 0..self.bounds.1 {
  78. if self.letters.get(&(row, col)) == Some(&'A') {
  79. let cross_letters = vec![
  80. self.letters.get(&(row - 1, col - 1)),
  81. self.letters.get(&(row - 1, col + 1)),
  82. self.letters.get(&(row + 1, col - 1)),
  83. self.letters.get(&(row + 1, col + 1)),
  84. ];
  85. if cross_letters
  86. .iter()
  87. .all(|&c| c.is_some() && (c.unwrap() == &'S' || c.unwrap() == &'M'))
  88. {
  89. let cross_word = cross_letters
  90. .iter()
  91. .map(|&c| c.unwrap())
  92. .collect::<String>();
  93. if cross_word == "SMMS" || cross_word == "MSSM" {
  94. } else {
  95. let counts = cross_word.chars().counts();
  96. if counts.get(&'S') == Some(&2) && counts.get(&'M') == Some(&2) {
  97. count += 1;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. count
  105. }
  106. }
  107.  
  108. fn convert_indices_to_vec(
  109. indices: impl Iterator<Item = (i32, i32)>,
  110. letters: &FxHashMap<(RowIndex, ColIndex), char>,
  111. ) -> Vec<Option<&char>> {
  112. indices
  113. .map(|(row, col)| letters.get(&(row as isize, col as isize)))
  114. .collect::<Vec<Option<&char>>>()
  115. }
  116.  
  117. fn check_letters(letters: Vec<Option<&char>>, word: &str) -> bool {
  118. letters.iter().all(Option::is_some) && letters.into_iter().flatten().collect::<String>() == word
  119. }
  120.  
  121. #[derive(Debug, Clone, Copy)]
  122. enum Direction {
  123. Up,
  124. UpLeft,
  125. UpRight,
  126. Down,
  127. DownLeft,
  128. DownRight,
  129. Left,
  130. Right,
  131. }
  132.  
  133. impl Direction {
  134. fn get_indices(&self, row: RowIndex, col: ColIndex, word_len: usize) -> Vec<(isize, isize)> {
  135. let word_len = word_len as isize;
  136. match self {
  137. Direction::Up => (0..word_len).map(|i| (row - i, col)).collect(),
  138. Direction::UpLeft => (0..word_len).map(|i| (row - i, col - i)).collect(),
  139. Direction::UpRight => (0..word_len).map(|i| (row - i, col + i)).collect(),
  140. Direction::Down => (0..word_len).map(|i| (row + i, col)).collect(),
  141. Direction::DownLeft => (0..word_len).map(|i| (row + i, col - i)).collect(),
  142. Direction::DownRight => (0..word_len).map(|i| (row + i, col + i)).collect(),
  143. Direction::Left => (0..word_len).map(|i| (row, col - i)).collect(),
  144. Direction::Right => (0..word_len).map(|i| (row, col + i)).collect(),
  145. }
  146. }
  147. }
  148.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
error[E0433]: failed to resolve: use of undeclared type or module `FxHashMap`
  --> prog.rs:12:27
   |
12 |         let mut letters = FxHashMap::default();
   |                           ^^^^^^^^^ use of undeclared type or module `FxHashMap`

error[E0412]: cannot find type `FxHashMap` in this scope
 --> prog.rs:6:14
  |
6 |     letters: FxHashMap<(RowIndex, ColIndex), char>,
  |              ^^^^^^^^^ not found in this scope

error[E0412]: cannot find type `FxHashMap` in this scope
   --> prog.rs:110:15
    |
110 |     letters: &FxHashMap<(RowIndex, ColIndex), char>,
    |               ^^^^^^^^^ not found in this scope

error[E0601]: `main` function not found in crate `prog`
  |
  = note: consider adding a `main` function to `prog.rs`

error: aborting due to 4 previous errors

Some errors occurred: E0412, E0433, E0601.
For more information about an error, try `rustc --explain E0412`.
stdout
Standard output is empty