Skip to content

Instantly share code, notes, and snippets.

@sandeshbhusal
Created August 15, 2021 14:08
Show Gist options
  • Save sandeshbhusal/59e60709d3b0d35689fe17f36323f8b4 to your computer and use it in GitHub Desktop.
Save sandeshbhusal/59e60709d3b0d35689fe17f36323f8b4 to your computer and use it in GitHub Desktop.
Advent of Code 2020 Problem 2 Solution #1 RUST Solution
use scan_fmt::{scan_fmt};
use std::{fs::File, io:: {BufRead, BufReader}};
fn main(){
let file_path : String = String::from("src/input.txt");
let bufreader = BufReader::new(File::open(file_path).expect("Could not open file"));
let correct_passwords = bufreader.lines().map(|_line| {
match scan_fmt!(&_line.unwrap(), "{}-{} {}: {}" , i32, i32, char, String) {
Ok((a, b, c, d)) => {
if a <= d.chars().map(|k| { if c == k {1} else {0} } ).sum::<i32>() && b>= d.chars().map(|k| { if c == k {1} else {0} } ).sum::<i32>() {
1
} else {0}
},
Err(_) => 0,
}
}).sum::<i32>();
println!("Correct passwords: {}", correct_passwords);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment