From f08bd0576acdd81ec8920d3a2326c701ee2e2197 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Thu, 3 Dec 2020 10:58:14 -0500 Subject: [PATCH] clean up solution a bit --- 2020/Day03.cs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/2020/Day03.cs b/2020/Day03.cs index b80c2b8..97d1641 100644 --- a/2020/Day03.cs +++ b/2020/Day03.cs @@ -10,17 +10,15 @@ 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; + int count = 0, x = dx, y = dy; + while (y < lines.Length) { string line = lines[y]; char tile = line[x % line.Length]; if (tile == '#') { count++; } + x += dx; + y += dy; } return count; } @@ -32,11 +30,11 @@ namespace AdventOfCode { 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); + var deltas = new List<(int, int)> { (1, 1), (3, 1), (5, 1), (7, 1), (1, 2) }; + long result = 1; + foreach ((int dx, int dy) in deltas) { + result *= CountTreeIntersections(input, dx, dy); + } return result; }