-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e9e79d
commit 7e06d83
Showing
7 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//! # Repose Record | ||
use crate::util::hash::*; | ||
use crate::util::parse::*; | ||
|
||
type Input = FastMap<usize, [u32; 60]>; | ||
|
||
pub fn parse(input: &str) -> Input { | ||
// Records need to be in chronological order. | ||
let mut records: Vec<_> = input.lines().map(str::as_bytes).collect(); | ||
records.sort_unstable(); | ||
|
||
// Build each sleep schedule | ||
let mut id = 0; | ||
let mut start = 0; | ||
let mut guards = FastMap::new(); | ||
|
||
for record in records { | ||
match record.len() { | ||
31 => start = to_index(&record[15..17]), | ||
27 => { | ||
let end = to_index(&record[15..17]); | ||
let minutes = guards.entry(id).or_insert_with(|| [0; 60]); | ||
(start..end).for_each(|i| minutes[i] += 1); | ||
} | ||
_ => id = to_index(&record[26..record.len() - 13]), | ||
} | ||
} | ||
|
||
guards | ||
} | ||
|
||
/// Find the guard with the greatest total minutes asleep. | ||
pub fn part1(input: &Input) -> usize { | ||
choose(input, |(_, m)| m.iter().sum()) | ||
} | ||
|
||
/// Find the guard with the highest single minute asleep. | ||
pub fn part2(input: &Input) -> usize { | ||
choose(input, |(_, m)| *m.iter().max().unwrap()) | ||
} | ||
|
||
fn choose(input: &Input, strategy: impl Fn(&(&usize, &[u32; 60])) -> u32) -> usize { | ||
// Find the guard using a specific strategy. | ||
let (id, minutes) = input.iter().max_by_key(strategy).unwrap(); | ||
// Find the minute spent asleep the most | ||
let (minute, _) = minutes.iter().enumerate().max_by_key(|(_, m)| **m).unwrap(); | ||
// Return result | ||
id * minute | ||
} | ||
|
||
fn to_index(slice: &[u8]) -> usize { | ||
slice.iter().fold(0, |acc, n| 10 * acc + n.to_decimal() as usize) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,6 +116,7 @@ mod year2018 { | |
mod day01_test; | ||
mod day02_test; | ||
mod day03_test; | ||
mod day04_test; | ||
} | ||
|
||
mod year2019 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use aoc::year2018::day04::*; | ||
|
||
const EXAMPLE: &str = "\ | ||
[1518-11-01 00:00] Guard #10 begins shift | ||
[1518-11-01 00:05] falls asleep | ||
[1518-11-01 00:25] wakes up | ||
[1518-11-01 00:30] falls asleep | ||
[1518-11-01 00:55] wakes up | ||
[1518-11-01 23:58] Guard #99 begins shift | ||
[1518-11-02 00:40] falls asleep | ||
[1518-11-02 00:50] wakes up | ||
[1518-11-03 00:05] Guard #10 begins shift | ||
[1518-11-03 00:24] falls asleep | ||
[1518-11-03 00:29] wakes up | ||
[1518-11-04 00:02] Guard #99 begins shift | ||
[1518-11-04 00:36] falls asleep | ||
[1518-11-04 00:46] wakes up | ||
[1518-11-05 00:03] Guard #99 begins shift | ||
[1518-11-05 00:45] falls asleep | ||
[1518-11-05 00:55] wakes up"; | ||
|
||
#[test] | ||
fn part1_test() { | ||
let input = parse(EXAMPLE); | ||
assert_eq!(part1(&input), 240); | ||
} | ||
|
||
#[test] | ||
fn part2_test() { | ||
let input = parse(EXAMPLE); | ||
assert_eq!(part2(&input), 4455); | ||
} |