From 88f7a8abba9ab9a66d4e0f49a6898db61eebc3dc Mon Sep 17 00:00:00 2001 From: Kevin Caffrey Date: Fri, 15 Dec 2023 13:39:00 -0500 Subject: [PATCH] faster day15 part1 --- README.md | 4 ++-- src/bin/15.rs | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 0c5e337..1a895f1 100644 --- a/README.md +++ b/README.md @@ -45,9 +45,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www. | [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.4µs` | `4.3ms` | -| [Day 15](./src/bin/15.rs) | `29.0µs` | `143.0µs` | +| [Day 15](./src/bin/15.rs) | `20.4µs` | `143.7µs` | -**Total: 6.88ms** +**Total: 6.87ms** --- diff --git a/src/bin/15.rs b/src/bin/15.rs index 55351fb..9303ad3 100644 --- a/src/bin/15.rs +++ b/src/bin/15.rs @@ -1,18 +1,18 @@ advent_of_code::solution!(15); pub fn part_one(input: &str) -> Option { - Some( - input - .trim() - .as_bytes() - .split(|&ch| ch == b',') - .map(|s| { - s.iter() - .copied() - .fold(0, |acc, ch| ((acc + ch as u32) * 17) % 256) - }) - .sum(), - ) + let mut hash_sum = 0u32; + let mut cur_hash = 0u8; + for ch in input.as_bytes() { + match *ch { + b',' | b'\n' => { + hash_sum += cur_hash as u32; + cur_hash = 0; + } + ch => cur_hash = cur_hash.wrapping_add(ch).wrapping_mul(17), + } + } + Some(hash_sum) } pub fn part_two(input: &str) -> Option {