File tree Expand file tree Collapse file tree 3 files changed +38
-2
lines changed Expand file tree Collapse file tree 3 files changed +38
-2
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
<!-- AOC TILES BEGIN -->
2
2
<h1 align =" center " >
3
- Advent of Code - 190 /450 ⭐
3
+ Advent of Code - 192 /450 ⭐
4
4
</h1 >
5
5
<h1 align =" center " >
6
6
2023 - 50 ⭐
237
237
<img src =" .aoc_tiles/tiles/2021/25.png " width =" 161px " >
238
238
</a >
239
239
<h1 align =" center " >
240
- 2020 - 40 ⭐
240
+ 2020 - 42 ⭐
241
241
</h1 >
242
242
<a href =" 2020/01/01.rs " >
243
243
<img src =" .aoc_tiles/tiles/2020/01.png " width =" 161px " >
299
299
<a href =" 2020/20/20.rs " >
300
300
<img src =" .aoc_tiles/tiles/2020/20.png " width =" 161px " >
301
301
</a >
302
+ <a href =" 2020/21/21.rs " >
303
+ <img src =" .aoc_tiles/tiles/2020/21.png " width =" 161px " >
304
+ </a >
302
305
<!-- AOC TILES END -->
303
306
304
307
* The above tiles are clickable, leading to the solution of the corresponding day.*
You can’t perform that action at this time.
0 commit comments