simplify day 7
This commit is contained in:
parent
19e219bcc3
commit
2133290565
@ -1,59 +1,54 @@
|
|||||||
use color_eyre::eyre;
|
use color_eyre::eyre;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Default)]
|
fn distance_between(a: usize, b: usize) -> usize {
|
||||||
struct Map {
|
// can't do the fancypants (a - b).abs() thing here because these are unsigned
|
||||||
counts: Vec<usize>, // index is position, value is how many at that position
|
if a > b {
|
||||||
index: Vec<usize>, // values are indices of `counts`, sorted from most to least
|
a - b
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
b - a
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Map {
|
|
||||||
fn from(nums: &[usize]) -> Map {
|
|
||||||
let hi = nums.iter().max().unwrap_or(&0);
|
|
||||||
let mut counts = vec![0; hi + 1];
|
|
||||||
for n in nums {
|
|
||||||
counts[*n] += 1;
|
|
||||||
}
|
|
||||||
let mut index: Vec<usize> = (0..counts.len()).collect();
|
|
||||||
index.sort_by(|&a, &b| counts[b].cmp(&counts[a])); // flip the comparison function to sort in reverse
|
|
||||||
|
|
||||||
Map {counts, index}
|
fn cost_simple(counts: &[usize], pos: usize) -> usize {
|
||||||
|
counts.iter()
|
||||||
|
.enumerate()
|
||||||
|
.fold(0, |total, (i, count)| {
|
||||||
|
total + (distance_between(pos, i) * count) // yes, these parens are unnecessary
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cost_simple(&self, pos: usize) -> usize {
|
|
||||||
let mut total = 0;
|
|
||||||
for (i, count) in self.counts.iter().enumerate() {
|
|
||||||
let distance = if pos > i {pos - i} else {i - pos};
|
|
||||||
total += distance * count;
|
|
||||||
}
|
|
||||||
total
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cost_complex(&self, pos: usize) -> usize {
|
fn cost_complex(counts: &[usize], pos: usize) -> usize {
|
||||||
let mut total = 0;
|
counts.iter()
|
||||||
for (i, count) in self.counts.iter().enumerate() {
|
.enumerate()
|
||||||
let distance = if pos > i {pos -i} else {i - pos};
|
.fold(0, |total, (i, count)| {
|
||||||
let cost_single = distance * (distance + 1) / 2; // Gauss' method for summing numbers 0 to n
|
let dx = distance_between(pos, i);
|
||||||
total += cost_single * count;
|
let cost_single = dx * (dx + 1) / 2; // Gauss' method for summing numbers from 1 to n
|
||||||
}
|
total + (cost_single * count)
|
||||||
total
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn run(data: &str) -> eyre::Result<(usize, usize)> {
|
pub fn run(data: &str) -> eyre::Result<(usize, usize)> {
|
||||||
let nums = data.trim().split(',')
|
let mut counts = Vec::new();
|
||||||
.map(|s| s.parse::<usize>())
|
for s in data.trim().split(',') {
|
||||||
.collect::<Result<Vec<_>, _>>()?;
|
let n = s.parse::<usize>()?;
|
||||||
let map = Map::from(&nums);
|
if n >= counts.len() {
|
||||||
|
counts.resize(n + 1, 0);
|
||||||
|
}
|
||||||
|
counts[n] += 1;
|
||||||
|
}
|
||||||
|
|
||||||
let mut min_simple = usize::MAX;
|
let mut min_simple = usize::MAX;
|
||||||
let mut min_complex = usize::MAX;
|
let mut min_complex = usize::MAX;
|
||||||
for i in 0..map.counts.len() {
|
for i in 0..counts.len() {
|
||||||
let simple = map.cost_simple(i);
|
let simple = cost_simple(&counts, i);
|
||||||
if simple < min_simple {min_simple = simple;}
|
if simple < min_simple {min_simple = simple;}
|
||||||
|
|
||||||
let complex = map.cost_complex(i);
|
let complex = cost_complex(&counts, i);
|
||||||
if complex < min_complex {min_complex = complex;}
|
if complex < min_complex {min_complex = complex;}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user