This commit is contained in:
JonOfUs 2023-12-15 16:06:12 +01:00
parent c2e2d2930a
commit 5000ae7344
4 changed files with 67 additions and 2 deletions

1
res/15/example 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
res/15/input Normal file

File diff suppressed because one or more lines are too long

59
src/days/d15.rs Normal file
View file

@ -0,0 +1,59 @@
use std::{collections::HashMap, fs};
pub fn solve() {
let path = "res/15/input";
let mut contents = fs::read_to_string(path).expect("I/O error, wrong path?");
//let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
contents.retain(|c| !c.is_whitespace());
let result: usize = contents
.split(',')
.map(|p| p.chars().fold(0, |x, y| ((x + y as usize) * 17) % 256))
.sum();
println!("Result 1: {result}");
let mut boxes: HashMap<usize, Vec<(&str, usize)>> = HashMap::new();
for p in contents.split(',') {
let (t, n) = match p.contains('=') {
true => {
let (t, n) = p.split_once('=').unwrap();
(t, n.parse::<usize>().unwrap())
}
false => {
let (t, _) = p.split_once('-').unwrap();
(t, 0usize)
}
};
let i = t.chars().fold(0, |x, y| ((x + y as usize) * 17) % 256);
let v = boxes.entry(i).or_insert(vec![]);
if n == 0 {
if v.iter().any(|(p, _)| *p == t) {
v.remove(v.iter().position(|(p, _)| *p == t).unwrap());
}
} else {
if v.iter().any(|(p, _)| *p == t) {
let pos = v.iter().position(|(p, _)| *p == t).unwrap();
v[pos] = (t, n);
} else {
v.push((t, n))
}
}
}
dbg!(&boxes);
let result: usize = boxes
.iter()
.map(|(k, v)| {
v.iter()
.enumerate()
.map(|(i, (_, n))| (k + 1) * (i + 1) * n)
.sum::<usize>()
})
.sum();
println!("Result 2: {result}");
}

View file

@ -5,8 +5,8 @@ extern crate scan_fmt;
use std::time::Instant; use std::time::Instant;
fn main() { fn main() {
//days::d14::solve() days::d15::solve()
_all_days() //_all_days()
} }
fn _all_days() { fn _all_days() {
@ -66,6 +66,10 @@ fn _all_days() {
println!("\nDay 14"); println!("\nDay 14");
days::d14::solve(); days::d14::solve();
print_elapsed(&start); print_elapsed(&start);
println!("\nDay 15");
days::d15::solve();
print_elapsed(&start);
} }
fn print_elapsed(start: &Instant) { fn print_elapsed(start: &Instant) {