diff --git a/README.md b/README.md index 87a053d..678e4be 100644 --- a/README.md +++ b/README.md @@ -43,9 +43,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www. | [Day 11](./src/bin/11.rs) | `16.3µs` | `15.7µs` | | [Day 12](./src/bin/12.rs) | `137.9µs` | `618.3µs` | | [Day 13](./src/bin/13.rs) | `12.3µs` | `15.9µs` | -| [Day 14](./src/bin/14.rs) | `26.1µs` | `14.6ms` | +| [Day 14](./src/bin/14.rs) | `26.3µs` | `12.6ms` | -**Total: 17.01ms** +**Total: 15.01ms** --- diff --git a/src/bin/14.rs b/src/bin/14.rs index ddac75f..58bde9e 100644 --- a/src/bin/14.rs +++ b/src/bin/14.rs @@ -28,15 +28,14 @@ pub fn part_one(input: &str) -> Option { pub fn part_two(input: &str) -> Option { let mut platform = input .lines() - .map(|line| line.chars().collect::>()) + .map(|line| line.as_bytes().to_vec()) .collect::>(); let mut count = 0u64; let mut seen = HashMap::new(); loop { cycle(&mut platform); count += 1; - let state = print(&platform); - match seen.entry(state) { + match seen.entry(stringify(&platform)) { Entry::Occupied(val) => { let cycle_length = count - *val.get(); let remainder = 1_000_000_000 @@ -56,93 +55,90 @@ pub fn part_two(input: &str) -> Option { Some(load(&platform)) } -fn north(platform: &mut [Vec]) -> u32 { - let mut score = 0; +fn north(platform: &mut [Vec]) { for col in 0..platform[0].len() { let mut empty_spaces = 0; for row in 0..platform.len() { match platform[row][col] { - '.' => empty_spaces += 1, - '#' => empty_spaces = 0, - 'O' => { + b'.' => empty_spaces += 1, + b'#' => empty_spaces = 0, + b'O' if empty_spaces > 0 => { let new_row = row - empty_spaces; - platform[row][col] = '.'; - platform[new_row][col] = 'O'; - score += platform[0].len() - new_row; + platform[row][col] = b'.'; + platform[new_row][col] = b'O'; } - _ => unreachable!(), + _ => {} } } } - score as u32 } -fn west(platform: &mut [Vec]) { +fn west(platform: &mut [Vec]) { for row in 0..platform.len() { let mut empty_spaces = 0; for col in 0..platform[0].len() { match platform[row][col] { - '.' => empty_spaces += 1, - '#' => empty_spaces = 0, - 'O' => { + b'.' => empty_spaces += 1, + b'#' => empty_spaces = 0, + b'O' if empty_spaces > 0 => { let new_col = col - empty_spaces; - platform[row][col] = '.'; - platform[row][new_col] = 'O'; + platform[row][col] = b'.'; + platform[row][new_col] = b'O'; } - _ => unreachable!(), + _ => {} } } } } -fn south(platform: &mut [Vec]) { +fn south(platform: &mut [Vec]) { for col in 0..platform[0].len() { let mut empty_spaces = 0; for row in (0..platform.len()).rev() { match platform[row][col] { - '.' => empty_spaces += 1, - '#' => empty_spaces = 0, - 'O' => { + b'.' => empty_spaces += 1, + b'#' => empty_spaces = 0, + b'O' if empty_spaces > 0 => { let new_row = row + empty_spaces; - platform[row][col] = '.'; - platform[new_row][col] = 'O'; + platform[row][col] = b'.'; + platform[new_row][col] = b'O'; } - _ => unreachable!(), + _ => {} } } } } -fn east(platform: &mut [Vec]) { +fn east(platform: &mut [Vec]) { for row in 0..platform.len() { let mut empty_spaces = 0; for col in (0..platform[0].len()).rev() { match platform[row][col] { - '.' => empty_spaces += 1, - '#' => empty_spaces = 0, - 'O' => { + b'.' => empty_spaces += 1, + b'#' => empty_spaces = 0, + b'O' if empty_spaces > 0 => { let new_col = col + empty_spaces; - platform[row][col] = '.'; - platform[row][new_col] = 'O'; + platform[row][col] = b'.'; + platform[row][new_col] = b'O'; } - _ => unreachable!(), + _ => {} } } } } -fn cycle(platform: &mut [Vec]) { +fn cycle(platform: &mut [Vec]) { north(platform); west(platform); south(platform); east(platform); } -fn load(platform: &Vec>) -> u32 { +fn load(platform: &[Vec]) -> u32 { let mut score = 0; for (row_idx, row) in platform.iter().enumerate() { for ch in row.iter().copied() { - if ch == 'O' { + if ch == b'O' { score += platform.len() - row_idx; } } @@ -150,10 +146,12 @@ fn load(platform: &Vec>) -> u32 { score as u32 } -fn print(platform: &[Vec]) -> String { +fn stringify(platform: &[Vec]) -> String { platform .iter() - .flat_map(|r| r.iter().chain(std::iter::once(&'\n'))) + .flat_map(|r| r.iter()) + .copied() + .map(char::from) .collect::() }