diff options
| author | DJ O'Leary <dijitol@proton.me> | 2025-03-03 00:56:33 +0100 |
|---|---|---|
| committer | DJ O'Leary <dijitol@proton.me> | 2025-03-03 00:56:33 +0100 |
| commit | e24effa8fd3574e9d396be0e1a47f101c27c0a60 (patch) | |
| tree | 793896d20384facd10d925e09f8f0dd89c930cce /2023/Go/internal/day_01 | |
perf: Merge Advent of Code year specific repos
Diffstat (limited to '2023/Go/internal/day_01')
| -rw-r--r-- | 2023/Go/internal/day_01/day_01.go | 116 | ||||
| -rw-r--r-- | 2023/Go/internal/day_01/day_01_test.go | 31 |
2 files changed, 147 insertions, 0 deletions
diff --git a/2023/Go/internal/day_01/day_01.go b/2023/Go/internal/day_01/day_01.go new file mode 100644 index 0000000..c646a4d --- /dev/null +++ b/2023/Go/internal/day_01/day_01.go @@ -0,0 +1,116 @@ +package day_01 + +import ( + "errors" + "os" + "strconv" + "strings" +) + +func Solve() { + filename := "../_input/day_01.txt" + content := readFile(filename) + + numbers := []int{} + + lines := strings.Split(content, "\n") + for _, line := range lines { + first_num, last_num, err := findNumbers(line) + + if err != nil { + continue + } + + num_str := first_num + last_num + num := strToInt(num_str) + numbers = append(numbers, num) + } + + acc := accumulate(numbers) + + println(acc) +} + +func readFile(filename string) string { + content, err := os.ReadFile(filename) + + if err != nil { + panic(err) + } + + return string(content) +} + +var num_map = map[string]string{ + "zero": "0", + "0": "0", + "one": "1", + "1": "1", + "two": "2", + "2": "2", + "three": "3", + "3": "3", + "four": "4", + "4": "4", + "five": "5", + "5": "5", + "six": "6", + "6": "6", + "seven": "7", + "7": "7", + "eight": "8", + "8": "8", + "nine": "9", + "9": "9", +} + +func findNumbers(line string) (string, string, error) { + if line == "" || line == "\n" { + return "", "", errors.New("Empty line") + } + + found_numbers := []string{} + window_size := 5 + for i := range line { + window_end := i + window_size + if window_end > len(line) { + window_end = len(line) + } + sub_str := line[i:window_end] + + for key, value := range num_map { + if strings.HasPrefix(sub_str, key) { + found_numbers = append(found_numbers, value) + } + } + } + + if len(found_numbers) == 0 { + return "", "", errors.New("No numbers found") + } + + first := found_numbers[0] + last := found_numbers[len(found_numbers)-1] + + return first, last, nil +} + +func accumulate(numbers []int) int { + acc := 0 + + for _, num := range numbers { + acc += num + } + + return acc +} + +func strToInt(str string) int { + num, err := strconv.ParseInt(str, 0, 0) + + if err != nil { + panic(err) + } + + return int(num) +} diff --git a/2023/Go/internal/day_01/day_01_test.go b/2023/Go/internal/day_01/day_01_test.go new file mode 100644 index 0000000..8063619 --- /dev/null +++ b/2023/Go/internal/day_01/day_01_test.go @@ -0,0 +1,31 @@ +package day_01 + +import ( + "testing" +) + +type input string +type answer string + +func TestFindNumbers(t *testing.T) { + data := map[input]answer{ + "1abc2": "12", + "pqr3stu8vwx": "38", + "a1b2c3d4e5f": "15", + "treb7uchet": "77", + } + + for input, expected := range data { + actual_first, actual_last, err := findNumbers(string(input)) + + if err != nil { + t.Error("find_numbers returned an error") + } + + actual := actual_first + actual_last + + if actual != string(expected) { + t.Errorf("Expected %s, got %s", expected, actual) + } + } +} |
