From bcce0722fb1f6aaef3a76416e23f5881f5cbe99f Mon Sep 17 00:00:00 2001 From: Araozu Date: Sun, 25 Feb 2024 16:37:21 -0500 Subject: [PATCH] Day 3 part 1 --- main.go | 7 +++++-- solutions/day03.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 solutions/day03.go diff --git a/main.go b/main.go index b6eebd1..f1978c4 100644 --- a/main.go +++ b/main.go @@ -10,8 +10,11 @@ func main() { runAndBenchmark("01", "1", false, solutions.Day01Part01) runAndBenchmark("01", "2", false, solutions.Day01Part02) - runAndBenchmark("02", "1", false, solutions.Day01Part01) - runAndBenchmark("02", "2", false, solutions.Day01Part02) + runAndBenchmark("02", "1", false, solutions.Day02Part01) + runAndBenchmark("02", "2", false, solutions.Day02Part02) + + runAndBenchmark("03", "1", false, solutions.Day03Part01) + runAndBenchmark("03", "2", true, solutions.Day03Part02) } type execute func(bool) int diff --git a/solutions/day03.go b/solutions/day03.go new file mode 100644 index 0000000..0610592 --- /dev/null +++ b/solutions/day03.go @@ -0,0 +1,36 @@ +package solutions + +import "strings" + +func countTrees(input string, left, down int) int { + verticalPos := down + horizontalPos := left + lines := strings.Split(input, "\n") + width := len(lines[0]) + treesCount := 0 + + for verticalPos < len(lines) { + line := lines[verticalPos] + charAt := string(line[horizontalPos]) + + if charAt == "#" { + treesCount += 1 + } + + horizontalPos += left + horizontalPos = horizontalPos % width + verticalPos += down + } + + return treesCount +} + +func Day03Part01(isTest bool) int { + input := ReadInput("03", isTest) + + return countTrees(input, 3, 1) +} + +func Day03Part02(isTest bool) int { + return -1 +}