This commit is contained in:
MaxJa4
2023-12-15 17:17:26 +01:00
parent 043a777732
commit 923ea69669
4 changed files with 140 additions and 0 deletions

18
.vscode/launch.json vendored
View File

@@ -242,6 +242,24 @@
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'part15'",
"cargo": {
"args": [
"build",
"--bin=part15",
"--package=advent-of-code-2023"
],
"filter": {
"name": "part15",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

120
src/bin/part15.rs Normal file
View File

@@ -0,0 +1,120 @@
use std::time;
use std::collections::HashMap;
#[derive(Debug, PartialEq, Eq, Hash)]
struct Lens {
label: String,
focal_length: Option<u8>,
}
fn main() {
let t1 = time::Instant::now();
let data = load_input1();
let d1 = t1.elapsed();
println!("Loading: {:?}", d1);
let t2 = time::Instant::now();
let sum = data.iter().map(|d| *d as u32).sum::<u32>();
let d2 = t2.elapsed();
println!("Summing: {:?}", d2);
println!("Total: {:?}", d1 + d2);
println!("Sum: {}", sum);
let t3 = time::Instant::now();
let lenses = load_input2();
let mut boxes: HashMap<u8, Vec<Lens>> = HashMap::new();
for lens in lenses {
let box_id: u8 = hash(&lens.label);
if lens.focal_length.is_none() { // - Operation
if let Some(relevant_box) = boxes.get_mut(&box_id) {
relevant_box.retain(|l| *l != lens);
if relevant_box.is_empty() {
boxes.remove(&box_id);
}
}
} else { // = Operation
if let Some(relevant_box) = boxes.get_mut(&box_id) {
if let Some(relevant_lens) = relevant_box.iter_mut().find(|l| l.label == lens.label) {
relevant_lens.focal_length = lens.focal_length;
} else {
relevant_box.push(lens);
}
} else {
boxes.insert(box_id, vec![lens]);
}
}
}
let d3 = t3.elapsed();
println!("Hashing: {:?}", d3);
let t4 = time::Instant::now();
let mut total: u32 = 0;
for b in boxes {
for (i, l) in b.1.iter().enumerate() {
let mut product: u32 = 1 + b.0 as u32;
product *= 1 + i as u32;
product *= l.focal_length.unwrap() as u32;
total += product;
}
}
let d4 = t4.elapsed();
println!("Summing: {:?}", d4);
println!("Total: {:?}", d3 + d4);
println!("Result: {}", total);
}
fn hash(s: &str) -> u8 {
let mut h: u32 = 0;
for c in s.chars() {
h += c as u32;
h *= 17;
h = h % 256;
}
h as u8
}
fn load_input1() -> Vec<u8> {
let data = std::fs::read_to_string("src/bin/part15.txt")
.expect("Could not read file")
.split(',')
.map(|l| {
hash(l)
})
.collect::<Vec<_>>();
data
}
fn load_input2() -> Vec<Lens> {
let data: Vec<Lens> = std::fs::read_to_string("src/bin/part15.txt")
.expect("Could not read file")
.split(',')
.map(|l| {
if l.contains('-') {
return Lens{
label: l.split('-').collect::<Vec<_>>()[0].to_string(),
focal_length: None
}
} else if l.contains('=') {
let parts = l.split('=').collect::<Vec<_>>();
return Lens {
label: parts[0].to_string(),
focal_length: Some(parts[1].parse::<u8>().unwrap())
}
} else {
panic!("Invalid input");
}
})
.collect::<Vec<_>>();
data
}

1
src/bin/part15.sample Normal file
View File

@@ -0,0 +1 @@
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7

1
src/bin/part15.txt Normal file

File diff suppressed because one or more lines are too long