From fb2e53d0d30f9855ba5b204b0cf70c6ed0d8eb37 Mon Sep 17 00:00:00 2001 From: Kyle Hoehns <9507268+kylehoehns@users.noreply.github.com> Date: Fri, 1 Dec 2023 07:57:51 -0600 Subject: [PATCH] day 1 solution --- puzzles/day01/main.go | 70 +++++++++++++++++++++++++++ puzzles/day01/main_test.go | 27 +++++++++++ puzzles/day01/test-input-part-two.txt | 7 +++ puzzles/day01/test-input.txt | 4 ++ 4 files changed, 108 insertions(+) create mode 100644 puzzles/day01/main.go create mode 100644 puzzles/day01/main_test.go create mode 100644 puzzles/day01/test-input-part-two.txt create mode 100644 puzzles/day01/test-input.txt diff --git a/puzzles/day01/main.go b/puzzles/day01/main.go new file mode 100644 index 0000000..af5cd6d --- /dev/null +++ b/puzzles/day01/main.go @@ -0,0 +1,70 @@ +package main + +import ( + "fmt" + "github.com/kylehoehns/aoc-2023-go/pkg/files" + "github.com/kylehoehns/aoc-2023-go/pkg/ints" + "strconv" + "strings" + "unicode" +) + +func main() { + fmt.Println("Part 1: ", getCalibrationValues("input.txt")) + fmt.Println("Part 2: ", getCalibrationValues("input.txt")) +} + +func getCalibrationValues(name string) int { + lines := files.ReadLines(name) + totalCalibrationValue := 0 + for _, line := range lines { + firstDigit := getFirstDigit(line) + lastDigit := getLastDigit(line) + combined := strconv.Itoa(firstDigit) + strconv.Itoa(lastDigit) + totalCalibrationValue += ints.FromString(combined) + } + return totalCalibrationValue +} + +func getFirstDigit(s string) int { + for i := 0; i < len(s); i++ { + if found, d := containsSpelledDigit(s[:i]); found { + return d + } else if unicode.IsDigit(rune(s[i])) { + return int(s[i] - '0') + } + } + panic("No digit found in " + s) +} + +func getLastDigit(s string) int { + for i := len(s) - 1; i >= 0; i-- { + if found, d := containsSpelledDigit(s[i:]); found { + return d + } else if unicode.IsDigit(rune(s[i])) { + return int(s[i] - '0') + } + } + panic("No digit found in " + s) +} + +var spelledDigits = map[string]int{ + "one": 1, + "two": 2, + "three": 3, + "four": 4, + "five": 5, + "six": 6, + "seven": 7, + "eight": 8, + "nine": 9, +} + +func containsSpelledDigit(s string) (bool, int) { + for k, v := range spelledDigits { + if strings.Contains(s, k) { + return true, v + } + } + return false, 0 +} diff --git a/puzzles/day01/main_test.go b/puzzles/day01/main_test.go new file mode 100644 index 0000000..85bb992 --- /dev/null +++ b/puzzles/day01/main_test.go @@ -0,0 +1,27 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestPart1(t *testing.T) { + + t.Run("Part 1", func(t *testing.T) { + expected := 142 + actual := getCalibrationValues("test-input.txt") + assert.Equal(t, expected, actual) + }) + +} + +func TestPart2(t *testing.T) { + + t.Run("Part 2", func(t *testing.T) { + expected := 281 + actual := getCalibrationValues("test-input-part-two.txt") + assert.Equal(t, expected, actual) + }) + +} diff --git a/puzzles/day01/test-input-part-two.txt b/puzzles/day01/test-input-part-two.txt new file mode 100644 index 0000000..4316a6b --- /dev/null +++ b/puzzles/day01/test-input-part-two.txt @@ -0,0 +1,7 @@ +two1nine +eightwothree +abcone2threexyz +xtwone3four +4nineeightseven2 +zoneight234 +7pqrstsixteen \ No newline at end of file diff --git a/puzzles/day01/test-input.txt b/puzzles/day01/test-input.txt new file mode 100644 index 0000000..1ba8437 --- /dev/null +++ b/puzzles/day01/test-input.txt @@ -0,0 +1,4 @@ +1abc2 +pqr3stu8vwx +a1b2c3d4e5f +treb7uchet \ No newline at end of file