Add solution for 2020 day 02
This commit is contained in:
parent
793f6e4fcc
commit
c995c3c472
@ -5,7 +5,7 @@
|
|||||||
| Day | Part 1 | Part 2 | Language |
|
| Day | Part 1 | Part 2 | Language |
|
||||||
| :-: | :----: | :----: | :------- |
|
| :-: | :----: | :----: | :------- |
|
||||||
| 01 | ✓ | ✓ | Rust |
|
| 01 | ✓ | ✓ | Rust |
|
||||||
| 02 | | | |
|
| 02 | ✓ | ✓ | Rust |
|
||||||
| 03 | | | |
|
| 03 | | | |
|
||||||
| 04 | | | |
|
| 04 | | | |
|
||||||
| 05 | | | |
|
| 05 | | | |
|
||||||
|
2
2020/day-02/.gitignore
vendored
Normal file
2
2020/day-02/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
part_one
|
||||||
|
part_two
|
30
2020/day-02/common.rs
Normal file
30
2020/day-02/common.rs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
use std::env;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
|
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||||
|
|
||||||
|
pub fn read_file() -> Result<Vec<(usize, usize, char, String)>> {
|
||||||
|
let path = env::args().skip(1).next().unwrap();
|
||||||
|
let mut file = File::open(path)?;
|
||||||
|
let mut content = String::new();
|
||||||
|
file.read_to_string(&mut content)?;
|
||||||
|
|
||||||
|
let result = content
|
||||||
|
.lines()
|
||||||
|
.map(|line| {
|
||||||
|
let tokens = line
|
||||||
|
.split(&[' ', ':', '-'][..])
|
||||||
|
.filter(|&x| x != "")
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
(
|
||||||
|
tokens[0].parse().unwrap(),
|
||||||
|
tokens[1].parse().unwrap(),
|
||||||
|
tokens[2].chars().next().unwrap(),
|
||||||
|
tokens[3].into(),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
1000
2020/day-02/inputs/puzzle.txt
Normal file
1000
2020/day-02/inputs/puzzle.txt
Normal file
File diff suppressed because it is too large
Load Diff
3
2020/day-02/inputs/sample.txt
Normal file
3
2020/day-02/inputs/sample.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
1-3 a: abcde
|
||||||
|
1-3 b: cdefg
|
||||||
|
2-9 c: ccccccccc
|
33
2020/day-02/part_one.rs
Normal file
33
2020/day-02/part_one.rs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
mod common;
|
||||||
|
|
||||||
|
use common::{read_file, Result};
|
||||||
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
let entries = read_file()?;
|
||||||
|
|
||||||
|
let mut valid_passwords = 0;
|
||||||
|
|
||||||
|
'entries: for (min, max, req_char, password) in entries {
|
||||||
|
let mut char_counter = 0;
|
||||||
|
|
||||||
|
for character in password.chars() {
|
||||||
|
if character == req_char {
|
||||||
|
char_counter += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if char_counter > max {
|
||||||
|
continue 'entries;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if char_counter < min {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
valid_passwords += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{}", valid_passwords);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
22
2020/day-02/part_two.rs
Normal file
22
2020/day-02/part_two.rs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
mod common;
|
||||||
|
|
||||||
|
use common::{read_file, Result};
|
||||||
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
let entries = read_file()?;
|
||||||
|
|
||||||
|
let mut valid_passwords = 0;
|
||||||
|
|
||||||
|
for (pos1, pos2, req_char, password) in entries {
|
||||||
|
let a = password.chars().nth(pos1 - 1).unwrap() == req_char;
|
||||||
|
let b = password.chars().nth(pos2 - 1).unwrap() == req_char;
|
||||||
|
|
||||||
|
if a ^ b {
|
||||||
|
valid_passwords += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{}", valid_passwords);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user