diff options
| author | DJ O'Leary <dijitol@proton.me> | 2026-06-26 18:04:09 +0200 |
|---|---|---|
| committer | DJ O'Leary <dijitol@proton.me> | 2026-06-26 18:04:09 +0200 |
| commit | 6d8fb621d1e93b83786890d6d9cb6f0d8defa8ae (patch) | |
| tree | 17c90d3984c114aa023600cc5bec6f6004b02243 /2023/go/internal/day_06/part_01/part_01.go | |
| parent | ad4ea5ca4bc9f7540ee1d6a4fa2d881709dc6459 (diff) | |
refactor!: invert repo directory structure
now is language first
Diffstat (limited to '2023/go/internal/day_06/part_01/part_01.go')
| -rw-r--r-- | 2023/go/internal/day_06/part_01/part_01.go | 85 |
1 files changed, 0 insertions, 85 deletions
diff --git a/2023/go/internal/day_06/part_01/part_01.go b/2023/go/internal/day_06/part_01/part_01.go deleted file mode 100644 index 5735924..0000000 --- a/2023/go/internal/day_06/part_01/part_01.go +++ /dev/null @@ -1,85 +0,0 @@ -package part_01 - -import ( - "os" - "strconv" - "strings" -) - -const USE_SAMPLE = false - -/** - * Tried: - * - 72128 (too low) - * - 3317888 (correct!) - */ -func Solve() { - var filePath string - if USE_SAMPLE { - filePath = "../_input/day_06_part_01_sample.txt" - } else { - filePath = "../_input/day_06.txt" - } - contents, _ := os.ReadFile(filePath) - - lines := strings.Split(string(contents), "\r\n") - - timeLine := lines[0] - times := parseTimeLine(timeLine) - - distLine := lines[1] - distances := parseDistanceLine(distLine) - - answer := calculateAnswer(times, distances) - - println(answer) -} - -func parseTimeLine(timeLine string) []int { - prefix := "Time:" - return parseLine(prefix, timeLine) -} - -func parseDistanceLine(distLine string) []int { - prefix := "Distance:" - return parseLine(prefix, distLine) -} - -func parseLine(prefix string, line string) []int { - withoutTitle := strings.TrimPrefix(line, prefix) - - numbers := []int{} - for _, number := range strings.Split(withoutTitle, " ") { - if number == "" { - continue - } - numberInt, _ := strconv.Atoi(number) - numbers = append(numbers, numberInt) - } - return numbers -} - -func calculateAnswer(times []int, distances []int) int { - answer := 1 - for i, maxTime := range times { - recordDistance := distances[i] - - var greaterThanRecord []int - for buttonPressedFor := 0; buttonPressedFor <= maxTime; buttonPressedFor++ { - distance := calculateDistance(buttonPressedFor, maxTime) - if distance > recordDistance { - greaterThanRecord = append(greaterThanRecord, distance) - } - } - - if len(greaterThanRecord) > 0 { - answer *= len(greaterThanRecord) - } - } - - return answer -} - -func calculateDistance(buttonPressedFor int, maxTime int) int { - return buttonPressedFor * (maxTime - buttonPressedFor) -} |
