1
0
mirror of https://github.com/chylex/Advent-of-Code.git synced 2025-05-16 10:34:04 +02:00

Add 2020 - Day 2 - Part 1

This commit is contained in:
chylex 2022-02-23 01:36:42 +01:00
parent ef3703949e
commit cb2c6becbf
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
6 changed files with 1090 additions and 0 deletions
.idea/runConfigurations
2020
README.md

View File

@ -0,0 +1,18 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="2020 - Day 02" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
<option name="command" value="run --bin 02" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/2020/02" />
<option name="channel" value="DEFAULT" />
<option name="requiredFeatures" value="true" />
<option name="allFeatures" value="false" />
<option name="emulateTerminal" value="false" />
<option name="withSudo" value="false" />
<option name="backtrace" value="SHORT" />
<envs />
<option name="isRedirectInput" value="false" />
<option name="redirectInputPath" value="" />
<method v="2">
<option name="CARGO.BUILD_TASK_PROVIDER" enabled="true" />
</method>
</configuration>
</component>

1000
2020/02/input/1.txt Normal file

File diff suppressed because it is too large Load Diff

47
2020/02/main.rs Normal file
View File

@ -0,0 +1,47 @@
use std::error::Error;
use std::str::FromStr;
use utils::GenericError;
#[path = "../utils/mod.rs"]
mod utils;
fn main() -> Result<(), Box<dyn Error>> {
let lines = utils::parse_input_lines::<PasswordRule>()?;
let valid_passwords = lines.iter().filter(|x| x.is_valid()).count();
println!("Valid passwords: {}", valid_passwords);
Ok(())
}
struct PasswordRule {
left_value: usize,
right_value: usize,
required_char: String,
actual_password: String
}
impl PasswordRule {
fn is_valid(&self) -> bool {
let count = self.actual_password.matches(self.required_char.as_str()).count();
return count >= self.left_value && count <= self.right_value;
}
}
impl FromStr for PasswordRule {
type Err = GenericError<'static>;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (s_range, s_rest) = s.split_once(' ').ok_or(GenericError::new("Missing space."))?;
let (s_min, s_max) = s_range.split_once('-').ok_or(GenericError::new("Missing dash in the left part of the input."))?;
let (s_char, s_password) = s_rest.split_once(": ").ok_or(GenericError::new("Missing colon followed by space in the right part of the input."))?;
return Ok(PasswordRule {
left_value: s_min.parse::<usize>().map_err(|_| GenericError::new("Cannot parse first number."))?,
right_value: s_max.parse::<usize>().map_err(|_| GenericError::new("Cannot parse second number."))?,
required_char: s_char.to_string(),
actual_password: s_password.to_string()
})
}
}

View File

@ -6,3 +6,7 @@ edition = "2021"
[[bin]]
name = "01"
path = "01/main.rs"
[[bin]]
name = "02"
path = "02/main.rs"

View File

@ -1,4 +1,5 @@
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::fs::File;
use std::io;
use std::io::{BufRead, BufReader};
@ -12,3 +13,22 @@ pub fn read_input_lines() -> Result<Vec<String>, io::Error> {
pub fn parse_input_lines<T : FromStr>() -> Result<Vec<T>, Box<dyn Error>> where <T as FromStr>::Err : Into<Box<dyn Error>> {
return read_input_lines()?.iter().map(|line| line.parse::<T>()).collect::<Result<Vec<T>, T::Err>>().map_err(Into::into);
}
#[derive(Debug)]
pub struct GenericError<'a> {
pub message: &'a str
}
impl<'a> GenericError<'a> {
pub fn new(message: &'a str) -> GenericError<'a> {
GenericError { message }
}
}
impl Display for GenericError<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Parse error: {}", self.message)
}
}
impl Error for GenericError<'_> {}

View File

@ -38,6 +38,7 @@ The versions should not matter, but I used Visual Studio 2019 with `MSVC v142 (1
| 2015 | 01 | NASM x64 |
| 2015 | 02 | NASM x64 |
| 2020 | 01 | Rust |
| 2020 | 02 | Rust |
| 2021 | 01 | Kotlin |
| 2021 | 02 | Kotlin |
| 2021 | 03 | Kotlin |