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.

63 lines
1.4 KiB

3 years ago
  1. using System;
  2. using static System.Console;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using Xunit;
  7. namespace AdventOfCode {
  8. public class Day03 {
  9. static int CountTreeIntersections(string[] lines, int dx, int dy) {
  10. int count = 0;
  11. int x = 0;
  12. int y = 0;
  13. while (y < lines.Length - 1) {
  14. x += dx;
  15. y += dy;
  16. string line = lines[y];
  17. char tile = line[x % line.Length];
  18. if (tile == '#') {
  19. count++;
  20. }
  21. }
  22. return count;
  23. }
  24. static int Part1() {
  25. string[] input = File.ReadAllLines(Util.RootDir + "day03.txt");
  26. return CountTreeIntersections(input, 3, 1);
  27. }
  28. static long Part2() {
  29. string[] input = File.ReadAllLines(Util.RootDir + "day03.txt");
  30. long result = CountTreeIntersections(input, 1, 1);
  31. result *= CountTreeIntersections(input, 3, 1);
  32. result *= CountTreeIntersections(input, 5, 1);
  33. result *= CountTreeIntersections(input, 7, 1);
  34. result *= CountTreeIntersections(input, 1, 2);
  35. return result;
  36. }
  37. [Fact]
  38. public static void Test() {
  39. string[] example =
  40. @"..##.......
  41. #...#...#..
  42. .#....#..#.
  43. ..#.#...#.#
  44. .#...##..#.
  45. ..#.##.....
  46. .#.#.#....#
  47. .#........#
  48. #.##...#...
  49. #...##....#
  50. .#..#...#.#".Split('\n');
  51. Assert.Equal(7, CountTreeIntersections(example, 3, 1));
  52. Assert.Equal(218, Part1());
  53. Assert.Equal(3847183340, Part2());
  54. }
  55. }
  56. }