1
1
use std:: { collections:: HashSet , io:: stdin} ;
2
2
3
- use itertools:: Itertools ;
3
+ use itertools:: { iproduct , Itertools } ;
4
4
5
- fn adjacent ( xyz : ( i32 , i32 , i32 , i32 ) ) -> HashSet < ( i32 , i32 , i32 , i32 ) > {
6
- ( -1 ..=1 )
7
- . cartesian_product ( -1 ..=1 )
8
- . cartesian_product ( -1 ..=1 )
9
- . cartesian_product ( -1 ..=1 )
10
- . map ( |( ( ( x, y) , z) , w) | ( x + xyz. 0 , y + xyz. 1 , z + xyz. 2 , w+xyz. 3 ) )
11
- . collect ( )
12
- }
5
+ fn adjacent ( p : & Vec < i32 > ) -> HashSet < Vec < i32 > > {
6
+ let r = [ -1 , 0 , 1 ] ;
13
7
14
- fn main ( ) {
15
- let mut active = stdin ( )
16
- . lines ( )
17
- . enumerate ( )
18
- . map ( |( y, line) | {
19
- line. unwrap ( )
20
- . chars ( )
21
- . enumerate ( )
22
- . filter ( |( _, c) | * c == '#' )
23
- . map ( |( x, _) | ( y as i32 , x as i32 , 0 , 0 ) )
24
- . collect_vec ( )
25
- } )
26
- . flatten ( )
27
- . collect :: < HashSet < _ > > ( ) ;
28
- println ! ( "{:?}" , active) ;
8
+ match p. len ( ) {
9
+ 3 => iproduct ! ( r, r, r)
10
+ . map ( |( dx, dy, dz) | vec ! [ p[ 0 ] + dx, p[ 1 ] + dy, p[ 2 ] + dz] )
11
+ . collect ( ) ,
12
+ 4 => iproduct ! ( r, r, r, r)
13
+ . map ( |( dx, dy, dz, dw) | vec ! [ p[ 0 ] + dx, p[ 1 ] + dy, p[ 2 ] + dz, p[ 3 ] + dw] )
14
+ . collect ( ) ,
15
+ _ => HashSet :: new ( ) ,
16
+ }
17
+ }
29
18
30
- println ! ( "{:?}" , active. len( ) ) ;
19
+ fn solve ( initial_active : HashSet < Vec < i32 > > ) {
20
+ let mut active = initial_active;
31
21
for _ in 0 ..6 {
32
22
let mut new_active = HashSet :: new ( ) ;
33
23
let mut inactive = HashSet :: new ( ) ;
34
24
for xyz in active. iter ( ) {
35
- inactive. extend ( adjacent ( * xyz) . difference ( & active) ) ;
36
- if ( 3 ..=4 ) . contains ( & adjacent ( * xyz) . intersection ( & active) . count ( ) ) {
37
- new_active. insert ( * xyz) ;
25
+ let adj = adjacent ( xyz) ;
26
+ inactive. extend ( adj. difference ( & active) . cloned ( ) ) ;
27
+ if ( 3 ..=4 ) . contains ( & adj. intersection ( & active) . count ( ) ) {
28
+ new_active. insert ( xyz. to_vec ( ) ) ;
38
29
}
39
30
}
40
31
for xyz in inactive. iter ( ) {
41
- if adjacent ( * xyz) . intersection ( & active) . count ( ) == 3 {
42
- new_active. insert ( * xyz) ;
32
+ if adjacent ( xyz) . intersection ( & active) . count ( ) == 3 {
33
+ new_active. insert ( xyz. to_vec ( ) ) ;
43
34
}
44
35
}
45
36
active = new_active;
46
- println ! ( "{:?}" , active. len( ) ) ;
47
37
}
38
+ println ! ( "{:?}" , active. len( ) ) ;
48
39
}
40
+
41
+ fn main ( ) {
42
+ let active = stdin ( )
43
+ . lines ( )
44
+ . enumerate ( )
45
+ . map ( |( y, line) | {
46
+ line. unwrap ( )
47
+ . chars ( )
48
+ . enumerate ( )
49
+ . filter ( |( _, c) | * c == '#' )
50
+ . map ( |( x, _) | vec ! [ y as i32 , x as i32 , 0 ] )
51
+ . collect_vec ( )
52
+ } )
53
+ . flatten ( )
54
+ . collect :: < HashSet < _ > > ( ) ;
55
+ solve ( active. clone ( ) ) ;
56
+ solve ( active. iter ( ) . map ( |v| v. iter ( ) . cloned ( ) . chain ( vec ! [ 4 ] ) . collect ( ) ) . collect ( ) ) ;
57
+ }
0 commit comments