mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Clean up day 24.
Still doesn't give the right answer, but now it's more readable.
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
use std::cmp::Ordering;
|
|
||||||
use std::cmp::Reverse;
|
use std::cmp::Reverse;
|
||||||
use std::io::BufRead;
|
use std::io::BufRead;
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
@@ -43,19 +42,6 @@ impl Group {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Ord for Group {
|
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
|
||||||
self.effective_power().cmp(&other.effective_power())
|
|
||||||
.then(other.initiative.cmp(&self.initiative))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialOrd for Group {
|
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
||||||
Some(self.cmp(other))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Day24 {
|
pub struct Day24 {
|
||||||
units: Vec<Group>,
|
units: Vec<Group>,
|
||||||
@@ -114,8 +100,7 @@ impl Day24 {
|
|||||||
|
|
||||||
fn simulate(&mut self) -> bool {
|
fn simulate(&mut self) -> bool {
|
||||||
let mut order: Vec<usize> = (0..self.units.len()).collect();
|
let mut order: Vec<usize> = (0..self.units.len()).collect();
|
||||||
order.sort_unstable_by_key(|&x| &self.units[x]);
|
order.sort_unstable_by_key(|&x| Reverse((self.units[x].effective_power(), self.units[x].initiative)));
|
||||||
order.reverse();
|
|
||||||
|
|
||||||
// select targets
|
// select targets
|
||||||
let mut targets: Vec<Option<usize>> = vec![None; self.units.len()];
|
let mut targets: Vec<Option<usize>> = vec![None; self.units.len()];
|
||||||
@@ -130,11 +115,7 @@ impl Day24 {
|
|||||||
let damage = self.units.iter().map(|x| unit.damage_to(x)).collect_vec();
|
let damage = self.units.iter().map(|x| unit.damage_to(x)).collect_vec();
|
||||||
let target = (0..self.units.len())
|
let target = (0..self.units.len())
|
||||||
.filter(|&x| !is_targeted[x] && self.units[x].faction != unit.faction && self.units[x].is_alive())
|
.filter(|&x| !is_targeted[x] && self.units[x].faction != unit.faction && self.units[x].is_alive())
|
||||||
.max_by(|&x, &y| {
|
.max_by_key(|&x| (damage[x], self.units[x].effective_power(), self.units[x].initiative));
|
||||||
damage[x].cmp(&damage[y])
|
|
||||||
.then(self.units[x].effective_power().cmp(&self.units[y].effective_power()))
|
|
||||||
.then(self.units[x].initiative.cmp(&self.units[y].initiative))
|
|
||||||
});
|
|
||||||
|
|
||||||
if let Some(target) = target {
|
if let Some(target) = target {
|
||||||
targets[i] = Some(target);
|
targets[i] = Some(target);
|
||||||
@@ -208,23 +189,22 @@ impl Solution for Day24 {
|
|||||||
|
|
||||||
fn part2(&mut self, input: &mut Read) -> String {
|
fn part2(&mut self, input: &mut Read) -> String {
|
||||||
self.read_input(input);
|
self.read_input(input);
|
||||||
let original_count = self.units.iter().map(|x| x.count).collect_vec();
|
let original = self.units.clone();
|
||||||
|
|
||||||
loop {
|
for boost in 1.. {
|
||||||
for (unit, count) in self.units.iter_mut().zip(original_count.iter()) {
|
self.units = original.clone();
|
||||||
unit.count = *count;
|
for unit in self.units.iter_mut().filter(|unit| unit.faction == 'D') {
|
||||||
if unit.faction == 'D' {
|
unit.power += boost;
|
||||||
unit.power += 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
println!("{:?}", self.units);
|
|
||||||
|
|
||||||
self.full_simulation();
|
self.full_simulation();
|
||||||
|
let result: u32 = self.units.iter().map(|x| x.count).sum();
|
||||||
if self.faction_won('D') {
|
if self.faction_won('D') {
|
||||||
let result: u32 = self.units.iter().map(|x| x.count).sum();
|
let result: u32 = self.units.iter().map(|x| x.count).sum();
|
||||||
return result.to_string();
|
return result.to_string();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
unreachable!();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user