Day 3 part 2

This commit is contained in:
Araozu 2024-02-25 16:43:55 -05:00
parent bcce0722fb
commit 91ce1fc9f3
2 changed files with 14 additions and 6 deletions

View File

@ -14,7 +14,7 @@ func main() {
runAndBenchmark("02", "2", false, solutions.Day02Part02) runAndBenchmark("02", "2", false, solutions.Day02Part02)
runAndBenchmark("03", "1", false, solutions.Day03Part01) runAndBenchmark("03", "1", false, solutions.Day03Part01)
runAndBenchmark("03", "2", true, solutions.Day03Part02) runAndBenchmark("03", "2", false, solutions.Day03Part02)
} }
type execute func(bool) int type execute func(bool) int
@ -24,5 +24,5 @@ func runAndBenchmark(day, part string, isTest bool, fn execute) {
computation := fn(isTest) computation := fn(isTest)
endMs := time.Now().UnixMicro() endMs := time.Now().UnixMicro()
duration := endMs - startMs duration := endMs - startMs
fmt.Printf("Day %s part %s:\t%10d\t%5d micros.\n", day, part, computation, duration) fmt.Printf("Day %s part %s:\t%12d\t%5d micros.\n", day, part, computation, duration)
} }

View File

@ -2,9 +2,9 @@ package solutions
import "strings" import "strings"
func countTrees(input string, left, down int) int { func countTrees(input string, right, down int) int {
verticalPos := down verticalPos := down
horizontalPos := left horizontalPos := right
lines := strings.Split(input, "\n") lines := strings.Split(input, "\n")
width := len(lines[0]) width := len(lines[0])
treesCount := 0 treesCount := 0
@ -17,7 +17,7 @@ func countTrees(input string, left, down int) int {
treesCount += 1 treesCount += 1
} }
horizontalPos += left horizontalPos += right
horizontalPos = horizontalPos % width horizontalPos = horizontalPos % width
verticalPos += down verticalPos += down
} }
@ -32,5 +32,13 @@ func Day03Part01(isTest bool) int {
} }
func Day03Part02(isTest bool) int { func Day03Part02(isTest bool) int {
return -1 input := ReadInput("03", isTest)
amount1 := countTrees(input, 1, 1)
amount2 := countTrees(input, 3, 1)
amount3 := countTrees(input, 5, 1)
amount4 := countTrees(input, 7, 1)
amount5 := countTrees(input, 1, 2)
return amount1 * amount2 * amount3 * amount4 * amount5
} }