Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. use std::fs::File;
  2. use std::io::{self, prelude::*, BufReader};
  3. use lazy_static::lazy_static;
  4. use regex::Regex;
  5.  
  6. fn main() -> io::Result<()>{
  7.  
  8. let mut board =vec![vec![0u8; 1000]; 1000];
  9.  
  10. //let file = File::open("input")?;
  11. let ref mut file = std::io::Cursor::new(b"
  12. #1 @ 1,3: 4x4
  13. #2 @ 3,1: 4x4
  14. #3 @ 5,5: 2x2".to_vec());
  15. let reader = BufReader::new(file);
  16. for line in reader.lines() {
  17. update_board_with_line(&mut board, &line?[..]);
  18. }
  19. // The two sigils in the closure are a code smell
  20. let overlaps = board.iter().flat_map(|a| a.iter()).filter(|&x| *x > 1).count();
  21. // But so are double-derefereces like this. Like, where did all these references come from!?
  22. let overlaps = board.iter().flat_map(|a| a.iter()).filter(|x| **x > 1).count();
  23. // One trick to remove layers of indirection from iterators is to use the `cloned` iterator.
  24. // But is this any clearer? Probably not in this case.
  25. let overlaps = board.iter().flat_map(|a| a.iter().cloned()).filter(|x| *x > 1).count();
  26. println!("{}", overlaps);
  27. Ok(())
  28. }
  29.  
  30. fn update_board_with_line(board: &mut Vec<Vec<u8>>, line: &str) {
  31. lazy_static! {
  32. static ref CAPTURE_REGEX: Regex = Regex::new(r"(\d+) @ (\d+),(\d+): (\d+)x(\d+)").unwrap();
  33. }
  34. for cap in CAPTURE_REGEX.captures_iter(line) {
  35. // These unwraps take some thinking to convince me that they won't fail. I'd probably leave a comment here.
  36. // If it were just one I'd probably use 'expect' instead with an explanation of why the unwrap should work.
  37. let rect = (&cap[2].parse::<usize>().unwrap(),
  38. &cap[3].parse::<usize>().unwrap(),
  39. &cap[4].parse::<usize>().unwrap(),
  40. &cap[5].parse::<usize>().unwrap());
  41.  
  42. // All the dereferences here definitely seem suspicous - hard to read.
  43. // If I found myself writing this I'd try to avoid them.
  44. for row in *rect.1 .. *rect.3+*rect.1 {
  45. for col in *rect.0 .. *rect.2+*rect.0 {
  46. //board[row][col] = board[row][col] + 1;
  47. }
  48. }
  49.  
  50. // This version removes
  51. //
  52. // The ampersands here were purely unnecessary.
  53. //
  54. // when taking a reference to an element of an array
  55. // the dereference is necessary to avoid copying,
  56. // like
  57. //
  58. // let cap2 = &cap[2];
  59. //
  60. // but it's not necessary when chaining a method call like `parse`
  61. // the compiler will automatically add the reference-indirection necessary
  62. // to make the call.
  63. //
  64. // and in fact the ampersands that were here were just creating
  65. // an "artificial" reference indirection to the results of parse.
  66. //
  67. // e.g. &cap[2].parse().unwrap() is like writing &(cap[2].parse().unwrap())
  68. //
  69. // resulting in a type of &usize instead of just usize.
  70. //
  71. // this _can_ be useful if the result is only used in a reference context,
  72. // but in this example just makes the subsequent code that needs direct
  73. // usizes more complex by requiring them to be immediately dereferenced.
  74. let rect = (cap[2].parse::<usize>().unwrap(),
  75. cap[3].parse::<usize>().unwrap(),
  76. cap[4].parse::<usize>().unwrap(),
  77. cap[5].parse::<usize>().unwrap());
  78.  
  79. // See.. no dereference now
  80. for row in rect.1 .. rect.3+rect.1 {
  81. for col in rect.0 .. rect.2+rect.0 {
  82. board[row][col] = board[row][col] + 1;
  83. }
  84. }
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement