Compare commits
2 commits
f7767d9f31
...
735089ad42
Author | SHA1 | Date | |
---|---|---|---|
Tristan Daniël Maat | 735089ad42 | ||
Tristan Daniël Maat | 157f260d8f |
1
day-4/.gitignore
vendored
Normal file
1
day-4/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target/
|
78
day-4/Cargo.lock
generated
Normal file
78
day-4/Cargo.lock
generated
Normal file
|
@ -0,0 +1,78 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "day-4"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"indoc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indoc"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "47741a8bc60fb26eb8d6e0238bbb26d8575ff623fdc97b1a2c00c050b9684ed8"
|
||||
dependencies = [
|
||||
"indoc-impl",
|
||||
"proc-macro-hack",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indoc-impl"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce046d161f000fffde5f432a0d034d0341dc152643b2598ed5bfce44c4f3a8f0"
|
||||
dependencies = [
|
||||
"proc-macro-hack",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"unindent",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-hack"
|
||||
version = "0.5.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.53"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8833e20724c24de12bbaba5ad230ea61c3eafb05b881c7c9d3cfe8638b187e68"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
|
||||
|
||||
[[package]]
|
||||
name = "unindent"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f14ee04d9415b52b3aeab06258a3f07093182b88ba0f9b8d203f211a7a7d41c7"
|
10
day-4/Cargo.toml
Normal file
10
day-4/Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "day-4"
|
||||
version = "0.1.0"
|
||||
authors = ["Tristan Daniël Maat <tm@tlater.net>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
indoc = "0.3"
|
1068
day-4/input
Normal file
1068
day-4/input
Normal file
File diff suppressed because it is too large
Load diff
120
day-4/src/main.rs
Normal file
120
day-4/src/main.rs
Normal file
|
@ -0,0 +1,120 @@
|
|||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = fs::read_to_string("input")?;
|
||||
let passports = parse_passports(&input)?;
|
||||
println!("{}", validate_passports(&passports));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_passports(input: &str) -> Result<Vec<HashMap<&str, &str>>, &str> {
|
||||
input
|
||||
.split("\n\n")
|
||||
.map(|passport| {
|
||||
passport
|
||||
.trim()
|
||||
.split(|c| c == ' ' || c == '\n')
|
||||
.map(|field| {
|
||||
let split = field.find(':').ok_or("Invalid passport entry; no separator")?;
|
||||
let item = field.split_at(split);
|
||||
|
||||
let key = item.0;
|
||||
let value = item
|
||||
.1
|
||||
.strip_prefix(":")
|
||||
.ok_or("Invalid entry; value too short")?;
|
||||
|
||||
Ok((key, value))
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn validate_passports(passports: &Vec<HashMap<&str, &str>>) -> usize {
|
||||
let required_fields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"];
|
||||
let rules = [
|
||||
|year: &str| match year.parse() {
|
||||
Ok(1920..=2002) => true,
|
||||
_ => false,
|
||||
},
|
||||
|year: &str| match year.parse() {
|
||||
Ok(2010..=2020) => true,
|
||||
_ => false,
|
||||
},
|
||||
|year: &str| match year.parse() {
|
||||
Ok(2020..=2030) => true,
|
||||
_ => false,
|
||||
},
|
||||
|height: &str| match height.split_at(height.chars().count() - 2) {
|
||||
(height, "cm") => match height.parse() {
|
||||
Ok(150..=193) => true,
|
||||
_ => false,
|
||||
},
|
||||
(height, "in") => match height.parse() {
|
||||
Ok(59..=76) => true,
|
||||
_ => false,
|
||||
},
|
||||
_ => false,
|
||||
},
|
||||
|color: &str| match color.split_at(1) {
|
||||
("#", code) => code.chars().all(|a| char::is_ascii_hexdigit(&a)),
|
||||
_ => false,
|
||||
},
|
||||
|color: &str| {
|
||||
["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]
|
||||
.iter()
|
||||
.any(|c| c == &color)
|
||||
},
|
||||
|pid: &str| pid.chars().count() == 9 && pid.chars().all(char::is_numeric),
|
||||
];
|
||||
|
||||
passports
|
||||
.iter()
|
||||
.filter(|passport| {
|
||||
required_fields
|
||||
.iter()
|
||||
.zip(rules.iter())
|
||||
.all(|(field, rule)| {
|
||||
if let Some(value) = passport.get(field) {
|
||||
rule(value)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
})
|
||||
.count()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use indoc::indoc;
|
||||
|
||||
#[test]
|
||||
fn test_simple() -> Result<(), String> {
|
||||
let example = indoc!(
|
||||
"ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
|
||||
byr:1937 iyr:2017 cid:147 hgt:183cm
|
||||
|
||||
iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
|
||||
hcl:#cfa07d byr:1929
|
||||
|
||||
hcl:#ae17e1 iyr:2013
|
||||
eyr:2024
|
||||
ecl:brn pid:760753108 byr:1931
|
||||
hgt:179cm
|
||||
|
||||
hcl:#cfa07d eyr:2025 pid:166559648
|
||||
iyr:2011 ecl:brn hgt:59in
|
||||
"
|
||||
);
|
||||
|
||||
let passports = parse_passports(example)?;
|
||||
assert_eq!(validate_passports(&passports), 2);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue