-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
203 lines (172 loc) · 4.88 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
use std::{
collections::{HashMap, HashSet},
fmt::Display,
hash::Hash,
str::FromStr,
};
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one(data));
println!("Part 2: {}", part_two(data));
}
fn part_one(data: &str) -> u16 {
let mut banks: MemoryBanks = data.parse().unwrap();
//let mut set: HashSet<MemoryBanks, _> = HashSet::with_hasher(FnvHasherBuilder::default());
let mut set: HashSet<MemoryBanks> = HashSet::new();
set.insert(banks);
for i in 0.. {
banks.step();
if set.contains(&banks) {
return i + 1;
}
set.insert(banks);
}
0
}
fn part_two(data: &str) -> u16 {
let mut banks: MemoryBanks = data.parse().unwrap();
//let mut set: HashSet<MemoryBanks, _> = HashSet::with_hasher(FnvHasherBuilder::default());
let mut set: HashMap<MemoryBanks, u16> = HashMap::new();
set.insert(banks, 0);
for i in 0.. {
banks.step();
if set.contains_key(&banks) {
return i - set.get(&banks).unwrap();
}
set.insert(banks, i);
}
0
}
#[derive(Clone, Copy)]
struct Location(u8);
impl From<u8> for Location {
fn from(value: u8) -> Self {
Self(value)
}
}
impl Display for Location {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)?;
Ok(())
}
}
#[derive(Clone, Copy)]
struct Blocks(u8);
impl From<u8> for Blocks {
fn from(value: u8) -> Self {
Self(value)
}
}
impl Display for Blocks {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)?;
Ok(())
}
}
const BLOCK_MAP: u128 = 0xff;
const BLOCK_SIZE: u8 = 8;
// The 16 banks each take 8 bits of the u128 data
// to indicate how many blocks they hold
// This means that each bank can hold up to 256 blocks,
// which is more than we need as there are only 112 blocks
// in total in the input data
//
// Sadly I couldn't get away with a u64, which would have
// meant 4 bits for each bank as I could see some of the
// banks would go over 15 in value
#[derive(Eq, Clone, Copy)]
struct MemoryBanks {
data: u128,
size: u8,
}
impl MemoryBanks {
fn max(&self) -> (Location, Blocks) {
let mut data = self.data;
let mut loc = 0.into();
let mut val: Blocks = 0.into();
for i in 0..self.size {
let block_val = u8::try_from(data & BLOCK_MAP).unwrap();
if block_val > val.0 {
loc = i.into();
val = block_val.into();
}
data >>= BLOCK_SIZE;
}
(loc, val)
}
fn allocate_from(&mut self, loc: Location, num: Blocks) {
let base = u128::from(num.0 / self.size);
let rem = num.0 % self.size;
let rel_index = |i: u8| ((i + self.size) - loc.0 - 1) % self.size;
let allocated = |i: u8| base + u128::from(rel_index(i) < rem);
let mut temp = self.data;
self.data = 0_u128;
for i in 0..self.size {
let start = if loc.0 == i { 0 } else { temp & BLOCK_MAP };
temp >>= BLOCK_SIZE;
let new = start + allocated(i);
self.data |= new << (BLOCK_SIZE * i);
}
}
fn step(&mut self) {
let (loc, num) = self.max();
self.allocate_from(loc, num);
}
}
impl Hash for MemoryBanks {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.data.hash(state);
}
}
impl PartialEq for MemoryBanks {
fn eq(&self, other: &Self) -> bool {
self.data == other.data
}
}
impl FromStr for MemoryBanks {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut data = 0_u128;
let mut size = 0;
for (i, val) in s.split_ascii_whitespace().rev().enumerate() {
if i >= 16 {
return Err(
"More thank 16 parts to the input, which is not accomodated for".to_owned(),
);
}
data <<= BLOCK_SIZE;
let val: u128 = val
.parse()
.map_err(|_| "Cannot parse '{val}' into a positive number")?;
data |= val;
size += 1;
}
Ok(Self { data, size })
}
}
impl Display for MemoryBanks {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut temp = self.data;
for _ in 0..self.size {
let val = temp & BLOCK_MAP;
temp >>= BLOCK_SIZE;
write!(f, "{val} ")?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
assert_eq!(5, part_one(data));
}
#[test]
fn two() {
let data = include_str!("test.txt");
assert_eq!(4, part_two(data));
}
}