mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +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();
|
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() {
|
for (pos, value) in ticket.iter().enumerate() {
|
||||||
if pos_can_be[pos].len() <= 1 {
|
if pos_can_be[pos].len() <= 1 {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (field, ranges) in &rules {
|
if !ranges.iter().any(|r| r.contains(value)) {
|
||||||
// If we already assigned this field, don't continue
|
pos_can_be[pos].remove(field);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while fixed_fields.len() != rules.len() {
|
||||||
// Fix fields that are the only option for a certain spot
|
// Fix fields that are the only option for a certain spot
|
||||||
for pos in 0..pos_can_be.len() {
|
for pos in 0..pos_can_be.len() {
|
||||||
if pos_can_be[pos].len() == 1 {
|
if pos_can_be[pos].len() == 1 {
|
||||||
|
|||||||
Reference in New Issue
Block a user