Day 2 first part done, aoc 2020
This commit is contained in:
7
advent_of_code/2020/2/Cargo.toml
Normal file
7
advent_of_code/2020/2/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "main"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
nom = "7.1.3"
|
||||||
1000
advent_of_code/2020/2/input.txt
Normal file
1000
advent_of_code/2020/2/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
67
advent_of_code/2020/2/src/main.rs
Normal file
67
advent_of_code/2020/2/src/main.rs
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
use nom::{
|
||||||
|
bytes::complete::tag,
|
||||||
|
character::complete::{alpha1, anychar, multispace1, u32},
|
||||||
|
multi::separated_list1,
|
||||||
|
sequence::{preceded, tuple},
|
||||||
|
IResult,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn parse_input(input: &str) -> IResult<&str, Vec<(u32, u32, char, &str)>> {
|
||||||
|
let (input, result) = separated_list1(
|
||||||
|
multispace1,
|
||||||
|
tuple((
|
||||||
|
u32,
|
||||||
|
preceded(tag("-"), u32),
|
||||||
|
preceded(tag(" "), anychar),
|
||||||
|
preceded(tag(": "), alpha1),
|
||||||
|
)),
|
||||||
|
)(input)?;
|
||||||
|
|
||||||
|
Ok((input, result))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve_1(input: &str) -> usize {
|
||||||
|
let (_, input) = parse_input(input).unwrap();
|
||||||
|
|
||||||
|
input
|
||||||
|
.into_iter()
|
||||||
|
.filter(|(min, max, c, s)| {
|
||||||
|
let mut cs = 0;
|
||||||
|
for ch in s.chars() {
|
||||||
|
if ch == *c {
|
||||||
|
cs += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cs >= *min && cs <= *max;
|
||||||
|
})
|
||||||
|
.count()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve_2(input: &str) {}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, this is Patrick!");
|
||||||
|
|
||||||
|
let input = include_str!("../input.txt");
|
||||||
|
|
||||||
|
let result_1 = solve_1(input);
|
||||||
|
println!("The number of valid passwords is {}", result_1);
|
||||||
|
|
||||||
|
let result_2 = solve_2(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_1() {
|
||||||
|
let test_input = include_str!("../test.txt");
|
||||||
|
assert_eq!(solve_1(test_input), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_2() {}
|
||||||
|
}
|
||||||
3
advent_of_code/2020/2/test.txt
Normal file
3
advent_of_code/2020/2/test.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
1-3 a: abcde
|
||||||
|
1-3 b: cdefg
|
||||||
|
2-9 c: ccccccccc
|
||||||
Reference in New Issue
Block a user