File tree Expand file tree Collapse file tree 3 files changed +112
-1
lines changed Expand file tree Collapse file tree 3 files changed +112
-1
lines changed Original file line number Diff line number Diff line change @@ -35,8 +35,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
3535|  [ Day 22] ( ./src/bin/22.rs )  |  ` 624.7µs `  |  ` 8.1ms `  | 
3636|  [ Day 23] ( ./src/bin/23.rs )  |  ` 157.2µs `  |  ` 620.5µs `  | 
3737|  [ Day 24] ( ./src/bin/24.rs )  |  ` 61.8µs `  |  ` 16.9µs `  | 
38+ |  [ Day 25] ( ./src/bin/25.rs )  |  ` 88.7µs `  |  ` 84.3µs `  | 
3839
39- ** Total: 26.27ms ** 
40+ ** Total: 26.45ms ** 
4041<!-- - benchmarking table ---> 
4142
4243--- 
Original file line number Diff line number Diff line change 1+ #####
2+ .####
3+ .####
4+ .####
5+ .#.#.
6+ .#...
7+ .....
8+ 
9+ #####
10+ ##.##
11+ .#.##
12+ ...##
13+ ...#.
14+ ...#.
15+ .....
16+ 
17+ .....
18+ #....
19+ #....
20+ #...#
21+ #.#.#
22+ #.###
23+ #####
24+ 
25+ .....
26+ .....
27+ #.#..
28+ ###..
29+ ###.#
30+ ###.#
31+ #####
32+ 
33+ .....
34+ .....
35+ .....
36+ #....
37+ #.#..
38+ #.#.#
39+ #####
Original file line number Diff line number Diff line change 1+ use  itertools:: Itertools ; 
2+ 
3+ advent_of_code:: solution!( 25 ) ; 
4+ 
5+ const  WIDTH :  usize  = 5 ; 
6+ const  HEIGHT :  usize  = 6 ; 
7+ 
8+ fn  parse_input ( input :  & str )  -> ( Vec < [ usize ;  WIDTH ] > ,  Vec < [ usize ;  WIDTH ] > )  { 
9+     let  mut  keys = Vec :: new ( ) ; 
10+     let  mut  padlocks = Vec :: new ( ) ; 
11+ 
12+     for  block in  input. split ( "\n \n " )  { 
13+         let  mut  data = [ 0 ;  WIDTH ] ; 
14+         for  line in  block. lines ( ) . filter ( |l| !l. is_empty ( ) )  { 
15+             for  ( i,  c)  in  line. chars ( ) . enumerate ( )  { 
16+                 if  c == '#'  { 
17+                     data[ i]  += 1 ; 
18+                 } 
19+             } 
20+         } 
21+         let  is_padlock = block. lines ( ) . next ( ) . unwrap ( )  == "#" . repeat ( WIDTH ) ; 
22+ 
23+         if  is_padlock { 
24+             padlocks. push ( data) ; 
25+         }  else  { 
26+             keys. push ( data) ; 
27+         } 
28+     } 
29+ 
30+     ( keys,  padlocks) 
31+ } 
32+ 
33+ pub  fn  part_one ( input :  & str )  -> Option < usize >  { 
34+     let  ( keys,  padlocks)  = parse_input ( input) ; 
35+ 
36+     let  overlaps = keys
37+         . iter ( ) 
38+         . cartesian_product ( padlocks. iter ( ) ) 
39+         . filter ( |( & key,  & padlock) | { 
40+             let  overlap = key
41+                 . iter ( ) 
42+                 . zip ( padlock. iter ( ) ) 
43+                 . any ( |( k,  p) | * k + * p > ( HEIGHT  + 1 ) ) ; 
44+ 
45+             !overlap
46+         } ) 
47+         . count ( ) ; 
48+ 
49+     Some ( overlaps) 
50+ } 
51+ 
52+ pub  fn  part_two ( input :  & str )  -> Option < usize >  { 
53+     part_one ( input) 
54+ } 
55+ 
56+ #[ cfg( test) ]  
57+ mod  tests { 
58+     use  super :: * ; 
59+ 
60+     #[ test]  
61+     fn  test_part_one ( )  { 
62+         let  result = part_one ( & advent_of_code:: template:: read_file ( "examples" ,  DAY ) ) ; 
63+         assert_eq ! ( result,  Some ( 3 ) ) ; 
64+     } 
65+ 
66+     #[ test]  
67+     fn  test_part_two ( )  { 
68+         let  result = part_two ( & advent_of_code:: template:: read_file ( "examples" ,  DAY ) ) ; 
69+         assert_eq ! ( result,  Some ( 3 ) ) ; 
70+     } 
71+ } 
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments