Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::fs::File;
- use std::io::{self, prelude::*, BufReader};
- use lazy_static::lazy_static;
- use regex::Regex;
- fn main() -> io::Result<()>{
- let mut board =vec![vec![0u8; 1000]; 1000];
- //let file = File::open("input")?;
- let ref mut file = std::io::Cursor::new(b"
- #1 @ 1,3: 4x4
- #2 @ 3,1: 4x4
- #3 @ 5,5: 2x2".to_vec());
- let reader = BufReader::new(file);
- for line in reader.lines() {
- update_board_with_line(&mut board, &line?[..]);
- }
- // The two sigils in the closure are a code smell
- let overlaps = board.iter().flat_map(|a| a.iter()).filter(|&x| *x > 1).count();
- // But so are double-derefereces like this. Like, where did all these references come from!?
- let overlaps = board.iter().flat_map(|a| a.iter()).filter(|x| **x > 1).count();
- // One trick to remove layers of indirection from iterators is to use the `cloned` iterator.
- // But is this any clearer? Probably not in this case.
- let overlaps = board.iter().flat_map(|a| a.iter().cloned()).filter(|x| *x > 1).count();
- println!("{}", overlaps);
- Ok(())
- }
- fn update_board_with_line(board: &mut Vec<Vec<u8>>, line: &str) {
- lazy_static! {
- static ref CAPTURE_REGEX: Regex = Regex::new(r"(\d+) @ (\d+),(\d+): (\d+)x(\d+)").unwrap();
- }
- for cap in CAPTURE_REGEX.captures_iter(line) {
- // These unwraps take some thinking to convince me that they won't fail. I'd probably leave a comment here.
- // If it were just one I'd probably use 'expect' instead with an explanation of why the unwrap should work.
- let rect = (&cap[2].parse::<usize>().unwrap(),
- &cap[3].parse::<usize>().unwrap(),
- &cap[4].parse::<usize>().unwrap(),
- &cap[5].parse::<usize>().unwrap());
- // All the dereferences here definitely seem suspicous - hard to read.
- // If I found myself writing this I'd try to avoid them.
- for row in *rect.1 .. *rect.3+*rect.1 {
- for col in *rect.0 .. *rect.2+*rect.0 {
- //board[row][col] = board[row][col] + 1;
- }
- }
- // This version removes
- //
- // The ampersands here were purely unnecessary.
- //
- // when taking a reference to an element of an array
- // the dereference is necessary to avoid copying,
- // like
- //
- // let cap2 = &cap[2];
- //
- // but it's not necessary when chaining a method call like `parse`
- // the compiler will automatically add the reference-indirection necessary
- // to make the call.
- //
- // and in fact the ampersands that were here were just creating
- // an "artificial" reference indirection to the results of parse.
- //
- // e.g. &cap[2].parse().unwrap() is like writing &(cap[2].parse().unwrap())
- //
- // resulting in a type of &usize instead of just usize.
- //
- // this _can_ be useful if the result is only used in a reference context,
- // but in this example just makes the subsequent code that needs direct
- // usizes more complex by requiring them to be immediately dereferenced.
- let rect = (cap[2].parse::<usize>().unwrap(),
- cap[3].parse::<usize>().unwrap(),
- cap[4].parse::<usize>().unwrap(),
- cap[5].parse::<usize>().unwrap());
- // See.. no dereference now
- for row in rect.1 .. rect.3+rect.1 {
- for col in rect.0 .. rect.2+rect.0 {
- board[row][col] = board[row][col] + 1;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement