From c7da89ec4bf36f8f11744f5a00fba8e8080e4b27 Mon Sep 17 00:00:00 2001 From: alexchao26 Date: Tue, 22 Dec 2020 23:10:30 -0500 Subject: [PATCH] algos added for splitting on multiple seperators --- 2016/day10/main.go | 19 ++----------------- algos/split-string-on.go | 21 +++++++++++++++++++++ algos/split-string-on_test.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 17 deletions(-) create mode 100644 algos/split-string-on.go create mode 100644 algos/split-string-on_test.go diff --git a/2016/day10/main.go b/2016/day10/main.go index dd05f21..9133576 100644 --- a/2016/day10/main.go +++ b/2016/day10/main.go @@ -6,6 +6,7 @@ import ( "sort" "strings" + "github.com/alexchao26/advent-of-code-go/algos" "github.com/alexchao26/advent-of-code-go/util" ) @@ -92,7 +93,7 @@ func parseInput(input string) (botsMap map[int][]int, rules []rule) { } botsMap[botID] = append(botsMap[botID], value) } else { - parts := splitOn(line, []string{" gives ", " and "}) + parts := algos.SplitStringOn(line, []string{" gives ", " and "}) r := rule{lowRule: parts[1], highRule: parts[2]} _, err := fmt.Sscanf(parts[0], "bot %d", &r.botID) if err != nil { @@ -103,19 +104,3 @@ func parseInput(input string) (botsMap map[int][]int, rules []rule) { } return botsMap, rules } - -func splitOn(in string, cutset []string) []string { - parts := strings.Split(in, cutset[0]) - cutset = cutset[1:] - var done bool - for !done && len(cutset) > 0 { - divider := cutset[0] - cutset = cutset[1:] - var newParts []string - for _, oldPart := range parts { - newParts = append(newParts, strings.Split(oldPart, divider)...) - } - parts = newParts - } - return parts -} diff --git a/algos/split-string-on.go b/algos/split-string-on.go new file mode 100644 index 0000000..bbc1d1c --- /dev/null +++ b/algos/split-string-on.go @@ -0,0 +1,21 @@ +package algos + +import "strings" + +// SplitStringOn is like strings.Split but takes in a slice of strings that are +// all used as dividers in the incoming string +func SplitStringOn(in string, cutset []string) []string { + parts := strings.Split(in, cutset[0]) + cutset = cutset[1:] + var done bool + for !done && len(cutset) > 0 { + divider := cutset[0] + cutset = cutset[1:] + var newParts []string + for _, oldPart := range parts { + newParts = append(newParts, strings.Split(oldPart, divider)...) + } + parts = newParts + } + return parts +} diff --git a/algos/split-string-on_test.go b/algos/split-string-on_test.go new file mode 100644 index 0000000..50a5860 --- /dev/null +++ b/algos/split-string-on_test.go @@ -0,0 +1,31 @@ +package algos_test + +import ( + "reflect" + "testing" + + "github.com/alexchao26/advent-of-code-go/algos" +) + +func TestSplitStringOn(t *testing.T) { + type args struct { + in string + cutset []string + } + tests := []struct { + name string + args args + want []string + }{ + {"simple_cutset_len_1", args{"bot 1 gets 4", []string{" gets "}}, []string{"bot 1", "4"}}, + {"simple", args{"hello x world y potato", []string{" x ", " y "}}, []string{"hello", "world", "potato"}}, + {"longer example", args{"onextwoxthree-one-two-threexfour", []string{"x", "-"}}, []string{"one", "two", "three", "one", "two", "three", "four"}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := algos.SplitStringOn(tt.args.in, tt.args.cutset); !reflect.DeepEqual(got, tt.want) { + t.Errorf("SplitStringOn() = %v, want %v", got, tt.want) + } + }) + } +}