Day 4 part 2
It passes the tests but the real solution is incorrect :(
This commit is contained in:
parent
dc1211cae2
commit
3f74dcb73e
25
src/day4.rs
25
src/day4.rs
@ -65,6 +65,10 @@ pub fn solve_part1() -> Result<u32, Box<Error>> {
|
||||
Ok(get_part1(INPUT)?)
|
||||
}
|
||||
|
||||
pub fn solve_part2() -> Result<u32, Box<Error>> {
|
||||
Ok(get_part2(INPUT)?)
|
||||
}
|
||||
|
||||
fn get_part1(filename: &str) -> Result<u32, Box<Error>> {
|
||||
let records = read_records(filename)?;
|
||||
let minutes_asleep = minutes_asleep_per_guard(records);
|
||||
@ -73,6 +77,22 @@ fn get_part1(filename: &str) -> Result<u32, Box<Error>> {
|
||||
Ok(sleepiest_guard.0 * sleepiest_minute)
|
||||
}
|
||||
|
||||
fn get_part2(filename: &str) -> Result<u32, Box<Error>> {
|
||||
let records = read_records(filename)?;
|
||||
let minutes_asleep = minutes_asleep_per_guard(records);
|
||||
let all_mins: Vec<u32> = minutes_asleep
|
||||
.values()
|
||||
.flat_map(|mins| mins.iter())
|
||||
.cloned()
|
||||
.collect();
|
||||
let sleepiest_minute = mode(&all_mins[..]);
|
||||
let sleepiest_guard = minutes_asleep
|
||||
.iter()
|
||||
.max_by_key(|(_, mins)| mins.into_iter().filter(|min| **min == sleepiest_minute).count())
|
||||
.unwrap();
|
||||
Ok(sleepiest_guard.0 * sleepiest_minute)
|
||||
}
|
||||
|
||||
fn mode(numbers: &[u32]) -> u32 {
|
||||
let mut occurences = HashMap::new();
|
||||
for &value in numbers {
|
||||
@ -283,4 +303,9 @@ mod tests {
|
||||
fn solves_part1() {
|
||||
assert_eq!(get_part1(TEST_INPUT).unwrap(), 240);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solves_part2() {
|
||||
assert_eq!(get_part2(TEST_INPUT).unwrap(), 4455);
|
||||
}
|
||||
}
|
||||
|
@ -15,4 +15,5 @@ fn main() {
|
||||
println!("{}", day3::solve_part2().unwrap().unwrap());
|
||||
println!("Day 4:");
|
||||
println!("{}", day4::solve_part1().unwrap());
|
||||
println!("{}", day4::solve_part2().unwrap());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user