[TASK] Finished Day 4
This commit is contained in:
1169
input/day4.txt
Normal file
1169
input/day4.txt
Normal file
File diff suppressed because it is too large
Load Diff
13
input/day4_example.txt
Normal file
13
input/day4_example.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
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
|
||||
68
src/day4.rs
Normal file
68
src/day4.rs
Normal file
@@ -0,0 +1,68 @@
|
||||
// use super::util;
|
||||
use std::fs;
|
||||
|
||||
static VALID_EYE_COLORS: &'static[&'static str] =
|
||||
&["amb", "blu", "brn", "gry", "grn", "hzl", "oth"];
|
||||
|
||||
pub fn solve() {
|
||||
let raw_batch = fs::read_to_string("input/day4.txt").expect("Couldn't read file!");
|
||||
let raw_fields = raw_batch
|
||||
.split("\r\n\r\n")
|
||||
.map(|s|
|
||||
s.split(char::is_whitespace)
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(|s| String::from(s))
|
||||
.collect::<Vec<String>>()
|
||||
).collect::<Vec<Vec<String>>>();
|
||||
|
||||
let required_fields = ["byr:", "iyr:", "eyr:", "hgt:", "hcl:", "ecl:", "pid:"];
|
||||
let valid_count1 =
|
||||
raw_fields.iter().filter(|f|
|
||||
required_fields.iter().all(|h|
|
||||
f.iter().any(|s| s.starts_with(h)))
|
||||
).count();
|
||||
|
||||
println!("Day 4 Part 1: {}", valid_count1);
|
||||
|
||||
let valid_count2 = raw_fields.iter()
|
||||
.filter(|rf| is_valid(rf))
|
||||
.count();
|
||||
|
||||
println!("Day 4 Part 2: {}", valid_count2);
|
||||
|
||||
}
|
||||
|
||||
fn is_valid(fields: &Vec<String>) -> bool {
|
||||
|
||||
return fields.iter().any(|f| f.starts_with("byr:") && is_number_between(&f[4..], 1920, 2002))
|
||||
&& fields.iter().any(|f| f.starts_with("iyr:") && is_number_between(&f[4..], 2010, 2020))
|
||||
&& fields.iter().any(|f| f.starts_with("eyr:") && is_number_between(&f[4..], 2020, 2030))
|
||||
&& fields.iter().any(|f| f.starts_with("hgt:") && is_valid_height(&f[4..]))
|
||||
&& fields.iter().any(|f| f.starts_with("hcl") && is_valid_color(&f[4..]))
|
||||
&& fields.iter().any(|f| f.starts_with("ecl") && is_valid_eye_color(&f[4..]))
|
||||
&& fields.iter().any(|f| f.starts_with("pid") && is_digits(&f[4..]) && f[4..].len() == 9)
|
||||
;
|
||||
}
|
||||
|
||||
fn is_number_between(s: &str, min: i32, max: i32) -> bool {
|
||||
return str::parse::<i32>(s)
|
||||
.map(|n| n >= min && n <= max).unwrap_or(false);
|
||||
}
|
||||
|
||||
fn is_valid_height(s: &str) -> bool {
|
||||
return (s.ends_with("cm") && is_number_between(&s[..(s.len() - 2)], 150, 193))
|
||||
|| (s.ends_with("in") && is_number_between(&s[..(s.len() - 2)], 59, 76));
|
||||
}
|
||||
|
||||
fn is_valid_color(s: &str) -> bool {
|
||||
return s.starts_with("#") &&
|
||||
s[1..].chars().all(|c| (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'));
|
||||
}
|
||||
|
||||
fn is_valid_eye_color(s: &str) -> bool {
|
||||
return VALID_EYE_COLORS.contains(&s);
|
||||
}
|
||||
|
||||
fn is_digits(s: &str) -> bool {
|
||||
return s.chars().all(|c| c.is_ascii_digit());
|
||||
}
|
||||
@@ -5,6 +5,7 @@ mod util;
|
||||
mod day1;
|
||||
mod day2;
|
||||
mod day3;
|
||||
mod day4;
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -18,6 +19,7 @@ fn main() {
|
||||
1 => day1::solve(),
|
||||
2 => day2::solve(),
|
||||
3 => day3::solve(),
|
||||
4 => day4::solve(),
|
||||
_ => println!("This day is not yet implemented")
|
||||
}
|
||||
} else {
|
||||
@@ -25,6 +27,7 @@ fn main() {
|
||||
day1::solve();
|
||||
day2::solve();
|
||||
day3::solve();
|
||||
day4::solve();
|
||||
}
|
||||
|
||||
println!("Execution took {} μs", now.elapsed().as_micros());
|
||||
|
||||
Reference in New Issue
Block a user