Skip to content

Commit

Permalink
feat: Day 15
Browse files Browse the repository at this point in the history
  • Loading branch information
rgoulais committed Dec 15, 2023
1 parent b880731 commit 4477e24
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
| [Day 12](./src/bin/12.rs) | `358.8µs` | `770.0µs` |
| [Day 13](./src/bin/13.rs) | `412.9µs` | `894.6µs` |
| [Day 14](./src/bin/14.rs) | `39.9µs` | `26.5ms` |
| [Day 15](./src/bin/15.rs) | `52.4µs` | `1.3ms` |

**Total: 29.17ms**
**Total: 30.52ms**
<!--- benchmarking table --->

---
Expand Down
1 change: 1 addition & 0 deletions data/examples/15.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7
94 changes: 94 additions & 0 deletions src/bin/15.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
advent_of_code::solution!(15);

use std::collections::HashMap;

pub struct Boxes {
keys: Vec<String>,
map: HashMap<String, u32>,
}

impl Boxes {
pub fn new() -> Self {
Self {
keys: Vec::new(),
map: HashMap::new(),
}
}

pub fn insert(&mut self, key: String, value: u32) {
if !self.map.contains_key(&key) {
self.keys.push(key.clone());
}
self.map.insert(key, value);
}

pub fn update(&mut self, key: String, value: u32) {
if self.map.contains_key(&key) {
self.map.insert(key, value);
}
}

pub fn remove(&mut self, key: &String) {
self.keys.retain(|k| k != key);
self.map.remove(key);
}
}


fn hash_algo(input_string: &str) -> u32 {
let mut current_value: u32 = 0;
for char in input_string.chars() {
let ascii_value = char as u32;
current_value += ascii_value;
current_value *= 17;
current_value %= 256;
}
current_value
}

pub fn part_one(input: &str) -> Option<u32> {
let mut current_value: u32 = 0;
for line in input.split(",") {
current_value += hash_algo(line);
}
Some(current_value)
}

pub fn part_two(input: &str) -> Option<u32> {
let mut current_value: u32 = 0;
let mut boxes = Boxes::new();
for line in input.split(",") {
if line.ends_with('-') {
boxes.remove(&line[0..line.len() - 1].to_string());
} else {
let (key, value) = line.split_at(line.find('=').unwrap());
boxes.insert(key.to_string(), value[1..].parse::<u32>().unwrap());
}
}
let mut slots: HashMap<u32, u32> = HashMap::with_capacity(256);
for key in boxes.keys {
let val = boxes.map.get(&key).unwrap();
let box_number = hash_algo(&key);
let slot = slots.entry(box_number).or_insert(0);
*slot += 1;
current_value += (box_number + 1) * *slot * val;
}
Some(current_value)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(1320));
}

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(145));
}
}

0 comments on commit 4477e24

Please sign in to comment.