Skip to content

Commit c0390c7

Browse files
committed
Day 15 and Day 16
1 parent dc35a35 commit c0390c7

File tree

10 files changed

+415
-12
lines changed

10 files changed

+415
-12
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ members = [
4545
"y23/d12-hot-springs",
4646
"y23/d13-point-of-incidence",
4747
"y23/d14-parabolic-reflector-dish",
48-
"y23/d15",
49-
"y23/d16",
48+
"y23/d15-lens-library",
49+
"y23/d16-the-floor-will-be-lava",
5050
"y23/d17",
5151
"y23/d18",
5252
"y23/d19",

y23/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Graph Cycle, Shoelace Theorem
4848

4949
### Day 11 - Cosmic Expansion
5050

51-
A very sparse matrix
51+
A very sparse graph
5252

5353
### Day 12 - Hot Springs
5454

@@ -60,12 +60,16 @@ Dynamic Programming
6060

6161
### Day 14 - Parabolic Reflector Dish
6262

63-
[Falling rocks](../y22/README.md#day-14), cycle detection
63+
[Falling rocks](../y22/README.md#day-14), cycle detection, hoping hashes don't collide
6464

65-
### Day 15
65+
### Day 15 - Lens Library
66+
67+
Homemade `HashMap`
6668

6769
### Day 16
6870

71+
BFS
72+
6973
### Day 17
7074

7175
### Day 18

y23/d14-parabolic-reflector-dish/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ edition = "2021"
88
[dependencies]
99
aoc = { path = "../../aoc" }
1010
itertools = "0.12.0"
11-
nohash-hasher = "0.2.0"
11+
nohash-hasher = "0.2.0"
File renamed without changes.

y23/d15-lens-library/src/main.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use std::collections::VecDeque;
2+
3+
use aoc::input_str;
4+
5+
fn hash(s: &str) -> u8 {
6+
s.bytes().fold(0, |acc, b| (acc + b) * 17)
7+
}
8+
9+
fn part1(input: &str) -> u32 {
10+
input
11+
.split(',')
12+
.map(|s| s.trim())
13+
.map(hash)
14+
.map(|h| h as u32)
15+
.sum()
16+
}
17+
18+
#[derive(Debug, Clone, Copy, Default)]
19+
struct Value<'a> {
20+
label: &'a str,
21+
value: u8,
22+
}
23+
24+
struct Hashmap<'a> {
25+
buckets: Vec<VecDeque<Value<'a>>>,
26+
}
27+
28+
impl<'a> Hashmap<'a> {
29+
fn new() -> Self {
30+
Self {
31+
buckets: vec![VecDeque::new(); 256],
32+
}
33+
}
34+
35+
fn get_bucket(&mut self, label: &'a str) -> &mut VecDeque<Value<'a>> {
36+
&mut self.buckets[hash(label) as usize]
37+
}
38+
39+
fn insert(&mut self, label: &'a str, value: u8) {
40+
let bucket = self.get_bucket(label);
41+
if let Some(v) = bucket.iter_mut().find(|v| v.label == label) {
42+
v.value = value;
43+
} else {
44+
bucket.push_back(Value { label, value });
45+
}
46+
}
47+
48+
fn remove(&mut self, label: &'a str) {
49+
let bucket = self.get_bucket(label);
50+
if let Some(pos) = bucket.iter().position(|v| v.label == label) {
51+
bucket.remove(pos);
52+
}
53+
}
54+
55+
fn score(&self) -> u32 {
56+
self.buckets
57+
.iter()
58+
.zip(1..)
59+
.flat_map(|(b, i)| {
60+
b.iter()
61+
.zip(1..)
62+
.map(move |(v, j)| (v.value as u32) * i * j)
63+
})
64+
.sum::<u32>()
65+
}
66+
}
67+
68+
fn part2(input: &str) -> u32 {
69+
let instructions = input.split(',').map(|s| s.trim());
70+
71+
// Instruction is either
72+
// - "label=value" (set label)
73+
// or
74+
// - "label-" (remove label)
75+
let mut hashmap = Hashmap::new();
76+
for instruction in instructions {
77+
if let Some(label) = &instruction.strip_suffix('-') {
78+
hashmap.remove(label);
79+
} else {
80+
let mut parts = instruction.split('=');
81+
let label = parts.next().unwrap();
82+
let value = parts.next().unwrap().parse::<u8>().unwrap();
83+
hashmap.insert(label, value);
84+
}
85+
}
86+
hashmap.score()
87+
}
88+
89+
fn main() {
90+
let input = input_str!(2023, 15);
91+
println!("Part 1: {}", part1(input));
92+
println!("Part 2: {}", part2(input));
93+
}

y23/d15/src/main.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

y23/d16/Cargo.toml renamed to y23/d16-the-floor-will-be-lava/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ edition = "2021"
77

88
[dependencies]
99
aoc = { path = "../../aoc" }
10+
rayon = "1.8.0"

0 commit comments

Comments
 (0)