Skip to content
Snippets Groups Projects
Commit d637343b authored by Philip Molares's avatar Philip Molares :popcorn:
Browse files

added day04

parent 0c683379
No related branches found
No related tags found
No related merge requests found
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "aho-corasick"
version = "0.7.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5"
dependencies = [
"memchr",
]
[[package]]
name = "day04"
version = "0.1.0"
dependencies = [
"regex",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "memchr"
version = "2.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525"
[[package]]
name = "regex"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38cf2c13ed4745de91a5eb834e11c00bcc3709e773173b2ce4c56c9fbde04b9c"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
"thread_local",
]
[[package]]
name = "regex-syntax"
version = "0.6.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b181ba2dcf07aaccad5448e8ead58db5b742cf85dfe035e2227f137a539a189"
[[package]]
name = "thread_local"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
dependencies = [
"lazy_static",
]
[package]
name = "day04"
version = "0.1.0"
authors = ["Philip Molares <philip.molares@udo.edu>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
regex = "1.1.9"
This diff is collapsed.
use std::fs::File;
use std::io::{self, prelude::*, BufReader};
use std::str::FromStr;
use regex::Regex;
#[derive(Debug)]
struct Passport {
byr: String,
iyr: String,
eyr: String,
hgt: String,
hcl: String,
ecl: String,
pid: String,
cid: String
}
impl FromStr for Passport {
type Err = std::num::ParseIntError;
fn from_str(input: &str) -> Result<Self, Self::Err> {
let regex_byr = Regex::new(r"byr:(?P<byr>[^\s]*)").unwrap();
let byr = String::from(
regex_byr.captures(input).and_then(|caps| {
caps.name("byr").and_then(|m| Some(m.as_str()))
}).unwrap_or("")
);
let regex_iyr = Regex::new(r"iyr:(?P<iyr>[^\s]*)").unwrap();
let iyr = String::from(
regex_iyr.captures(input).and_then(|caps| {
caps.name("iyr").and_then(|m| Some(m.as_str()))
}).unwrap_or("")
);
let regex_eyr = Regex::new(r"eyr:(?P<eyr>[^\s]*)").unwrap();
let eyr = String::from(
regex_eyr.captures(input).and_then(|caps| {
caps.name("eyr").and_then(|m| Some(m.as_str()))
}).unwrap_or("")
);
let regex_hgt = Regex::new(r"hgt:(?P<hgt>[^\s]*)").unwrap();
let hgt = String::from(
regex_hgt.captures(input).and_then(|caps| {
caps.name("hgt").and_then(|m| Some(m.as_str()))
}).unwrap_or("")
);
let regex_hcl = Regex::new(r"hcl:(?P<hcl>[^\s]*)").unwrap();
let hcl = String::from(
regex_hcl.captures(input).and_then(|caps| {
caps.name("hcl").and_then(|m| Some(m.as_str()))
}).unwrap_or("")
);
let regex_ecl = Regex::new(r"ecl:(?P<ecl>[^\s]*)").unwrap();
let ecl = String::from(
regex_ecl.captures(input).and_then(|caps| {
caps.name("ecl").and_then(|m| Some(m.as_str()))
}).unwrap_or("")
);
let regex_pid = Regex::new(r"pid:(?P<pid>[^\s]*)").unwrap();
let pid = String::from(
regex_pid.captures(input).and_then(|caps| {
caps.name("pid").and_then(|m| Some(m.as_str()))
}).unwrap_or("")
);
let regex_cid = Regex::new(r"cid:(?P<cid>[^\s]*)").unwrap();
let cid = String::from(
regex_cid.captures(input).and_then(|caps| {
caps.name("cid").and_then(|m| Some(m.as_str()))
}).unwrap_or("")
);
Ok(Passport {
byr: byr,
iyr: iyr,
eyr: eyr,
hgt: hgt,
hcl: hcl,
ecl: ecl,
pid: pid,
cid: cid
})
}
}
impl Passport {
fn valid(&self) -> bool {
self.byr != "" &&
self.iyr != "" &&
self.eyr != "" &&
self.hgt != "" &&
self.hcl != "" &&
self.ecl != "" &&
self.pid != ""
}
fn valid_byr(&self) -> bool {
self.byr.parse::<i64>().map_or(false, |byr| byr >= 1920 && byr <= 2002)
}
fn valid_iyr(&self) -> bool {
self.iyr.parse::<i64>().map_or(false, |iyr| iyr >= 2010 && iyr <= 2020)
}
fn valid_eyr(&self) -> bool {
self.eyr.parse::<i64>().map_or(false, |eyr| eyr >= 2020 && eyr <= 2030)
}
fn valid_hgt(&self) -> bool {
let regex_hgt = Regex::new(r"([0-9]{3}|[0-9]{2})(cm|in)").unwrap();
regex_hgt.captures(&self.hgt).and_then(|caps| {
caps.get(2).and_then(|unit| {
match unit.as_str() {
"cm" => {
caps.get(1).and_then(|number_string| {
let number = number_string.as_str().parse::<i64>().unwrap();
Some(number >= 150 && number <= 193)
})
}
"in" => {
caps.get(1).and_then(|number_string| {
let number = number_string.as_str().parse::<i64>().unwrap();
Some(number >= 59 && number <= 76)
})
}
_ => Some(false)
}
})
}).unwrap_or(false)
}
fn valid_hcl(&self) -> bool {
let regex_hcl = Regex::new(r"(#[0-9a-f]{6})").unwrap();
regex_hcl.captures(&self.hcl).and_then(|caps| {
caps.get(1).and_then(|_| Some(true))
}).unwrap_or(false)
}
fn valid_ecl(&self) -> bool {
let regex_ecl = Regex::new(r"(amb|blu|brn|gry|grn|hzl|oth)").unwrap();
regex_ecl.captures(&self.ecl).and_then(|caps| {
caps.get(1).and_then(|_| Some(true))
}).unwrap_or(false)
}
fn valid_pid(&self) -> bool {
self.pid.len() == (9 as usize)
}
fn valid_values(&self) -> bool {
self.valid_byr() &&
self.valid_iyr() &&
self.valid_eyr() &&
self.valid_hgt() &&
self.valid_hcl() &&
self.valid_ecl() &&
self.valid_pid()
}
}
fn main() -> io::Result<()> {
println!("Advent of Code 2020 – Day 4:");
let file = File::open("input.txt")?;
let reader = BufReader::new(file);
let mut input = reader
.lines()
.map(|l| l.unwrap());
let mut optional_line = input.next();
let mut buffer = String::from("");
let mut passports: Vec<Passport> = vec![];
while optional_line != None {
let line = optional_line.unwrap();
buffer += &line.clone();
buffer += &String::from(" ");
if &line == "" {
// split found
passports.push(Passport::from_str(&buffer).unwrap());
buffer = String::from("");
}
optional_line = input.next();
}
// push last entry
passports.push(Passport::from_str(&buffer).unwrap());
println!("{}", passports.len());
let correct_passports = passports
.iter()
.filter(|p| p.valid())
.count();
println!("Answer Part 1: {}", correct_passports);
let correct_value_passports = passports
.iter()
.filter(|p| p.valid_values())
.count();
println!("Answer Part 2: {}", correct_value_passports);
Ok(())
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment