1
- using System ;
2
1
using System . Collections . Generic ;
3
2
using System . Collections . Immutable ;
4
3
using System . Linq ;
5
- using System . Text . RegularExpressions ;
6
- using System . Text ;
7
4
8
5
namespace AdventOfCode . Y2020 . Day24 {
6
+ record Tile ( int x , int y , int z ) ;
9
7
10
8
[ ProblemName ( "Lobby Layout" ) ]
11
9
class Solution : Solver {
12
10
13
- public object PartOne ( string input ) {
14
- var tiles = Parse ( input ) ;
15
- return tiles . Values . Sum ( ) ;
16
- }
11
+ public object PartOne ( string input ) => ParseBlackTiles ( input ) . Count ( ) ;
17
12
18
- public object PartTwo ( string input ) {
19
- var tiles = Parse ( input ) ;
20
- for ( var i = 0 ; i < 100 ; i ++ ) {
21
- tiles = Flip ( tiles ) ;
22
- }
23
- return tiles . Values . Sum ( ) ;
24
- }
13
+ public object PartTwo ( string input ) =>
14
+ Enumerable . Range ( 0 , 100 )
15
+ . Aggregate ( ParseBlackTiles ( input ) , ( blackTiles , _ ) => Flip ( blackTiles ) )
16
+ . Count ( ) ;
25
17
26
- Dictionary < ( int , int , int ) , int > Flip ( Dictionary < ( int , int , int ) , int > tiles ) {
27
- var res = new Dictionary < ( int , int , int ) , int > ( ) ;
28
- var neighbours = new [ ] { ( 0 , 1 , - 1 ) , ( 1 , 0 , - 1 ) , ( 1 , - 1 , 0 ) , ( 0 , - 1 , 1 ) , ( - 1 , 0 , 1 ) , ( - 1 , 1 , 0 ) } ;
18
+ Dictionary < string , ( int x , int y , int z ) > HexDirections = new Dictionary < string , ( int x , int y , int z ) > {
19
+ { "x" , ( 0 , 0 , 0 ) } ,
20
+ { "e" , ( 0 , 1 , - 1 ) } ,
21
+ { "se" , ( 1 , 0 , - 1 ) } ,
22
+ { "sw" , ( 1 , - 1 , 0 ) } ,
23
+ { "w" , ( 0 , - 1 , 1 ) } ,
24
+ { "nw" , ( - 1 , 0 , 1 ) } ,
25
+ { "ne" , ( - 1 , 1 , 0 ) }
26
+ } ;
29
27
30
- var tilesX = new HashSet < ( int , int , int ) > ( ) ;
31
- foreach ( var tile in tiles . Keys ) {
32
- tilesX . Add ( tile ) ;
33
- foreach ( var neighbour in from dir in neighbours select ( tile . Item1 + dir . Item1 , tile . Item2 + dir . Item2 , tile . Item3 + dir . Item3 ) ) {
34
- tilesX . Add ( neighbour ) ;
35
- }
36
- }
28
+ IEnumerable < Tile > Neighbourhood ( Tile point ) =>
29
+ from dir in HexDirections . Values
30
+ select new Tile ( point . x + dir . x , point . y + dir . y , point . z + dir . z ) ;
37
31
38
- foreach ( var tile in tilesX ) {
39
- var color = tiles . GetValueOrDefault ( tile ) ;
40
- var blackNeighbours = 0 ;
41
- foreach ( var neighbour in from dir in neighbours select ( tile . Item1 + dir . Item1 , tile . Item2 + dir . Item2 , tile . Item3 + dir . Item3 ) ) {
42
- // Console.WriteLine(tile+" "+neighbour);
43
- if ( tiles . GetValueOrDefault ( neighbour ) == 1 ) {
44
- blackNeighbours ++ ;
45
- }
46
- }
32
+ HashSet < Tile > Flip ( HashSet < Tile > blackTiles ) {
33
+ var tiles = (
34
+ from black in blackTiles
35
+ from tile in Neighbourhood ( black )
36
+ select tile
37
+ ) . ToHashSet ( ) ;
47
38
48
- if ( color == 1 ) {
49
- res [ tile ] = blackNeighbours == 0 || blackNeighbours > 2 ? 0 : 1 ;
50
- } else {
51
- res [ tile ] = blackNeighbours == 2 ? 1 : 0 ;
52
- }
53
- }
54
- return res ;
39
+ return (
40
+ from tile in tiles
41
+ let blacks = Neighbourhood ( tile ) . Count ( n => blackTiles . Contains ( n ) )
42
+ where blacks == 2 || blacks == 3 && blackTiles . Contains ( tile )
43
+ select tile
44
+ ) . ToHashSet ( ) ;
55
45
}
56
46
57
- Dictionary < ( int , int , int ) , int > Parse ( string input ) {
58
- Dictionary < ( int , int , int ) , int > tiles = new Dictionary < ( int , int , int ) , int > ( ) ;
47
+ HashSet < Tile > ParseBlackTiles ( string input ) {
48
+ var tiles = new Dictionary < Tile , bool > ( ) ;
49
+
59
50
foreach ( var line in input . Split ( "\n " ) ) {
60
- var c = Wander ( line ) . Last ( ) ;
61
- tiles [ c ] = ( tiles . GetValueOrDefault ( c ) + 1 ) % 2 ;
51
+ var tile = Walk ( line ) ;
52
+ tiles [ tile ] = ! tiles . GetValueOrDefault ( tile ) ;
62
53
}
63
54
64
- return tiles ;
55
+ return ( from kvp in tiles where kvp . Value select kvp . Key ) . ToHashSet ( ) ;
65
56
}
66
- IEnumerable < ( int x , int y , int z ) > Wander ( string input ) {
57
+
58
+ Tile Walk ( string line ) {
67
59
var ( x , y , z ) = ( 0 , 0 , 0 ) ;
68
- while ( input != "" ) {
69
- if ( input . StartsWith ( "e" ) ) { ( x , y , z ) = ( x + 0 , y + 1 , z - 1 ) ; input = input . Substring ( 1 ) ; }
70
- else if ( input . StartsWith ( "se" ) ) { ( x , y , z ) = ( x + 1 , y + 0 , z - 1 ) ; input = input . Substring ( 2 ) ; }
71
- else if ( input . StartsWith ( "sw" ) ) { ( x , y , z ) = ( x + 1 , y - 1 , z + 0 ) ; input = input . Substring ( 2 ) ; }
72
- else if ( input . StartsWith ( "w" ) ) { ( x , y , z ) = ( x + 0 , y - 1 , z + 1 ) ; input = input . Substring ( 1 ) ; }
73
- else if ( input . StartsWith ( "nw" ) ) { ( x , y , z ) = ( x - 1 , y + 0 , z + 1 ) ; input = input . Substring ( 2 ) ; }
74
- else if ( input . StartsWith ( "ne" ) ) { ( x , y , z ) = ( x - 1 , y + 1 , z + 0 ) ; input = input . Substring ( 2 ) ; }
75
- yield return ( x , y , z ) ;
60
+ while ( line != "" ) {
61
+ foreach ( var kvp in HexDirections ) {
62
+ if ( line . StartsWith ( kvp . Key ) ) {
63
+ line = line . Substring ( kvp . Key . Length ) ;
64
+ ( x , y , z ) = ( x + kvp . Value . x , y + kvp . Value . y , z + kvp . Value . z ) ;
65
+ }
66
+ }
76
67
}
68
+ return new Tile ( x , y , z ) ;
77
69
}
70
+
78
71
}
79
- }
72
+ }
0 commit comments