day 1: cleanup and refactor

* I misunderstood the problem and thought we were asked for the distance
  between the numbers' positions, not their values. Removed remnants of
  that.
* Move to struct-oriented solution
* Fix clippys and unnecessary code
This commit is contained in:
Keenan Tims 2024-11-30 22:03:19 -08:00
parent 8cab780ff3
commit 968d795720
Signed by: ktims
GPG Key ID: 11230674D69038D4

View File

@ -25,60 +25,58 @@ fn main() {
println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64()); println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64());
} }
// PROBLEM 1 solution struct Locations {
left: Vec<u64>,
#[derive(Debug)] right: Vec<u64>,
struct Entry {
pos: usize,
val: u32,
} }
fn problem1<T: BufRead>(input: Lines<T>) -> u64 { impl<T: BufRead> From<Lines<T>> for Locations {
let mut left = Vec::new(); fn from(input: Lines<T>) -> Self {
let mut right = Vec::new(); let mut left = Vec::new();
for (pos, line) in input.enumerate() { let mut right = Vec::new();
if line.is_err() { for line in input.map(|i| i.unwrap()) {
panic!("can't read line"); let parts: Vec<&str> = line.split_ascii_whitespace().collect();
left.push(parts[0].parse::<u64>().unwrap());
right.push(parts[1].parse::<u64>().unwrap());
} }
let line = line.unwrap(); Self { left, right }
let parts: Vec<&str> = line.split_ascii_whitespace().collect();
left.push(Entry {
pos,
val: parts[0].parse::<u32>().unwrap(),
});
right.push(Entry {
pos,
val: parts[1].parse::<u32>().unwrap(),
});
} }
left.sort_by_key(|entry| entry.val); }
right.sort_by_key(|entry| entry.val);
println!("{:?}", right); impl Locations {
fn sort(&mut self) {
self.left.sort();
self.right.sort();
}
fn right_count(&self) -> HashMap<u64, u64> {
let mut right_count: HashMap<u64, u64> = HashMap::new();
for rval in &self.right {
right_count.insert(*rval, *right_count.get(rval).unwrap_or(&0) + 1);
}
right_count
}
}
zip(left, right) // PROBLEM 1 solution
.map(|(left, right)| {
println!("{:?} {:?}", left, right); fn problem1<T: BufRead>(input: Lines<T>) -> u64 {
u64::abs_diff(left.val as u64, right.val as u64) let mut locations = Locations::from(input);
}) locations.sort();
zip(locations.left, locations.right)
.map(|(l, r)| u64::abs_diff(l, r))
.sum() .sum()
} }
// PROBLEM 2 solution // PROBLEM 2 solution
fn problem2<T: BufRead>(input: Lines<T>) -> u64 { fn problem2<T: BufRead>(input: Lines<T>) -> u64 {
let mut left = Vec::new(); let locations = Locations::from(input);
let mut right_count = HashMap::new(); let right_count = locations.right_count();
for (pos, line) in input.enumerate() { locations
if line.is_err() { .left
panic!("can't read line"); .iter()
} .map(|l| l * right_count.get(l).unwrap_or(&0))
let line = line.unwrap(); .sum::<u64>()
let parts: Vec<&str> = line.split_ascii_whitespace().collect();
left.push(parts[0].parse::<u32>().unwrap());
let right = parts[1].parse::<u32>().unwrap();
right_count.insert(right, *right_count.get(&right).unwrap_or(&0) + 1);
}
left.iter().map(|l| l * right_count.get(l).unwrap_or(&0)).sum::<u32>() as u64
} }
#[cfg(test)] #[cfg(test)]