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.

61 lines
1.3 KiB

3 years ago
3 years ago
3 years ago
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, x = dx, y = dy;
  11. while (y < lines.Length) {
  12. string line = lines[y];
  13. char tile = line[x % line.Length];
  14. if (tile == '#') {
  15. count++;
  16. }
  17. x += dx;
  18. y += dy;
  19. }
  20. return count;
  21. }
  22. static int Part1() {
  23. string[] input = File.ReadAllLines(Util.RootDir + "day03.txt");
  24. return CountTreeIntersections(input, 3, 1);
  25. }
  26. static long Part2() {
  27. string[] input = File.ReadAllLines(Util.RootDir + "day03.txt");
  28. var deltas = new List<(int, int)> { (1, 1), (3, 1), (5, 1), (7, 1), (1, 2) };
  29. long result = 1;
  30. foreach ((int dx, int dy) in deltas) {
  31. result *= CountTreeIntersections(input, dx, dy);
  32. }
  33. return result;
  34. }
  35. [Fact]
  36. public static void Test() {
  37. string[] example =
  38. @"..##.......
  39. #...#...#..
  40. .#....#..#.
  41. ..#.#...#.#
  42. .#...##..#.
  43. ..#.##.....
  44. .#.#.#....#
  45. .#........#
  46. #.##...#...
  47. #...##....#
  48. .#..#...#.#".Split('\n');
  49. Assert.Equal(7, CountTreeIntersections(example, 3, 1));
  50. Assert.Equal(218, Part1());
  51. Assert.Equal(3847183340, Part2());
  52. }
  53. }
  54. }