Skip to content

Commit cf5d98b

Browse files
committed
Add day 21
1 parent 27c52eb commit cf5d98b

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

.aoc_tiles/tiles/2020/21.png

7.75 KB
Loading

2020/21/21.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use std::{collections::{HashMap, HashSet}, io::stdin};
2+
use itertools::Itertools;
3+
4+
fn main() {
5+
let lines = stdin().lines().filter_map(Result::ok).collect_vec();
6+
7+
let mut all: Vec<&str> = Vec::new();
8+
let mut possible = HashMap::new();
9+
10+
for line in &lines {
11+
let (left, right) = line.split_once(" (contains ").unwrap();
12+
let ingredients: HashSet<&str> = left.split(" ").collect();
13+
let allergens = right.trim_matches(')').split(", ").collect_vec();
14+
all.extend(&ingredients);
15+
16+
for allergen in allergens {
17+
possible.entry(allergen)
18+
.or_insert(ingredients.clone())
19+
.retain(|k| ingredients.contains(k))
20+
}
21+
}
22+
println!("{}", all.iter().filter(|k| !possible.values().flatten().contains(k)).count());
23+
24+
let mut pairings: HashMap<&str, &str> = HashMap::new();
25+
while possible.len() != pairings.len() {
26+
possible.iter()
27+
.filter(|(_, v)| v.len() == 1)
28+
.for_each(|(k, v)| { pairings.insert(k, v.iter().next().unwrap()); });
29+
possible.values_mut()
30+
.for_each(|v| v.retain(|a| !pairings.values().contains(a)))
31+
}
32+
println!("{}", pairings.iter().sorted().map(|(_, b)| b).join(","));
33+
}

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!-- AOC TILES BEGIN -->
22
<h1 align="center">
3-
Advent of Code - 190/450 ⭐
3+
Advent of Code - 192/450 ⭐
44
</h1>
55
<h1 align="center">
66
2023 - 50 ⭐
@@ -237,7 +237,7 @@
237237
<img src=".aoc_tiles/tiles/2021/25.png" width="161px">
238238
</a>
239239
<h1 align="center">
240-
2020 - 40
240+
2020 - 42
241241
</h1>
242242
<a href="2020/01/01.rs">
243243
<img src=".aoc_tiles/tiles/2020/01.png" width="161px">
@@ -299,6 +299,9 @@
299299
<a href="2020/20/20.rs">
300300
<img src=".aoc_tiles/tiles/2020/20.png" width="161px">
301301
</a>
302+
<a href="2020/21/21.rs">
303+
<img src=".aoc_tiles/tiles/2020/21.png" width="161px">
304+
</a>
302305
<!-- AOC TILES END -->
303306

304307
*The above tiles are clickable, leading to the solution of the corresponding day.*

0 commit comments

Comments
 (0)