Skip to content
Snippets Groups Projects
Commit 6faf8212 authored by Falk Rehse's avatar Falk Rehse
Browse files

Switch day 4 to HashMap

parent ffe6e195
No related branches found
No related tags found
No related merge requests found
...@@ -6,7 +6,6 @@ version = 3 ...@@ -6,7 +6,6 @@ version = 3
name = "advent-of-code-23" name = "advent-of-code-23"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"dict",
"regex", "regex",
] ]
...@@ -19,12 +18,6 @@ dependencies = [ ...@@ -19,12 +18,6 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "dict"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39fbb71fba1e2c935f51b056de0d812d9b625b8165b2d7f8848902080fbdffb4"
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.6.4" version = "2.6.4"
......
...@@ -6,5 +6,4 @@ edition = "2021" ...@@ -6,5 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
dict = "0.1.5"
regex = "1.10.2" regex = "1.10.2"
use crate::util::*; use std::collections::HashMap;
use dict::{Dict, DictIface}; use crate::util::*;
pub fn solve_from_str(input: &str) -> u32 { pub fn solve_from_str(input: &str) -> u32 {
let mut copies: Dict<u32> = Dict::new(); let mut copies: HashMap<usize, u32> = HashMap::new();
let total_cards = input.lines().count(); let total_cards = input.lines().count();
...@@ -18,20 +18,19 @@ pub fn solve_from_str(input: &str) -> u32 { ...@@ -18,20 +18,19 @@ pub fn solve_from_str(input: &str) -> u32 {
.map(|number| if winning.contains(number) { 1 } else { 0 }) .map(|number| if winning.contains(number) { 1 } else { 0 })
.sum::<u32>(); .sum::<u32>();
let multiplier = if !copies.contains_key(&i.to_string()) { let multiplier = if !copies.contains_key(&i) {
copies.add(i.to_string(), 1); copies.insert(i, 1);
1 1
} else { } else {
*copies.get(&i.to_string()).unwrap() *copies.get(&i).unwrap()
}; };
for k in i + 1..usize::min(i + amount as usize + 1, total_cards + 1) as usize { for k in i + 1..usize::min(i + amount as usize + 1, total_cards + 1) as usize {
let current_multiplier = copies.get(&k.to_string()).copied().unwrap_or(1); let current_multiplier = copies.get(&k).copied().unwrap_or(1);
copies.remove_key(&k.to_string()); copies.insert(k, current_multiplier + multiplier);
copies.add(k.to_string(), current_multiplier + multiplier);
} }
}); });
copies.iter().map(|card| card.val).sum() copies.iter().map(|card| card.1).sum()
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment