Advertisement
R7900

DayTwentyfour

Mar 31st, 2021
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 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 DayTwentyfour
  9. {
  10. private readonly List<string> _input;
  11. private readonly List<string> _diagonals = new List<string> { "se", "sw", "nw", "ne" };
  12. private Dictionary<(double X, double Y), bool> _grid;
  13. private readonly Dictionary<string, (double X, double Y)> _offsets;
  14. private readonly List<(double X, double Y)> _offsetValues;
  15.  
  16. public DayTwentyfour()
  17. {
  18. _input = ReadFile("daytwentyfour.txt").ToList();
  19. _offsets = new Dictionary<string, (double, double)>
  20. {
  21. {"e", (1,0) },
  22. {"w", (-1,0) },
  23. {"se", (0.5,-1) },
  24. {"sw", (-0.5,-1) },
  25. {"nw", (-0.5,1) },
  26. {"ne", (0.5,1) },
  27. };
  28. _grid = new Dictionary<(double X, double Y), bool>();
  29. _offsetValues = _offsets.Values.Select(x => x).ToList();
  30. }
  31.  
  32. public void Process()
  33. {
  34. Console.WriteLine($"Part 1: {PartOne()}");
  35. Console.WriteLine($"Part 2: {PartTwo()}");
  36. }
  37.  
  38. private int PartOne()
  39. {
  40. return GetTiles().Count(x => !x.Value);
  41. }
  42.  
  43. private int PartTwo()
  44. {
  45. _grid = GetTiles();
  46. Enumerable.Range(1, 100).ToList().ForEach(_ =>
  47. {
  48. var tiles = _grid.Keys.ToList();
  49. var copy = new Dictionary<(double X, double Y), bool>(_grid);
  50. var keys = _grid.Keys.ToHashSet();
  51.  
  52. foreach (var tile in tiles)
  53. {
  54. var neighbours = _offsetValues.Select(x => (x.X + tile.X, x.Y + tile.Y)).ToList();
  55. neighbours.ForEach(tile =>
  56. {
  57. copy[tile] = UpdateTile(tile, keys);
  58. });
  59.  
  60. copy[tile] = UpdateTile(tile, keys);
  61. }
  62.  
  63. _grid = new Dictionary<(double X, double Y), bool>(copy);
  64. });
  65.  
  66. return _grid.Values.Count(x => !x);
  67. }
  68.  
  69. private bool UpdateTile((double X, double Y) tile, HashSet<(double X, double Y)> keys)
  70. {
  71. var exists = keys.Contains(tile);
  72. var inactiveNeighbours = _offsetValues.Select(x => (x.X + tile.X, x.Y + tile.Y)).Count(item => keys.Contains(item) && !_grid[item]);
  73. var isInactive = exists && !_grid[tile];
  74.  
  75. if (isInactive && (inactiveNeighbours == 0 || inactiveNeighbours > 2))
  76. {
  77. return true;
  78. }
  79. else if (!isInactive && inactiveNeighbours == 2)
  80. {
  81. return false;
  82. }
  83.  
  84. return !exists || _grid[tile];
  85. }
  86.  
  87. private Dictionary<(double X, double Y), bool> GetTiles()
  88. {
  89. var toFlip = new List<(double X, double Y)>();
  90. foreach (var line in _input)
  91. {
  92. var steps = new List<string>();
  93. var buffer = "";
  94.  
  95. for (var i = 0; i < line.Length; i++)
  96. {
  97. buffer += line[i];
  98. if (i < line.Length - 1)
  99. {
  100. buffer += line[i + 1];
  101. }
  102.  
  103. if (_diagonals.Contains(buffer))
  104. {
  105. steps.Add(buffer);
  106. i++;
  107. }
  108. else
  109. {
  110. steps.Add(buffer[0].ToString());
  111. }
  112. buffer = string.Empty;
  113. }
  114.  
  115. var coord = (X: 0.0, Y: 0.0);
  116. steps.Select(x => _offsets[x]).ToList().ForEach(x => coord = (coord.X + x.X, coord.Y + x.Y));
  117. toFlip.Add(coord);
  118. }
  119.  
  120. return toFlip.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count() % 2 == 0);
  121. }
  122. }
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement