diff options
| author | Santo Cariotti <santo@dcariotti.me> | 2021-12-08 14:40:28 +0100 | 
|---|---|---|
| committer | Santo Cariotti <santo@dcariotti.me> | 2021-12-08 14:40:28 +0100 | 
| commit | ee4c8035e1f31ca1c5e856277d9706f7730785d9 (patch) | |
| tree | 75efac057a5794092e526b8f9a603569a2845f3f | |
| parent | 888fe73d71bee5d2f23c09522bce090fcd77e976 (diff) | |
Use function for part 1
| -rw-r--r-- | day4/src/main.rs | 82 | 
1 files changed, 43 insertions, 39 deletions
diff --git a/day4/src/main.rs b/day4/src/main.rs index aed66ec..6e44c10 100644 --- a/day4/src/main.rs +++ b/day4/src/main.rs @@ -1,44 +1,7 @@  use std::fs::File;  use std::io::{BufRead, BufReader}; -fn main() -> std::io::Result<()> { -    let file = File::open("input.txt")?; -    let reader = BufReader::new(file); -    let mut lines = reader.lines(); - -    let mut inputs: Vec<_> = lines -        .next() -        .unwrap() -        .unwrap() -        .trim() -        .split(',') -        .map(|x| x.parse::<u32>().unwrap()) -        .collect::<Vec<u32>>(); - -    let mut grids: Vec<Vec<u32>> = vec![]; -    let mut n: usize = 0; - -    lines.next(); // Ignore the first empty line -    while let Some(line) = lines.next() { -        let mut line = line.unwrap(); -        grids.push(Vec::with_capacity(5 * 5)); - -        for _ in 0..5 { -            grids[n].extend( -                line.trim() -                    .split(' ') -                    .filter(|x| !x.is_empty()) -                    .map(|x| x.parse::<u32>().unwrap()) -                    .collect::<Vec<u32>>(), -            ); -            line = match lines.next() { -                Some(x) => x.unwrap(), -                None => "".to_string(), -            }; -        } -        n += 1; -    } - +fn part1(grids: &Vec<Vec<u32>>, mut inputs: Vec<u32>) -> u32 {      let mut values = Vec::<u32>::new();      for _ in 0..4 { @@ -97,7 +60,48 @@ fn main() -> std::io::Result<()> {          sum += x;      } -    println!("{}", sum * values.pop().unwrap()); +    sum * values.pop().unwrap() +} + +fn main() -> std::io::Result<()> { +    let file = File::open("input.txt")?; +    let reader = BufReader::new(file); +    let mut lines = reader.lines(); + +    let inputs: Vec<_> = lines +        .next() +        .unwrap() +        .unwrap() +        .trim() +        .split(',') +        .map(|x| x.parse::<u32>().unwrap()) +        .collect::<Vec<u32>>(); + +    let mut grids: Vec<Vec<u32>> = vec![]; +    let mut n: usize = 0; + +    lines.next(); // Ignore the first empty line +    while let Some(line) = lines.next() { +        let mut line = line.unwrap(); +        grids.push(Vec::with_capacity(5 * 5)); + +        for _ in 0..5 { +            grids[n].extend( +                line.trim() +                    .split(' ') +                    .filter(|x| !x.is_empty()) +                    .map(|x| x.parse::<u32>().unwrap()) +                    .collect::<Vec<u32>>(), +            ); +            line = match lines.next() { +                Some(x) => x.unwrap(), +                None => "".to_string(), +            }; +        } +        n += 1; +    } + +    println!("{}", part1(&grids, inputs.clone()));      Ok(())  }  |