Skip to content

Commit

Permalink
Year 2018 Day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
maneatingape committed Jul 19, 2024
1 parent 4e9e79d commit 7e06d83
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
| 1 | [Chronal Calibration](https://adventofcode.com/2018/day/1) | [Source](src/year2018/day01.rs) | 15 |
| 2 | [Inventory Management System](https://adventofcode.com/2018/day/2) | [Source](src/year2018/day02.rs) | 77 |
| 3 | [No Matter How You Slice It](https://adventofcode.com/2018/day/3) | [Source](src/year2018/day03.rs) | 56 |
| 4 | [Repose Record](https://adventofcode.com/2018/day/4) | [Source](src/year2018/day04.rs) | 46 |

## 2017

Expand Down
1 change: 1 addition & 0 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ mod year2018 {
benchmark!(year2018, day01);
benchmark!(year2018, day02);
benchmark!(year2018, day03);
benchmark!(year2018, day04);
}

mod year2019 {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ pub mod year2018 {
pub mod day01;
pub mod day02;
pub mod day03;
pub mod day04;
}

/// # Rescue Santa from deep space with a solar system adventure.
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,12 @@ fn year2017() -> Vec<Solution> {
}

fn year2018() -> Vec<Solution> {
vec![solution!(year2018, day01), solution!(year2018, day02), solution!(year2018, day03)]
vec![
solution!(year2018, day01),
solution!(year2018, day02),
solution!(year2018, day03),
solution!(year2018, day04),
]
}

fn year2019() -> Vec<Solution> {
Expand Down
53 changes: 53 additions & 0 deletions src/year2018/day04.rs
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)
}
1 change: 1 addition & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ mod year2018 {
mod day01_test;
mod day02_test;
mod day03_test;
mod day04_test;
}

mod year2019 {
Expand Down
32 changes: 32 additions & 0 deletions tests/year2018/day04_test.rs
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);
}

0 comments on commit 7e06d83

Please sign in to comment.