From 32b30b4174ec23d5487a47a09ec99a3e4c7ca514 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Wed, 6 Dec 2023 06:17:00 -0600 Subject: [PATCH] 2023 Day 6 Complete! --- 2023/day06/input | 2 ++ 2023/day06/main.go | 49 ++++++++++++++++++++++++++++++++++++++++++++ 2023/day06/testinput | 2 ++ 3 files changed, 53 insertions(+) create mode 100644 2023/day06/input create mode 100644 2023/day06/main.go create mode 100644 2023/day06/testinput diff --git a/2023/day06/input b/2023/day06/input new file mode 100644 index 0000000..17ae311 --- /dev/null +++ b/2023/day06/input @@ -0,0 +1,2 @@ +Time: 53 71 78 80 +Distance: 275 1181 1215 1524 diff --git a/2023/day06/main.go b/2023/day06/main.go new file mode 100644 index 0000000..740392b --- /dev/null +++ b/2023/day06/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "fmt" + "strings" + + h "git.bullercodeworks.com/brian/adventofcode/helpers" +) + +func main() { + inp := h.StdinToStringSlice() + part1(inp) + fmt.Println() + part2(inp) +} + +func part1(input []string) { + var times []int + var distances []int + for _, v := range strings.Fields(input[0])[1:] { + times = append(times, h.Atoi(v)) + } + for _, v := range strings.Fields(input[1])[1:] { + distances = append(distances, h.Atoi(v)) + } + res := 1 + for i := range times { + res *= countWaysToWin(times[i], distances[i]) + } + fmt.Println("# Part 1") + fmt.Println(res) +} + +func part2(input []string) { + time := h.Atoi(strings.ReplaceAll(strings.TrimPrefix(input[0], "Time:"), " ", "")) + distance := h.Atoi(strings.ReplaceAll(strings.TrimPrefix(input[1], "Distance:"), " ", "")) + fmt.Println("# Part 2") + fmt.Println(countWaysToWin(time, distance)) +} + +func countWaysToWin(time, distance int) int { + var res int + for i := 1; i <= time; i++ { + if i*(time-i) > distance { + res++ + } + } + return res +} diff --git a/2023/day06/testinput b/2023/day06/testinput new file mode 100644 index 0000000..28f5ae9 --- /dev/null +++ b/2023/day06/testinput @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200