Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7fc6572

Browse files
Fix regressions
1 parent 8991a07 commit 7fc6572

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

‎src/year2016/day18.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn count(input: &str, rows: u32) -> u32 {
4141
// Count the traps in each row.
4242
total += row.count_ones();
4343
// Only consider the left and right values for the next row.
44-
row = (row << 1) ^ (row >> 1) & mask;
44+
row = ((row << 1) ^ (row >> 1)) & mask;
4545
}
4646

4747
// We want the number of safe tiles so convert from the number of traps.

‎src/year2020/day21.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
//! [`Day 16`]: crate::year2020::day16
4646
//! [`count_ones`]: u32::count_ones
4747
use crate::util::hash::*;
48+
use std::collections::BTreeMap;
4849

4950
pub struct Input<'a> {
5051
ingredients: FastMap<&'a str, Ingredient>,
@@ -102,34 +103,32 @@ pub fn part1(input: &Input<'_>) -> u32 {
102103
}
103104

104105
pub fn part2(input: &Input<'_>) -> String {
105-
let mut ingredients = input.ingredients.clone();
106-
ingredients.retain(|_, v| v.candidates != 0);
107-
108106
let inverse_allergens: FastMap<_, _> =
109107
input.allergens.iter().map(|(k, v)| (1 << v, k)).collect();
110-
111-
// There must be at least one ingredient with only one allergen.
112-
let mut todo: Vec<_> = ingredients
108+
let mut todo: Vec<_> = input
109+
.ingredients
113110
.iter()
114-
.filter(|(_, v)| v.candidates.count_ones() == 1)
115-
.map(|(k, v)| (*k, v.candidates))
111+
.filter_map(|(&k, &v)| (v.candidates != 0).then_some((k, v.candidates)))
116112
.collect();
117-
let mut done = Vec::new();
113+
let mut done = BTreeMap::new();
118114

119115
// Eliminate known allergens from other ingredients.
120-
while let Some(pair @ (next, allergen)) = todo.pop() {
121-
ingredients.remove(next);
122-
done.push(pair);
123-
124-
for (name, ingredient) in &mut ingredients {
125-
ingredient.candidates &= !allergen;
126-
if ingredient.candidates.count_ones() == 1 {
127-
todo.push((name, ingredient.candidates));
116+
while done.len() < todo.len() {
117+
let mut mask = 0;
118+
119+
// There must be at least one ingredient with only one allergen.
120+
for (name, candidates) in &todo {
121+
if candidates.count_ones() == 1 {
122+
let allergen = inverse_allergens[candidates];
123+
done.insert(*allergen, *name);
124+
125+
mask |= candidates;
128126
}
129127
}
128+
129+
todo.iter_mut().for_each(|(_, candidates)| *candidates &= !mask);
130130
}
131131

132132
// Sort by alphabetical order of the allergens.
133-
done.sort_by_cached_key(|(_, v)| inverse_allergens[v]);
134-
done.iter().map(|(k, _)| *k).collect::<Vec<_>>().join(",")
133+
done.into_values().collect::<Vec<_>>().join(",")
135134
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /