1
0
Fork 0

Added Day 5 Part 1

master
Ishan Jain 2 years ago
parent 992a5586ec
commit 2c6be28d86
No known key found for this signature in database
GPG Key ID: F261A0E73038D89D

2003
input

File diff suppressed because it is too large Load Diff

@ -1,13 +1,4 @@
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
FBFBBFFRLR
BFFFBBFRRR
FFFBBBFRRR
BBFFBBFRLL

@ -1,88 +1,58 @@
use std::collections::HashMap;
use std::io::{stdin, Read};
fn main() {
let mut input = String::new();
stdin().read_to_string(&mut input).unwrap();
let passports: usize = input
.split("\n\n")
let max_seat_id: usize = input
.split('\n')
.filter(|x| !x.is_empty())
.into_iter()
.map(|x| x.replace('\n', " "))
.map(|x| {
let mut map = HashMap::new();
.map(|seat| {
let seat = seat.as_bytes();
let (region, columns) = seat.split_at(7);
x.split(' ').filter(|x| !x.is_empty()).for_each(|y| {
let mut y = y.split(':');
let key = y.next().unwrap();
let value = y.next().unwrap();
let mut lower = 0;
let mut upper = 127;
map.insert(key.to_owned(), value.to_owned());
});
map
})
.filter(|map| {
validate(&map, "byr")
&& validate(&map, "iyr")
&& validate(&map, "eyr")
&& validate(&map, "hgt")
&& validate(&map, "hcl")
&& validate(&map, "ecl")
&& validate(&map, "pid")
})
.count();
for r in region {
match r {
b'F' => {
let mid = (lower + upper) / 2;
upper = mid;
}
b'B' => {
let mid = (lower + upper + 1) / 2;
lower = mid;
}
_ => unreachable!(),
}
}
println!("{:?}", passports);
}
let region = lower;
lower = 0;
upper = 7;
fn validate(map: &HashMap<String, String>, key: &str) -> bool {
match (key, map.get(key)) {
("byr", Some(v)) => {
let byr = v.parse::<i32>().unwrap();
byr >= 1920 && byr <= 2002
}
("iyr", Some(v)) => {
let iyr = v.parse::<i32>().unwrap();
iyr >= 2010 && iyr <= 2020
}
("eyr", Some(v)) => {
let eyr = v.parse::<i32>().unwrap();
eyr >= 2020 && eyr <= 2030
}
("hgt", Some(v)) => {
if v.contains("cm") {
let v = v.trim_end_matches(char::is_alphabetic);
let v = v.parse::<i32>().unwrap();
v >= 150 && v <= 193
} else if v.contains("in") {
let v = v.trim_end_matches(char::is_alphabetic);
let v = v.parse::<i32>().unwrap();
v >= 59 && v <= 76
} else {
false
for col in columns {
match col {
b'L' => {
let mid = (lower + upper) / 2;
upper = mid;
}
b'R' => {
let mid = (lower + upper + 1) / 2;
lower = mid;
}
_ => unreachable!(),
}
}
}
("hcl", Some(v)) => {
v.starts_with('#')
&& v.chars()
.skip(1)
.filter(|x| match x {
'0'..='9' => true,
'a'..='f' => true,
_ => false,
})
.count()
== 6
}
("ecl", Some(v)) => match v.as_ref() {
"amb" | "blu" | "brn" | "gry" | "grn" | "hzl" | "oth" => true,
_ => false,
},
("pid", Some(v)) => v.len() == 9 && v.chars().filter(|&x| char::is_numeric(x)).count() == 9,
("cid", _) => true,
(_, None) => false,
(_, _) => unreachable!(),
}
let col = lower;
region * 8 + col
})
.max()
.unwrap();
println!("{:?}", max_seat_id);
}

Loading…
Cancel
Save