diff options
| author | DJ O'Leary <dijitol@proton.me> | 2025-11-29 20:23:02 +0100 |
|---|---|---|
| committer | DJ O'Leary <dijitol@proton.me> | 2025-11-29 20:23:02 +0100 |
| commit | 4c3a50166bf0f78ce89459c352cc1c6536a8b96d (patch) | |
| tree | 02e4b66a1435680ef1e92bf82576747b27f16447 /2023/Go/internal/day_06/part_02/part_02.go | |
| parent | be2f8528b3829f8263507da301812d852297c168 (diff) | |
chore: make language folders snake_case
Diffstat (limited to '2023/Go/internal/day_06/part_02/part_02.go')
| -rw-r--r-- | 2023/Go/internal/day_06/part_02/part_02.go | 81 |
1 files changed, 0 insertions, 81 deletions
diff --git a/2023/Go/internal/day_06/part_02/part_02.go b/2023/Go/internal/day_06/part_02/part_02.go deleted file mode 100644 index 316b169..0000000 --- a/2023/Go/internal/day_06/part_02/part_02.go +++ /dev/null @@ -1,81 +0,0 @@ -package part_02 - -import ( - "os" - "strconv" - "strings" -) - -const USE_SAMPLE = false - -/** - * Tried: - * - 24655068 (correct!) - */ -func Solve() { - var filePath string - if USE_SAMPLE { - filePath = "../_input/day_06_part_02_sample.txt" - } else { - filePath = "../_input/day_06.txt" - } - contents, _ := os.ReadFile(filePath) - - lines := strings.Split(string(contents), "\r\n") - - timeLine := lines[0] - time := parseTimeLine(timeLine) - - distLine := lines[1] - distance := parseDistanceLine(distLine) - - answer := calculateAnswer(time, distance) - - 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 := []string{} - for _, number := range strings.Split(withoutTitle, " ") { - if number == "" { - continue - } - numbers = append(numbers, number) - } - - number := strings.Join(numbers, "") - numberInt, err := strconv.Atoi(number) - if err != nil { - panic(err) - } - - return numberInt -} - -func calculateAnswer(maxTime int, recordDistance int) int { - var greaterThanRecord []int - for buttonPressedFor := 0; buttonPressedFor <= maxTime; buttonPressedFor++ { - distance := calculateDistance(buttonPressedFor, maxTime) - if distance > recordDistance { - greaterThanRecord = append(greaterThanRecord, distance) - } - } - - return len(greaterThanRecord) -} - -func calculateDistance(buttonPressedFor int, maxTime int) int { - return buttonPressedFor * (maxTime - buttonPressedFor) -} |
