mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 12:50:32 +01:00
Optimizations for day 16.
Don't use checks that are idempotent more than once, and reduce hashmap use.
This commit is contained in:
@@ -86,26 +86,21 @@ impl Solution for Day16 {
|
||||
|
||||
let mut fixed_fields = HashMap::new();
|
||||
|
||||
while fixed_fields.len() != rules.len() {
|
||||
for ticket in &tickets {
|
||||
for ticket in &tickets {
|
||||
for (field, ranges) in &rules {
|
||||
for (pos, value) in ticket.iter().enumerate() {
|
||||
if pos_can_be[pos].len() <= 1 {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (field, ranges) in &rules {
|
||||
// If we already assigned this field, don't continue
|
||||
if fixed_fields.contains_key(field) || !pos_can_be[pos].contains(field) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if !ranges.iter().any(|r| r.contains(value)) {
|
||||
pos_can_be[pos].remove(field);
|
||||
}
|
||||
if !ranges.iter().any(|r| r.contains(value)) {
|
||||
pos_can_be[pos].remove(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while fixed_fields.len() != rules.len() {
|
||||
// Fix fields that are the only option for a certain spot
|
||||
for pos in 0..pos_can_be.len() {
|
||||
if pos_can_be[pos].len() == 1 {
|
||||
|
||||
Reference in New Issue
Block a user