Rust challenge 26/100 - advent of code 2020 day 2
Table of content
What is this
The rules of the game are explained in my original post.
26th Challenge
Challenge
I have never part taken in Advent of Code. This year I might. To get used to the idea I’m solving last year’s second challenge. Solve AoC day 2 2020.
Solution
fn main() {
let answer = INPUT.lines()
.filter(|&x| check_line(x))
.count();
println!("{}",answer);
}
fn check_line(line:&str) -> bool {
let mut s:Vec<&str> = line
.split(' ')
.collect();
let range = s
.remove(0)
.split_once('-')
.unwrap();
let min:usize = range
.0
.parse()
.unwrap();
let max:usize = range
.1
.parse()
.unwrap();
let letter = s
.remove(0)
.trim_end_matches(":")
.chars()
.nth(0)
.unwrap();
let password = s.remove(0);
let num = password
.chars()
.filter(|c| c.eq(&letter))
.count();
num >= min && num <= max
}
const INPUT:&str = "1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc";
To see the full source see github and see the live demo in playground.