day15: rust

This commit is contained in:
matthias@arch 2023-12-15 15:21:33 +01:00
parent 7f212eeebc
commit bbf0a59d14
3 changed files with 83 additions and 0 deletions

16
15/README.md Normal file
View File

@ -0,0 +1,16 @@
# [Day 15](https://adventofcode.com/2023/day/15)
:gift::gift::gift::gift::gift::gift::gift::gift::gift::gift::gift::gift::gift::gift::gift:
Today's language: **Rust**
Lines of code: **54**
Execution time: **0,002 s**
What a nice name: Holiday ASCII String Helper Manual Arrangement Procedure
```shell
rustc +nightly day15.rs
./day15
```
<!-- hashmap -->

66
15/day15.rs Normal file
View File

@ -0,0 +1,66 @@
#![feature(extract_if)]
use std::io::{self, BufRead};
use std::collections::LinkedList;
use std::str::FromStr;
// tilt so that all 'O' characters want to DECREMENT their Y index
// using custom `at` function that "remaps" x and y, this function can be used for all directions
fn ascii_hash(s: &str) -> usize {
let mut hash: usize = 0;
for c in s.as_bytes().iter() {
hash += *c as usize;
hash *= 17;
hash %= 256;
}
return hash;
}
fn print(boxes: &Vec<LinkedList<(String, u8)>>) {
for (i, l) in boxes.iter().enumerate() {
if l.is_empty() { continue;}
print!("Box {}: ", i);
for t in l.iter() {
print!("[{} {}] ", t.0, t.1);
}
println!();
}
}
fn main() {
// let input = "example.txt";
let input = "input.txt";
let mut lines = read_lines(&input);
let mut hash_sum: usize = 0;
let mut boxes = vec![LinkedList::<(String, u8)>::new(); 256];
for s in lines.next().expect("Line not ok").unwrap().split(',') {
hash_sum += ascii_hash(&s) as usize;
if *s.as_bytes().last().unwrap() == b'-' {
let hash = ascii_hash(&s[..s.len()-1]);
let _ = boxes[hash].extract_if(|t| t.0 == &s[..s.len()-1]).for_each(drop);
}
else {
let hash = ascii_hash(&s[..s.len()-2]);
let focal_length = s[s.len()-1..].parse::<u8>().unwrap();
if let Some(t) = boxes[hash].iter_mut().filter(|t| t.0 == &s[..s.len()-2]).next() {
t.1 = focal_length;
}
else {
boxes[hash].push_back((String::from_str(&s[..s.len()-2]).expect("String err"), focal_length));
}
}
// println!("\nAfter \"{}\"", s);
// print(&boxes);
}
println!("Hash sum (1): {}", hash_sum);
println!("Focus power sum (2): {}", boxes.iter().enumerate().map(|(b, l)| l.iter().enumerate().map(|(i, t)| (b+1) * (i+1) * t.1 as usize).sum::<usize>()).sum::<usize>());
}
fn read_lines<P>(filename: P) -> io::Lines<io::BufReader<std::fs::File>>
where P: AsRef<std::path::Path>, {
return match std::fs::File::open(filename) {
Err(why) => panic!("Could not open file. {}", why),
Ok(file) => std::io::BufReader::new(file).lines()
};
}

View File

@ -17,5 +17,6 @@ This is a repository for my solutions for the [advent of code 2023](https://adve
| [11](11) | Rust | 66 | 0,005 s | |
| [13](13) | Rust | 84 | 0,002 s | binary encoding ftw |
| [14](14) | Rust | 85 | 0,172 s | no bruteforce in < 90 lines |
| [15](15) | Rust | 54 | 0,002 s | hashmap |
Lines of code are without blank lines and comments