Advertisement
R7900

DayEighteen

Mar 31st, 2021
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using static AdventOfCode2020.Common;
  6.  
  7. namespace AdventOfCode2020.Days
  8. {
  9. public class DayEighteen
  10. {
  11. private readonly List<string> _input;
  12. private const string _bracketRegex = @"\([\d\s\-\+\/\*]+\)";
  13. private readonly List<string> _partTwoOperators;
  14. private readonly List<string> _operators;
  15.  
  16. public DayEighteen()
  17. {
  18. _input = ReadFile("dayeighteen.txt").ToList();
  19. _operators = new List<string> { "+", "-", "*", "/" };
  20. _partTwoOperators = new List<string> { "+", "-" };
  21. }
  22.  
  23. public void Process()
  24. {
  25. Console.WriteLine($"Part 1: {PartOne()}");
  26. Console.WriteLine($"Part 2: {PartTwo()}");
  27. }
  28.  
  29. private long PartOne() => _input.Select(x => ProcessBrackets(x, false)).Select(x => Calculate(x, _operators)).Sum();
  30.  
  31. private long PartTwo() => _input.Select(x => ProcessBrackets(x, true)).Select(x => Calculate(x, _partTwoOperators)).Sum();
  32.  
  33. private long Calculate(string arithmetic, List<string> priorityOperators)
  34. {
  35. var delimited = arithmetic.Split(' ').ToList();
  36. var operatorIndices = Enumerable.Range(0, delimited.Count())
  37. .Where(i => priorityOperators.Contains(delimited[i]))
  38. .ToList();
  39.  
  40. while (operatorIndices.Count() > 0)
  41. {
  42. var index = operatorIndices[0];
  43. var currResult = CalculateResult(delimited.GetRange(index - 1, 3)).ToString();
  44.  
  45. delimited.RemoveRange(index - 1, 3);
  46. delimited.Insert(index - 1, currResult);
  47.  
  48. operatorIndices = Enumerable.Range(0, delimited.Count())
  49. .Where(i => priorityOperators.Contains(delimited[i]))
  50. .ToList();
  51. }
  52.  
  53. return _operators.Intersect(delimited).Count() == 0
  54. ? long.Parse(delimited[0])
  55. : Calculate(string.Join(" ", delimited), _operators);
  56. }
  57.  
  58. private long CalculateResult(List<string> arithmetic)
  59. {
  60. var lhs = long.Parse(arithmetic[0]);
  61. var rhs = long.Parse(arithmetic[2]);
  62. switch (arithmetic[1])
  63. {
  64. case "+": return lhs + rhs;
  65. case "-": return lhs - rhs;
  66. case "*": return lhs * rhs;
  67. case "/": return lhs / rhs;
  68. default: return 0;
  69. }
  70. }
  71.  
  72. private string ProcessBrackets(string arithmetic, bool isPartTwo)
  73. {
  74. var regex = new Regex(_bracketRegex);
  75.  
  76. while (Regex.IsMatch(arithmetic, _bracketRegex))
  77. {
  78. arithmetic = isPartTwo
  79. ? regex.Replace(arithmetic, x => Calculate(x.Value[1..^1], _partTwoOperators).ToString())
  80. : regex.Replace(arithmetic, x => Calculate(x.Value[1..^1], _operators).ToString());
  81. }
  82.  
  83. return arithmetic;
  84. }
  85. }
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement