diff --git a/2020/Day03.cs b/2020/Day03.cs new file mode 100644 index 0000000..b80c2b8 --- /dev/null +++ b/2020/Day03.cs @@ -0,0 +1,63 @@ +using System; +using static System.Console; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Xunit; + +namespace AdventOfCode { + + public class Day03 { + + static int CountTreeIntersections(string[] lines, int dx, int dy) { + int count = 0; + int x = 0; + int y = 0; + while (y < lines.Length - 1) { + x += dx; + y += dy; + string line = lines[y]; + char tile = line[x % line.Length]; + if (tile == '#') { + count++; + } + } + return count; + } + + static int Part1() { + string[] input = File.ReadAllLines(Util.RootDir + "day03.txt"); + return CountTreeIntersections(input, 3, 1); + } + + static long Part2() { + string[] input = File.ReadAllLines(Util.RootDir + "day03.txt"); + long result = CountTreeIntersections(input, 1, 1); + result *= CountTreeIntersections(input, 3, 1); + result *= CountTreeIntersections(input, 5, 1); + result *= CountTreeIntersections(input, 7, 1); + result *= CountTreeIntersections(input, 1, 2); + return result; + } + + [Fact] + public static void Test() { + string[] example = +@"..##....... +#...#...#.. +.#....#..#. +..#.#...#.# +.#...##..#. +..#.##..... +.#.#.#....# +.#........# +#.##...#... +#...##....# +.#..#...#.#".Split('\n'); + + Assert.Equal(7, CountTreeIntersections(example, 3, 1)); + Assert.Equal(218, Part1()); + Assert.Equal(3847183340, Part2()); + } + } +} diff --git a/2020/Program.cs b/2020/Program.cs index 5468704..d8a26c9 100644 --- a/2020/Program.cs +++ b/2020/Program.cs @@ -19,8 +19,7 @@ namespace AdventOfCode { public class Program { static void Main(string[] args) { - Day01.Test(); - Day02.Test(); + Day03.Test(); } } } diff --git a/2020/day03.txt b/2020/day03.txt new file mode 100644 index 0000000..2808a19 --- /dev/null +++ b/2020/day03.txt @@ -0,0 +1,323 @@ +....#...##......##..#..#.#...#. +..######...#......#....#..#.##. +..#.#...##......#.#..#..#....#. +..#.....#..#.#........#.#..#..# +#......##..###...#.#..#.....#.. +#.......##...###...#....#...... +.....##...#......##.#.#..#.##.. +.........#......#.....#......#. +..#.#..#....#....#......##.#.## +.#...#..#.............#.#..#.#. +....#..#.#.##.#....#..#..#....# +...#..#.....#.......#...#..#..# +.....#.....#.......#..#...#.... +.##.......#...#..#........#...# +...#.......#.#.#...#.#.#......# +#....#..#.....#......##....#..# +###.#......#.#.#.#..#....#....# +......##......#.#...#...#..#... +.....#......#.#.#......#.#..... +...##...#..#........#..#.##.... +..##.#.#..#...###..........#.#. +.#..#..#.....#.........#.###.#. +....##.....#...#...##..#.##...# +....#.##....#.....##......#.... +........#.#.........#.#.......# +#....##.#....#..#...#.......... +#..###......#....##..........## +....##.#.....#..#.##......#.... +#..#......#......#............. +...##.....##.......#.......#... +#...#.#.....#..........#...###. +#.....#..#.#.###...#......###.. +...##.#......#........#..#..... +......#.....###.#...##........# +.#......##......##....#....#... +..#.#..#.....##....#....#..#... +..#.#.....#.##.#.....#.....#... +....#.......#...#.........##... +.#....#..#.......##.......#.... +..#..##.....#...##.##.#.#...... +.##.#....#............#.......# +.......#...#..#.#.##.....##..## +..###....#..#.##........##.#... +....#.#..#.....#..#.#.....#.... +..#..#.#..............#..#..... +.......#.#.#.........#......#.. +...##..#.#...#......##.#....... +#....#.#.........#...#....#..## +.#..#.#...#.......#.#.#....#.## +.#..###.#..#.#.....#..#........ +#.#..##.###.....##.........#..# +#...##...#..##..#..#..........# +.#...#..#...................... +...##..###...........#.#...##.. +..........#.#....#.#........... +..#....#....#..#....#.#.#...... +.#..#.....###......#...#...#... +#.##..#..#.........#..#....#... +........#......#...#.........#. +..#.....#.#..##...#.#.#...##... +..#...........#.##..#.#..#.##.. +..............##...#.#......#.. +#.#..#....#...##.###........#.. +.#...#..#........#........##..# +.....##..#...#.....#.#......... +.#...#...#....###...#.#.#..##.. +....#.........#....###..##....# +.#....#...####...##....####.#.. +..#..#.......#..#......#.#.#... +....#....#.......##..#.#....... +..#....#...........##.#.##..... +#..#..#...##.##....#.#.#.###.## +...#...#....#.#...##...#....### +......##........#.........#.#.. +....#####..#..##.......#.#....# +....##..#...###....#..#.....#.. +..#........#..#.#.....#....#... +..#....#......#..#...#......#.. +...#.....##...#.#..##.....#..#. +...#..#.......####.##...#...... +.....#..#..#.##..##....#..#.#.. +..#..#..##.#.#.##..#..#...#.... +...#..........#.........#....## +.##.....###...............#.##. +...##...........#.#.#......#..# +.#...#.#.##....#....#..#....... +.#...###.#....#..#..#..#......# +#..#........###...........#..#. +..#...#......#.#.#......####.#. +...#.#....##.#.....#.....##.... +...###..#.#.#...#.....#.###..#. +.#.#..#...##......#..........#. +##.....#.......#.#..###...#.#.. +##.###....#.....#.....###.#.... +#...#..##....#.#...#...#......# +.....##.#........#.###......... +.#..#..#.#......##.#...#.#..... +#..#.#........##...........##.# +#...###..#..####..#.#.......... +..#...#....#...##.#....#....##. +......#.#........#.....#..#.... +#.........#...#.....#...#..##.. +#....#.........#...#.##..###.#. +#...###...#.##.#.#............. +#.#....#....#......#....#.#...# +##...#.##......#..#.#....#..... +....#...#.##....#.............. +.........##..........#..##..#.. +......##....#.#......#......... +..#.#..............#......#..## +...........##.......#.#.#...... +##...#........##.......#.#..... +....#...#...#....#.#......##... +...#..#.#.#.........#..#.#....# +.##.#...#.#.........#.....##.#. +#.#.....#.#.....#.............. +..#.#..#....#..........#..##... +...#..#....................#..# +...........#...........#...#..# +............#...#............## +..#....##......##...........#.. +..#..#..#....#....##......##.## +##..........#..##.##.#...#..... +............#..#........###.#.. +###...##.#.#....#....#.#....#.. +...#......##...#.......####.... +.......#..#..#.#.....#......... +........##.......##.....#...... +#.#...#.###....#..#...##....... +...#.#....#..#####.#..##.#..... +..#.#..##.......###...#.....#.. +..#.......#..#...#...#..#.##... +......#..#.......#.....#....#.. +.......#........#.#.......##..# +.#.#.....#.......#.......##..#. +..#.#....#.#.#####.....#...#... +#..#............###.......#..#. +........##.........#..#...###.. +.#............##...#.....#..... +.#..##..#....#....#.......#.... +....##..........##............. +.##..........#..#..#....#...#.. +...#..#..#............####..... +.............#..#.##..#.#.##... +.....#........#....#.#.......#. +###.#..#.#...#....##........... +....#......#...#....##.......#. +.......#.#...#.#...#........##. +..........#........#..#.##..... +##..#.#.....##.#............... +.....#....#................#... +...##....#........##.#....#.... +.....##...###....#.#..#.......# +.....#.#.........##....###..... +.#.....##......#..##..##...##.# +.#..............#.....#.#...... +.##......#..#..#......##....... +.......#..................#.... +...#.#...##......####.........# +#....#####.#.#..#..#..#...#...# +##.#...#.......#....#...#...### +...#........#.....#...#.##..... +..##....#.......#....#.......## +#......#..##...#..##.#.....#.#. +..###........#.#..#........#.#. +...#.###..........#.....#.#.#.. +#.###.....#...#...#..##..###... +#....#.#.....#.#........#...... +........#.......##.......#..... +...........#...#......##....... +............#...#....#..#.....# +#.#.#.#....#.....#.#..........# +#.##...#...#..#....##.#.......# +...#..#......#..#...##..##..#.. +#....#......#.#.....##.#..#.... +#....#..##.#......#.#.....#..## +.#..##....##....#.#...#...#.... +#.#.###....#.#............#...# +.#.#....#..#..........#....#.#. +......#..#.#...............##.. +..#......###.#..........#.###.. +....#.##.#..#...##..#.#...#.... +..............#...##.......#.## +.#...........#....#....#.##.... +#..#....#.....#...#.....##...#. +.........#...#.##.......#...#.# +.....#......#.........#.#..#... +##..........#.#..##...#.#.#.... +##..##.#..#..#.....#.##....#... +........##....#.#.#....#......# +.#.##...#.###....#.........#..# +..........#....###..#.........# +#.#..#.#...#.......#..##....... +..#....#...###..............##. +#..###.....####...#..#..#...#.. +......#..#...###........###.... +..#.....#...#.......#....###..# +.#.........#.#.#....#.#.......# +#.#.###.#.#...........#........ +......#..#.........#........#.. +...........##.#........#.#...#. +.....#.#......##.......#.....## +...##...#............#..#.....# +.....#..##....##...##.#..#.#... +...#...#........#.#......##.... +........#..##..#..#......##.#.. +.#.#.....#.....#...........#.## +.#...#.#............#......#... +.....#...#........#....#..#.#.. +...##....#..#...#.............. +#....##.#.#............#....... +#..#..#.....##..#........##.#.# +##..#.#....#....##.......###..# +.#.#.#.....###.....#.#......### +.....#..#...###...#....#.#...#. +.##.....................##....# +.#.....#.........#....#.....##. +#...#....#.#...###.......#.#..# +...#.................#.#....#.# +.##...#.#...................... +.##.#........#...##............ +.#....#.....#.........#.##..##. +#......#...##..#.........##.##. +......#......#...####..#.##.... +.###....#..##......#.##......#. +..#...#....#..#.......#.#...... +#....#...#.................#.#. +....#.#.#..#...#..#.......#.#.# +#.#...##.......#.....##.#...... +#.........#.....##..##..#...... +....#..##..#.....#..#..#.#..#.. +......#.#..#.#.#....#.#.......# +.##......#..#....##...##..#.... +..#..#......#...##..#.##.....#. +..#..#.......#.#....#.....#...# +....#.#.....###...#.......#.#.. +..#....##.....##.#........##... +#...............##....#.....##. +.#.........#....#...##.###.##.# +.#.##..#.............#.#.#..#.. +.#.....#.................##.... +..####.........#.#......#.#..#. +#.......#..........#.#........# +.#.#...##.....#.#.......#....#. +..#.##.#.......###....#....#... +.#....##.............##.#.#.#.. +#.#.....#.#.#.#..#......##..#.. +.............#..........#.#.#.. +...#.#.............#.#...##.... +.......#..#.#.......#..#.#....# +.............#.........###..#.. +.#.#..#....#.....#..#.....#...# +#.....#....##..##.#..#........# +..##..###.....##....#.#..#..... +..#...##....#...#.#..........#. +...##..##.#.....#....#......... +..#...#........##.#..#........# +#.............#.###......#.##.. +.#...#........#...........#...# +..##.......#.#..##.##......#... +...#.#...##....##..#........... +.#......##........#....##....#. +.........#..#....#...#..##.##.. +....#..#.#...#.......#.#.##.... +...#.#......#.#..#..#.#....#..# +.......#........#.........###.. +#.#..#.#.........##............ +##..##..#.##..###...#.#...#.... +.#....#.#..#...#....#.##.....#. +.#.#.#.#........##...#..#.#.##. +.#..#.#..#...........#..#...... +..#.##.#...#....#.........#...# +.....##...#.#...#...#....#..... +..#..........#.#.#.......##.#.. +#.#............#..#.....#..#... +..#...........##.#.##.#....#..# +#..####.....#............#..... +.##......#####.#..#.....#....#. +...##..#.#......#.#..#..#...##. +#....................#.##...#.# +...#............#.............# +....#.##..........#.....#...... +....##..##....#.#.............. +...........#....##.#.....#..... +....#.....#....#....#......#... +#...##........#...#........#.#. +........#.....##..#.##.#..#.#.# +....##......##....#.....##....# +...#.#........##.......#...##.. +#......##..#.#.#....##......#.. +..#.......#.......##..#.##..... +.#...#...#.#.............##.... +......#.#.#.........##...#..#.# +.....#..####....#.##..........# +...#...#.#....#.....#..#.....## +.........#.......#......###.... +........##..##..#.#.#...###...# +.#..##.#....#...##.....#.#.#... +........##...#...##..#......... +.........#.......#.##..#...#### +#......#.....#..............#.# +##..##.#.##.....##...........#. +#.............#.........#...... +...####.#.##..#.#.#.##.#......# +..#.....##....#...#............ +#..............#......#...###.. +..#..#.#...#.##.........##..... +..#...##..#........#..#.##..##. +......###...#..#....#..#.###... +...##.##.###.....##.#.......#.. +#....#..###..#.......#.#.#..#.. +..##.............##..##...###.# +.#.#..#.........#..........#... +.........#.#.....##...#..#...## +....#..#....#####..#...#..#.... +...#.....#.....#...#.#..#.#.... +.#..#.............#.......##.#. +...##.......#.#.....##......#.. +...........##..#.##..###...#.#. +...........#...........#...#..# +..#....#.##.#..#..#...........# +..#.....##...#......#...#...... +...###.###.....##..........#..#