From 4477e24c62e3c010ce3d177c632b052d906e011b Mon Sep 17 00:00:00 2001 From: Raphael Goulais Date: Fri, 15 Dec 2023 07:05:58 +0100 Subject: [PATCH] feat: Day 15 --- README.md | 3 +- data/examples/15.txt | 1 + src/bin/15.rs | 94 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 data/examples/15.txt create mode 100644 src/bin/15.rs diff --git a/README.md b/README.md index 6cfea14..a321ed7 100644 --- a/README.md +++ b/README.md @@ -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** --- diff --git a/data/examples/15.txt b/data/examples/15.txt new file mode 100644 index 0000000..62f7ed0 --- /dev/null +++ b/data/examples/15.txt @@ -0,0 +1 @@ +rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7 \ No newline at end of file diff --git a/src/bin/15.rs b/src/bin/15.rs new file mode 100644 index 0000000..3387803 --- /dev/null +++ b/src/bin/15.rs @@ -0,0 +1,94 @@ +advent_of_code::solution!(15); + +use std::collections::HashMap; + +pub struct Boxes { + keys: Vec, + map: HashMap, +} + +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 { + 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 { + 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::().unwrap()); + } + } + let mut slots: HashMap = 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)); + } +}