From 1891be35f2237dffac9c3d6e18a9d089a9c1520f Mon Sep 17 00:00:00 2001 From: xuu Date: Fri, 8 Dec 2023 19:09:00 -0700 Subject: [PATCH] chore: cleanup day 1 --- day01/example1.txt | 4 ++ day01/example2.txt | 7 ++ day01/main.go | 160 +++++++++++++++++++-------------------------- day01/main_test.go | 54 +++++++++++++++ tools.go | 8 +++ 5 files changed, 139 insertions(+), 94 deletions(-) create mode 100644 day01/example1.txt create mode 100644 day01/example2.txt create mode 100644 day01/main_test.go diff --git a/day01/example1.txt b/day01/example1.txt new file mode 100644 index 0000000..1ba8437 --- /dev/null +++ b/day01/example1.txt @@ -0,0 +1,4 @@ +1abc2 +pqr3stu8vwx +a1b2c3d4e5f +treb7uchet \ No newline at end of file diff --git a/day01/example2.txt b/day01/example2.txt new file mode 100644 index 0000000..4316a6b --- /dev/null +++ b/day01/example2.txt @@ -0,0 +1,7 @@ +two1nine +eightwothree +abcone2threexyz +xtwone3four +4nineeightseven2 +zoneight234 +7pqrstsixteen \ No newline at end of file diff --git a/day01/main.go b/day01/main.go index ea46084..4877958 100644 --- a/day01/main.go +++ b/day01/main.go @@ -2,25 +2,41 @@ package main import ( "bufio" - "bytes" _ "embed" "fmt" + "os" "strings" + + aoc "go.sour.is/advent-of-code-2023" ) -//go:embed input.txt -var input []byte - func main() { - buf := bytes.NewReader(input) - scan := bufio.NewScanner(buf) + result, err := aoc.Runner(run) + if err != nil { + fmt.Println("ERR", err) + os.Exit(1) + } + + fmt.Println(result) +} + +type result struct { + sum int + sum2 int +} + +func (r result) String() string { + return fmt.Sprintln("result pt1:", r.sum, "\nresult pt2:", r.sum2) +} + +var numbers = []string{"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"} + +func run(scan *bufio.Scanner) (*result, error) { + result := &result{} - sum := 0 for scan.Scan() { var first, last int - - orig := scan.Text() - _ = orig + var first2, last2 int text := scan.Text() @@ -30,110 +46,66 @@ func main() { switch { case slice[0] >= '0' && slice[0] <= '9': - first = int(slice[0] - '0') - case strings.HasPrefix(string(slice), "one"): - first = 1 - case strings.HasPrefix(string(slice), "two"): - first = 2 - case strings.HasPrefix(string(slice), "three"): - first = 3 - case strings.HasPrefix(string(slice), "four"): - first = 4 - case strings.HasPrefix(string(slice), "five"): - first = 5 - case strings.HasPrefix(string(slice), "six"): - first = 6 - case strings.HasPrefix(string(slice), "seven"): - first = 7 - case strings.HasPrefix(string(slice), "eight"): - first = 8 - case strings.HasPrefix(string(slice), "nine"): - first = 9 + if first == 0 { + first = int(slice[0] - '0') + } + if first2 == 0 { + first2 = int(slice[0] - '0') + } + default: + if first2 != 0 { + continue + } + for i, s := range numbers { + if strings.HasPrefix(string(slice), s) { + first2 = i + break + } + } } - if first != 0 { + if first != 0 && first2 != 0 { break } } - text = string(reverse([]rune(text))) + text = string(aoc.Reverse([]rune(text))) for i := range text { copy(slice, []rune(text[i:])) - slice = reverse(slice) + slice = aoc.Reverse(slice) switch { case slice[4] >= '0' && slice[4] <= '9': - last = int(slice[4] - '0') - case strings.HasSuffix(string(slice), "one"): - last = 1 - case strings.HasSuffix(string(slice), "two"): - last = 2 - case strings.HasSuffix(string(slice), "three"): - last = 3 - case strings.HasSuffix(string(slice), "four"): - last = 4 - case strings.HasSuffix(string(slice), "five"): - last = 5 - case strings.HasSuffix(string(slice), "six"): - last = 6 - case strings.HasSuffix(string(slice), "seven"): - last = 7 - case strings.HasSuffix(string(slice), "eight"): - last = 8 - case strings.HasSuffix(string(slice), "nine"): - last = 9 + if last == 0 { + last = int(slice[4] - '0') + } + if last2 == 0 { + last2 = int(slice[4] - '0') + } + default: + if last2 != 0 { + continue + } + for i, s := range numbers { + if strings.HasSuffix(string(slice), s) { + last2 = i + break + } + } } - if last != 0 { + if last != 0 && last2 != 0 { break } } - sum += first*10 + last + result.sum += first*10 + last + result.sum2 += first2*10 + last2 } if err := scan.Err(); err != nil { - panic(err) + return nil, err } - fmt.Println(sum) + return result, nil } - -func reverse[T any](arr []T) []T{ - for i := 0; i < len(arr)/2; i++ { - arr[i], arr[len(arr)-i-1] = arr[len(arr)-i-1], arr[i] - } - return arr -} - - -// type sorter[T rune | int] []T - -// func (s sorter[T]) Less(i, j int) bool { return s[i] < s[j] } -// func (s sorter[T]) Len() int { return len(s) } -// func (s sorter[T]) Swap(i, j int) { s[i], s[j] = s[j], s[i] } - -/* -0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -one -two -three -four -five -six -seven -eight -nine -ten - - -*/ diff --git a/day01/main_test.go b/day01/main_test.go new file mode 100644 index 0000000..75dbfc5 --- /dev/null +++ b/day01/main_test.go @@ -0,0 +1,54 @@ +package main + +import ( + "bufio" + "bytes" + "testing" + + _ "embed" + + "github.com/matryer/is" +) + +//go:embed example1.txt +var example1 []byte + +//go:embed example2.txt +var example2 []byte + +//go:embed input.txt +var input []byte + +func TestExample1(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(example1)) + + result, err := run(scan) + is.NoErr(err) + + t.Log(result) + is.Equal(result.sum, 142) +} + + +func TestExample2(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(example2)) + + result, err := run(scan) + is.NoErr(err) + + t.Log(result) + is.Equal(result.sum2, 281) +} +func TestInput(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(input)) + + result, err := run(scan) + is.NoErr(err) + + t.Log(result) + is.Equal(result.sum, 54573) + is.Equal(result.sum2, 54591) +} diff --git a/tools.go b/tools.go index 115980a..a78e990 100644 --- a/tools.go +++ b/tools.go @@ -20,6 +20,14 @@ func Runner[R any, F func(*bufio.Scanner) (R, error)](run F) (R, error) { return run(scan) } +func Reverse[T any](arr []T) []T { + for i := 0; i < len(arr)/2; i++ { + arr[i], arr[len(arr)-i-1] = arr[len(arr)-i-1], arr[i] + } + return arr +} + + // greatest common divisor (GCD) via Euclidean algorithm func GCD(a, b uint64) uint64 { for b != 0 {