Advertisement
R7900

DaySeventeen

Mar 31st, 2021
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using static AdventOfCode2020.Common;
  5.  
  6. namespace AdventOfCode2020.Days
  7. {
  8. public class DaySeventeen
  9. {
  10. private readonly List<(int x, int y, int z, int w)> _neighbourOffsets;
  11. private Dictionary<(int x, int y, int z, int w), char> _cubes;
  12.  
  13. public DaySeventeen()
  14. {
  15. var input = ReadFile("dayseventeen.txt").ToList();
  16. _neighbourOffsets = GenerateNeighours().ToList();
  17. _neighbourOffsets.Remove((0, 0, 0, 0));
  18. _cubes = input.SelectMany((x, i) => x.Select((y, j) => (Coord: (j, i, 0, 0), Char: y))).ToDictionary(x => x.Coord, x => x.Char);
  19. }
  20.  
  21. public void Process()
  22. {
  23. var initialState = _cubes;
  24. Console.WriteLine($"Part 1: {Solve(false)}");
  25.  
  26. _cubes = initialState;
  27. Console.WriteLine($"Part 2: {Solve(true)}");
  28. }
  29.  
  30. private int Solve(bool isPartTwo)
  31. {
  32. var result = 0;
  33. for (int i = 0; i < 6; i++)
  34. {
  35. result = RunCycle(isPartTwo);
  36. }
  37. return result;
  38. }
  39.  
  40. private int RunCycle(bool isPartTwo)
  41. {
  42. var nextDict = new Dictionary<(int x, int y, int z, int w), char>();
  43.  
  44. Expand(isPartTwo);
  45.  
  46. var keys = _cubes.Keys.ToList();
  47. foreach (var key in keys)
  48. {
  49. var activeNeighbours = isPartTwo
  50. ? _neighbourOffsets.Select(x => (key.x + x.x, key.y + x.y, key.z + x.z, key.w + x.w)).Where(x => _cubes.ContainsKey(x) && _cubes[x] == '#').Count()
  51. : _neighbourOffsets.Where(x => x.w == 0).Select(x => (key.x + x.x, key.y + x.y, key.z + x.z, 0)).Where(x => _cubes.ContainsKey(x) && _cubes[x] == '#').Count();
  52.  
  53. char nextStatus;
  54.  
  55. if (_cubes[key] == '#')
  56. nextStatus = activeNeighbours == 2 || activeNeighbours == 3 ? '#' : '.';
  57. else
  58. nextStatus = activeNeighbours == 3 ? '#' : '.';
  59.  
  60. nextDict[key] = nextStatus;
  61.  
  62. }
  63. _cubes = nextDict;
  64. return _cubes.Keys.Count(x => _cubes[x] == '#');
  65. }
  66.  
  67. private void Expand(bool isPartTwo)
  68. {
  69. var keys = _cubes.Keys.ToList();
  70. foreach (var key in keys)
  71. {
  72. var neighbours = isPartTwo
  73. ? _neighbourOffsets.Select(x => (key.x + x.x, key.y + x.y, key.z + x.z, key.w + x.w))
  74. : _neighbourOffsets.Select(x => (key.x + x.x, key.y + x.y, key.z + x.z, 0));
  75.  
  76. foreach (var neighbour in neighbours)
  77. {
  78. if (!_cubes.TryGetValue(neighbour, out var value))
  79. {
  80. _cubes[neighbour] = '.';
  81. }
  82. }
  83. }
  84. }
  85.  
  86. private IEnumerable<(int x, int y, int z, int w)> GenerateNeighours()
  87. {
  88. for (int x = -1; x < 2; x++)
  89. for (int y = -1; y < 2; y++)
  90. for (int z = -1; z < 2; z++)
  91. for (int w = -1; w < 2; w++)
  92. yield return (x, y, z, w);
  93. }
  94. }
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement