Skip to content

Commit 35fd883

Browse files
committed
24
1 parent cb3146d commit 35fd883

File tree

1 file changed

+48
-55
lines changed

1 file changed

+48
-55
lines changed

2020/Day24/Solution.cs

Lines changed: 48 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,72 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Collections.Immutable;
43
using System.Linq;
5-
using System.Text.RegularExpressions;
6-
using System.Text;
74

85
namespace AdventOfCode.Y2020.Day24 {
6+
record Tile(int x, int y, int z);
97

108
[ProblemName("Lobby Layout")]
119
class Solution : Solver {
1210

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();
1712

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();
2517

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+
};
2927

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);
3731

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();
4738

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();
5545
}
5646

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+
5950
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);
6253
}
6354

64-
return tiles;
55+
return (from kvp in tiles where kvp.Value select kvp.Key).ToHashSet();
6556
}
66-
IEnumerable<(int x, int y, int z)> Wander(string input) {
57+
58+
Tile Walk(string line) {
6759
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+
}
7667
}
68+
return new Tile(x, y, z);
7769
}
70+
7871
}
79-
}
72+
}

0 commit comments

Comments
 (0)