You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.5 KiB

  1. using System;
  2. using static System.Console;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. using Xunit;
  8. namespace AdventOfCode {
  9. public class Day09 {
  10. static bool Valid(List<long> ints, int preambleSize, int target) {
  11. for (int i = target - preambleSize; i < target - 1; i++) {
  12. for (int j = i + 1; j < target; j++) {
  13. if (i == j) {
  14. continue;
  15. }
  16. if (ints[i] + ints[j] == ints[target]) {
  17. return true;
  18. }
  19. }
  20. }
  21. return false;
  22. }
  23. static long FindInvalid(List<long> ints, int preambleSize) {
  24. for (int target = preambleSize; target < ints.Count(); target++) {
  25. if (!Valid(ints, preambleSize, target)) {
  26. return ints[target];
  27. }
  28. }
  29. throw new Exception("didn't find an invalid int");
  30. }
  31. static long Part1() {
  32. string[] input = File.ReadAllLines(Util.RootDir + "day09.txt");
  33. List<long> ints = input.ToList().Select(long.Parse).ToList();
  34. return FindInvalid(ints, 25);
  35. }
  36. static int Part2() {
  37. return -1;
  38. }
  39. [Fact]
  40. public static void Test() {
  41. string[] example =
  42. @"35
  43. 20
  44. 15
  45. 25
  46. 47
  47. 40
  48. 62
  49. 55
  50. 65
  51. 95
  52. 102
  53. 117
  54. 150
  55. 182
  56. 127
  57. 219
  58. 299
  59. 277
  60. 309
  61. 576".Split('\n');
  62. List<long> ints = example.ToList().Select(long.Parse).ToList();
  63. Assert.Equal(127, FindInvalid(ints, 5));
  64. Assert.Equal(50047984, Part1());
  65. Assert.Equal(-1, Part2());
  66. }
  67. }
  68. }