@@ -4,9 +4,82 @@ namespace Formula9.AdventOfCode.Solutions2023.Day11;
44
55public  class  Day_11  :  AdventOfCodeProblem 
66{ 
7-  public  Day_11 ( )  :  base ( 2023 ,  11 )  {  } 
87
9-  public  override  ValueTask < string >  Solve_1 ( )  =>  new ( "Solution 1" ) ; 
8+  public  ISet < int >  EmptyRows  {  get ;  init ;  } 
9+  public  ISet < int >  EmptyCols  {  get ;  init ;  } 
10+  public  ISet < ( int  y ,  int  x ) >  Stars  {  get ;  init ;  } 
11+  public  ISet < ( ( int  y ,  int  x )  left ,  ( int  y ,  int  x )  right ) >  StarPairs  {  get ;  private  set ;  } 
1012
11-  public  override  ValueTask < string >  Solve_2 ( )  =>  new ( "Solution 2" ) ; 
13+  public  Day_11 ( )  :  base ( 2023 ,  11 )  
14+  { 
15+  EmptyRows  =  new  HashSet < int > ( ) ; 
16+  EmptyCols  =  new  HashSet < int > ( ) ; 
17+  Stars  =  new  HashSet < ( int  y ,  int  x ) > ( ) ; 
18+  StarPairs  =  new  HashSet < ( ( int  y ,  int  x )  left ,  ( int  y ,  int  x )  right ) > ( ) ; 
19+  ReadData ( ) ; 
20+  } 
21+ 
22+  public  void  ReadData ( ) 
23+  { 
24+  var  grid  =  Input . SplitByNewline ( ) ; 
25+  foreach  ( ( int  y ,  var  line )  in  grid . Enumerate ( ) ) 
26+  { 
27+  if  ( IsLineEmpty ( line ) ) 
28+  { 
29+  EmptyRows . Add ( y ) ; 
30+  continue ; 
31+  } 
32+  
33+  foreach  ( ( int  x ,  char  c )  in  line . Enumerate ( ) ) 
34+  { 
35+  if  ( c  ==  '#' )  Stars . Add ( ( y ,  x ) ) ; 
36+  } 
37+  } 
38+ 
39+  foreach  ( int  x  in  Enumerable . Range ( 0 ,  grid [ 0 ] . Length ) ) 
40+  { 
41+  if  ( Enumerable . Range ( 0 ,  grid . Length ) . Select ( y =>  grid [ y ] [ x ] ) . ToHashSet ( ) . Count  ==  1 ) 
42+  { 
43+  EmptyCols . Add ( x ) ; 
44+  } 
45+  } 
46+ 
47+  StarPairs  =  Stars . Combinations ( 2 ) . Select ( enumerable =>  ( left :  enumerable . First ( ) ,  right :  enumerable . Last ( ) ) ) . ToHashSet ( ) ; 
48+  } 
49+ 
50+  public  long  FindShortestPath ( ( int  x ,  int  y )  left ,  ( int  x ,  int  y )  right ,  int  expansionLevel ) 
51+  { 
52+  ( int  smallestX ,  int  smallestY )  =  ( Math . Min ( left . x ,  right . x ) ,  Math . Min ( left . y ,  right . y ) ) ; 
53+  ( int  biggestX ,  int  biggestY )  =  ( Math . Max ( left . x ,  right . x ) ,  Math . Max ( left . y ,  right . y ) ) ; 
54+  ( long  dx ,  long  dy )  =  ( Math . Abs ( left . x  -  right . x ) ,  Math . Abs ( left . y  -  right . y ) ) ; 
55+  foreach  ( var  row  in  EmptyRows ) 
56+  { 
57+  for  ( int  x  =  smallestX ;  x  <  biggestX ;  x ++ ) 
58+  { 
59+  if  ( row  ==  x ) 
60+  { 
61+  dx  +=  expansionLevel ; 
62+  break ; 
63+  } 
64+  } 
65+  } 
66+  foreach  ( var  col  in  EmptyCols ) 
67+  { 
68+  for  ( int  y  =  smallestY ;  y  <  biggestY ;  y ++ ) 
69+  { 
70+  if  ( col  ==  y ) 
71+  { 
72+  dy  +=  expansionLevel ; 
73+  break ; 
74+  } 
75+  } 
76+  } 
77+  return  dx  +  dy ; 
78+  }  
79+ 
80+  public  static   bool  IsLineEmpty ( string  l )  =>  l . Select ( c =>  c ) . ToHashSet ( ) . Count  ==  1 ; 
81+ 
82+  public  override  ValueTask < string >  Solve_1 ( )  =>  new ( StarPairs . Sum ( pairTuple =>  FindShortestPath ( pairTuple . left ,  pairTuple . right ,  1 ) ) . ToString ( ) ) ; 
83+ 
84+  public  override  ValueTask < string >  Solve_2 ( )  =>  new ( StarPairs . Sum ( pairTuple =>  FindShortestPath ( pairTuple . left ,  pairTuple . right ,  1_000_000  -  1 ) ) . ToString ( ) ) ; 
1285} 
0 commit comments