Skip to content

Commit

Permalink
faster day15 part1
Browse files Browse the repository at this point in the history
  • Loading branch information
kcaffrey committed Dec 15, 2023
1 parent 59539c8 commit 88f7a8a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
<!--- benchmarking table --->

---
Expand Down
24 changes: 12 additions & 12 deletions src/bin/15.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
advent_of_code::solution!(15);

pub fn part_one(input: &str) -> Option<u32> {
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<u32> {
Expand Down

0 comments on commit 88f7a8a

Please sign in to comment.