diff options
Diffstat (limited to 'go/2023/internal/day_06/part_02')
| -rw-r--r-- | go/2023/internal/day_06/part_02/part_02.go | 81 | ||||
| -rw-r--r-- | go/2023/internal/day_06/part_02/part_02_test.go | 84 |
2 files changed, 165 insertions, 0 deletions
diff --git a/go/2023/internal/day_06/part_02/part_02.go b/go/2023/internal/day_06/part_02/part_02.go new file mode 100644 index 0000000..316b169 --- /dev/null +++ b/go/2023/internal/day_06/part_02/part_02.go @@ -0,0 +1,81 @@ +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) +} diff --git a/go/2023/internal/day_06/part_02/part_02_test.go b/go/2023/internal/day_06/part_02/part_02_test.go new file mode 100644 index 0000000..67924dc --- /dev/null +++ b/go/2023/internal/day_06/part_02/part_02_test.go @@ -0,0 +1,84 @@ +package part_02 + +import "testing" + +func TestParseTimeLine(t *testing.T) { + tests := map[string]struct { + input string + expected int + }{ + "sample": {"Time: 7 15 30", 71530}, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + actual := parseTimeLine(test.input) + if actual != test.expected { + t.Errorf("Expected %d, got %d", test.expected, actual) + } + }) + } +} + +func TestParseDistLine(t *testing.T) { + tests := map[string]struct { + input string + expected int + }{ + "sample": {"Distance: 9 40 200", 940200}, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + actual := parseDistanceLine(test.input) + if actual != test.expected { + t.Errorf("Expected %d, got %d", test.expected, actual) + } + }) + } +} + +func TestCalculateAnswer(t *testing.T) { + tests := map[string]struct { + maxTime int + recordDistance int + expected int + }{ + "sample": {71530, 940200, 71503}, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + actual := calculateAnswer(test.maxTime, test.recordDistance) + if actual != test.expected { + t.Errorf("Expected %d, got %d", test.expected, actual) + } + }) + } +} + +func TestCalculateDistance(t *testing.T) { + tests := map[string]struct { + maxTime int + buttonPressedFor int + expected int + }{ + "7_7": {7, 7, 0}, + "7_6": {7, 6, 6}, + "7_5": {7, 5, 10}, + "7_4": {7, 4, 12}, + "7_3": {7, 3, 12}, + "7_2": {7, 2, 10}, + "7_1": {7, 1, 6}, + "7_0": {7, 0, 0}, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + actual := calculateDistance(test.buttonPressedFor, test.maxTime) + if actual != test.expected { + t.Errorf("Expected %d, got %d", test.expected, actual) + } + }) + } +} |
