Skip to content

Commit 01d9eae

Browse files
committed
feat: completed day 4
1 parent 6975063 commit 01d9eae

File tree

7 files changed

+1416
-1
lines changed

7 files changed

+1416
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
- [Day 1](https://github.com/ankjevel/adventofcode/tree/2020/day_01) 🌟 🌟
77
- [Day 2](https://github.com/ankjevel/adventofcode/tree/2020/day_02) 🌟 🌟
88
- [Day 3](https://github.com/ankjevel/adventofcode/tree/2020/day_03) 🌟 🌟
9-
- [Day 4](#)
9+
- [Day 4](https://github.com/ankjevel/adventofcode/tree/2020/day_04) 🌟 🌟
1010
- [Day 5](#)
1111
- [Day 6](#)
1212
- [Day 7](#)

day_04/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "day_04"
3+
version = "0.1.0"
4+
authors = ["Dennis Pettersson <mail@dennispettersson.se>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
regex = "1.4.2"
11+
lazy_static = "1.4.0"

day_04/src/lib.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#[macro_use(lazy_static)]
2+
extern crate lazy_static;
3+
extern crate regex;
4+
5+
pub mod part_01;
6+
pub mod part_02;
7+
8+
pub fn parse_input(input: &str) -> Vec<Vec<(String, String)>> {
9+
input
10+
.lines()
11+
.map(|string| string.trim())
12+
.into_iter()
13+
.fold(vec![vec![]], |mut list, string| {
14+
if string.is_empty() {
15+
list.push(vec![]);
16+
} else {
17+
let last = list.last_mut().unwrap();
18+
last.push(string.to_string());
19+
}
20+
list
21+
})
22+
.into_iter()
23+
.filter(|list| list.len() > 0)
24+
.map(|passport| {
25+
passport
26+
.join(" ")
27+
.split(" ")
28+
.map(|part| {
29+
let mut split = part.split(":").into_iter();
30+
(
31+
split.next().unwrap().to_owned(),
32+
split.next().unwrap().to_owned(),
33+
)
34+
})
35+
.collect()
36+
})
37+
.collect()
38+
}
39+
40+
#[cfg(test)]
41+
mod tests {
42+
use crate::parse_input;
43+
44+
const EXAMPLE_DATA: &'static str = "
45+
ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
46+
byr:1937 iyr:2017 cid:147 hgt:183cm
47+
48+
iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
49+
hcl:#cfa07d byr:1929
50+
51+
hcl:#ae17e1 iyr:2013
52+
eyr:2024
53+
ecl:brn pid:760753108 byr:1931
54+
hgt:179cm
55+
56+
hcl:#cfa07d eyr:2025 pid:166559648
57+
iyr:2011 ecl:brn hgt:59in
58+
";
59+
60+
#[test]
61+
fn it_parses_the_example_input() {
62+
assert_eq!(parse_input(&EXAMPLE_DATA).len(), 4);
63+
}
64+
}

day_04/src/main.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use std::io::Result;
2+
3+
use day_04::{parse_input, part_01::main as part_01, part_02::main as part_02};
4+
5+
fn main() -> Result<()> {
6+
let input = parse_input(include_str!("../../input/day_04"));
7+
8+
println!("part_01: {:?}", part_01(&input).unwrap());
9+
println!("part_02: {:?}", part_02(&input).unwrap());
10+
11+
Ok(())
12+
}

day_04/src/part_01.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use std::{collections::HashMap, io::Result};
2+
3+
pub fn main(input: &Vec<Vec<(String, String)>>) -> Result<usize> {
4+
Ok(input
5+
.iter()
6+
.map(|row| {
7+
let mut passport = HashMap::new();
8+
for (key, value) in row {
9+
passport.insert(key.to_string(), value.to_string());
10+
}
11+
passport.to_owned()
12+
})
13+
.filter(|passport| {
14+
if passport.keys().len() == 8 {
15+
true
16+
} else {
17+
passport.keys().len() == 7 && passport.contains_key("cid") == false
18+
}
19+
})
20+
.collect::<Vec<HashMap<String, String>>>()
21+
.len())
22+
}
23+
24+
#[cfg(test)]
25+
mod tests {
26+
use crate::parse_input;
27+
28+
use super::main;
29+
30+
const EXAMPLE_DATA: &'static str = "
31+
ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
32+
byr:1937 iyr:2017 cid:147 hgt:183cm
33+
34+
iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
35+
hcl:#cfa07d byr:1929
36+
37+
hcl:#ae17e1 iyr:2013
38+
eyr:2024
39+
ecl:brn pid:760753108 byr:1931
40+
hgt:179cm
41+
42+
hcl:#cfa07d eyr:2025 pid:166559648
43+
iyr:2011 ecl:brn hgt:59in
44+
";
45+
46+
#[test]
47+
fn should_get_the_same_amount_of_valid_passports_as_example() {
48+
assert_eq!(main(&parse_input(&EXAMPLE_DATA)).unwrap(), 2);
49+
}
50+
}

day_04/src/part_02.rs

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
use std::{collections::HashMap, io::Result};
2+
3+
use regex::Regex;
4+
5+
lazy_static! {
6+
static ref VALID_HEIGHT: Regex = Regex::new(r"(\d+)+((cm)|(in)){1}").unwrap();
7+
static ref VALID_HEX: Regex = Regex::new(r"#([a-f0-9]){6}").unwrap();
8+
static ref VALID_EYE_COLOR: Vec<String> = vec!["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]
9+
.iter()
10+
.map(|string| string.to_string())
11+
.collect();
12+
static ref VALID_PIN: Regex = Regex::new(r"^\d{9}$").unwrap();
13+
}
14+
15+
type Passport = HashMap<String, String>;
16+
17+
fn valid_birth_year(passport: &Passport) -> bool {
18+
match passport.get("byr") {
19+
Some(string) => (1920..=2002).contains(&string.parse::<u16>().unwrap_or(0)),
20+
_ => false,
21+
}
22+
}
23+
fn valid_issue_year(passport: &Passport) -> bool {
24+
match passport.get("iyr") {
25+
Some(string) => (2010..=2020).contains(&string.parse::<u16>().unwrap_or(0)),
26+
_ => false,
27+
}
28+
}
29+
fn valid_expiration_year(passport: &Passport) -> bool {
30+
match passport.get("eyr") {
31+
Some(string) => (2020..=2030).contains(&string.parse::<u16>().unwrap_or(0)),
32+
_ => false,
33+
}
34+
}
35+
fn valid_height(passport: &Passport) -> bool {
36+
let string = passport.get("hgt");
37+
if string.is_none() {
38+
return false;
39+
}
40+
41+
let string = string.unwrap().to_owned();
42+
if !VALID_HEIGHT.is_match(&string) {
43+
return false;
44+
}
45+
46+
let caps = VALID_HEIGHT.captures(&string).unwrap();
47+
let height = &caps.get(1).unwrap().as_str().parse::<u16>().unwrap_or(0);
48+
if let Some(_) = caps.get(3) {
49+
(150..=193).contains(height)
50+
} else {
51+
(59..=76).contains(height)
52+
}
53+
}
54+
fn valid_hair_color(passport: &Passport) -> bool {
55+
match passport.get("hcl") {
56+
None => false,
57+
Some(string) => VALID_HEX.is_match(&string),
58+
}
59+
}
60+
fn valid_eye_color(passport: &Passport) -> bool {
61+
match passport.get("ecl") {
62+
None => false,
63+
Some(string) => VALID_EYE_COLOR.contains(&string),
64+
}
65+
}
66+
fn valid_passport_id(passport: &Passport) -> bool {
67+
match passport.get("pid") {
68+
None => false,
69+
Some(string) => VALID_PIN.is_match(&string),
70+
}
71+
}
72+
73+
fn valid(passport: &Passport) -> bool {
74+
vec![
75+
valid_birth_year(&passport),
76+
valid_issue_year(&passport),
77+
valid_expiration_year(&passport),
78+
valid_height(&passport),
79+
valid_hair_color(&passport),
80+
valid_eye_color(&passport),
81+
valid_passport_id(&passport),
82+
]
83+
.into_iter()
84+
.filter(|valid| valid.to_owned())
85+
.count()
86+
== 7
87+
}
88+
89+
pub fn main(input: &Vec<Vec<(String, String)>>) -> Result<usize> {
90+
Ok(input
91+
.iter()
92+
.map(|row| {
93+
let mut passport = HashMap::new();
94+
for (key, value) in row {
95+
passport.insert(key.to_string(), value.to_string());
96+
}
97+
passport.to_owned()
98+
})
99+
.filter(|passport| valid(passport))
100+
.collect::<Vec<Passport>>()
101+
.len())
102+
}
103+
104+
#[cfg(test)]
105+
mod tests {
106+
use crate::parse_input;
107+
108+
use super::main;
109+
110+
const EXAMPLE_DATA: &'static str = "
111+
eyr:1972 cid:100 hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926
112+
113+
iyr:2019 hcl:#602927 eyr:1967 hgt:170cm ecl:grn pid:012533040 byr:1946
114+
115+
hcl:dab227 iyr:2012 ecl:brn hgt:182cm pid:021572410 eyr:2020 byr:1992 cid:277
116+
117+
hgt:59cm ecl:zzz eyr:2038 hcl:74454a iyr:2023 pid:3556412378 byr:2007
118+
119+
pid:087499704 hgt:74in ecl:grn iyr:2012 eyr:2030 byr:1980 hcl:#623a2f
120+
121+
eyr:2029 ecl:blu cid:129 byr:1989 iyr:2014 pid:896056539 hcl:#a97842 hgt:165cm
122+
123+
hcl:#888785 hgt:164cm byr:2001 iyr:2015 cid:88 pid:545766238 ecl:hzl eyr:2022
124+
125+
iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719
126+
";
127+
128+
#[test]
129+
fn should_get_the_same_amount_of_valid_passports_as_example() {
130+
assert_eq!(main(&parse_input(&EXAMPLE_DATA)).unwrap(), 4);
131+
}
132+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy