From f670d107e81ef432f6e1ee5f2dfadcae11c0e717 Mon Sep 17 00:00:00 2001 From: Karl Spickermann Date: Sun, 12 Dec 2021 16:15:16 +0100 Subject: [PATCH] Day12 with Check for Test Routes --- src/day12/day12.go | 102 +++++++++++++++++++++++++++++ src/day12/day12Input.txt | 25 +++++++ src/day12/day12Test.txt | 7 ++ src/day12/day12Test2.txt | 10 +++ src/day12/day12Test3.txt | 18 +++++ src/day12/day12TestPart2Routes.txt | 36 ++++++++++ src/day4/day4.go | 2 +- src/helper/helper.go | 53 +++++++++++---- 8 files changed, 241 insertions(+), 12 deletions(-) create mode 100644 src/day12/day12.go create mode 100644 src/day12/day12Input.txt create mode 100644 src/day12/day12Test.txt create mode 100644 src/day12/day12Test2.txt create mode 100644 src/day12/day12Test3.txt create mode 100644 src/day12/day12TestPart2Routes.txt diff --git a/src/day12/day12.go b/src/day12/day12.go new file mode 100644 index 0000000..ef19016 --- /dev/null +++ b/src/day12/day12.go @@ -0,0 +1,102 @@ +package main + +import ( + "AOC2021/src/helper" + "fmt" + "os" + "strings" + "unicode" +) + +type pathTracking struct { + route []string + visitedSmallCaveTwice bool +} + +type stepTracking struct { + destiny string + visitedSmallCaveTwice bool +} + +func main() { + args := os.Args[1:] + input, err := helper.GetInput(args[0]) + input2, err := helper.GetInput("day12TestPart2Routes.txt") + if err != nil { + fmt.Println(err) + } + + testRoutesPart2 := make([][]string, 0) + for _, line := range input2 { + splitLine := strings.Split(line, ",") + testRoutesPart2 = append(testRoutesPart2, splitLine) + } + + routeMap := make(map[string][]string) + for _, line := range input { + splitLine := strings.Split(line, "-") + if splitLine[0] != "start" && splitLine[1] != "end" { + routeMap[splitLine[1]] = append(routeMap[splitLine[1]], splitLine[0]) + } + routeMap[splitLine[0]] = append(routeMap[splitLine[0]], splitLine[1]) + } + for key, val := range routeMap { + fmt.Printf("%s : %s \n", key, val) + } + + //part1 + allRoutesCounter := 0 + getRoute(&routeMap, []string{"start"}, &allRoutesCounter, 1, &testRoutesPart2) + fmt.Println(allRoutesCounter) + + //part2 + allRoutesCounter = 0 + getRoute(&routeMap, []string{"start"}, &allRoutesCounter, 2, &testRoutesPart2) + fmt.Println(allRoutesCounter) + fmt.Println(len(testRoutesPart2)) + fmt.Println() + for _, route := range testRoutesPart2 { + fmt.Println(route) + } + +} + +func getRoute(routeMap *map[string][]string, drivenRoute []string, allRoutesCounter *int, part int, testRoutesPart2 *[][]string) { + if drivenRoute[len(drivenRoute)-1] == "end" { + *allRoutesCounter++ + for i, route := range *testRoutesPart2 { + if helper.EqualStringArray(route, drivenRoute) { + *testRoutesPart2 = append((*testRoutesPart2)[:i], (*testRoutesPart2)[i+1:]...) + } + } + return + } + + position := drivenRoute[len(drivenRoute)-1] + possibleNextSteps := []string{} + for _, step := range (*routeMap)[position] { + if !unicode.IsLower(rune(step[0])) || !(helper.ContainsString(step, drivenRoute) > 0) { + possibleNextSteps = append(possibleNextSteps, step) + } else if part == 2 && !checkRouteForDoubleVisitedSmallCaves(drivenRoute) { + possibleNextSteps = append(possibleNextSteps, step) + } + } + + for _, step := range possibleNextSteps { + newDrivenRoute := append(drivenRoute, step) + getRoute(routeMap, newDrivenRoute, allRoutesCounter, part, testRoutesPart2) + } +} + +func checkRouteForDoubleVisitedSmallCaves(route []string) bool { + smallCavesCounter := make(map[string]int) + for _, step := range route { + if unicode.IsLower(rune(step[0])) && step != "end" { + smallCavesCounter[step]++ + if smallCavesCounter[step] > 1 { + return true + } + } + } + return false +} diff --git a/src/day12/day12Input.txt b/src/day12/day12Input.txt new file mode 100644 index 0000000..284c1f1 --- /dev/null +++ b/src/day12/day12Input.txt @@ -0,0 +1,25 @@ +YW-end +DK-la +la-XG +end-gy +zq-ci +XG-gz +TF-la +xm-la +gy-gz +ci-start +YW-ci +TF-zq +ci-DK +la-TS +zq-YW +gz-YW +zq-gz +end-gz +ci-TF +DK-zq +gy-YW +start-DK +gz-DK +zq-la +start-TF \ No newline at end of file diff --git a/src/day12/day12Test.txt b/src/day12/day12Test.txt new file mode 100644 index 0000000..898cd56 --- /dev/null +++ b/src/day12/day12Test.txt @@ -0,0 +1,7 @@ +start-A +start-b +A-c +A-b +b-d +A-end +b-end \ No newline at end of file diff --git a/src/day12/day12Test2.txt b/src/day12/day12Test2.txt new file mode 100644 index 0000000..ef30b81 --- /dev/null +++ b/src/day12/day12Test2.txt @@ -0,0 +1,10 @@ +dc-end +HN-start +start-kj +dc-start +dc-HN +LN-dc +HN-end +kj-sa +kj-HN +kj-dc \ No newline at end of file diff --git a/src/day12/day12Test3.txt b/src/day12/day12Test3.txt new file mode 100644 index 0000000..da6e083 --- /dev/null +++ b/src/day12/day12Test3.txt @@ -0,0 +1,18 @@ +fs-end +he-DX +fs-he +start-DX +pj-DX +end-zg +zg-sl +zg-pj +pj-he +RW-he +fs-DX +pj-RW +zg-RW +start-pj +he-WI +zg-he +pj-fs +start-RW \ No newline at end of file diff --git a/src/day12/day12TestPart2Routes.txt b/src/day12/day12TestPart2Routes.txt new file mode 100644 index 0000000..ce854ca --- /dev/null +++ b/src/day12/day12TestPart2Routes.txt @@ -0,0 +1,36 @@ +start,A,b,A,b,A,c,A,end +start,A,b,A,b,A,end +start,A,b,A,b,end +start,A,b,A,c,A,b,A,end +start,A,b,A,c,A,b,end +start,A,b,A,c,A,c,A,end +start,A,b,A,c,A,end +start,A,b,A,end +start,A,b,d,b,A,c,A,end +start,A,b,d,b,A,end +start,A,b,d,b,end +start,A,b,end +start,A,c,A,b,A,b,A,end +start,A,c,A,b,A,b,end +start,A,c,A,b,A,c,A,end +start,A,c,A,b,A,end +start,A,c,A,b,d,b,A,end +start,A,c,A,b,d,b,end +start,A,c,A,b,end +start,A,c,A,c,A,b,A,end +start,A,c,A,c,A,b,end +start,A,c,A,c,A,end +start,A,c,A,end +start,A,end +start,b,A,b,A,c,A,end +start,b,A,b,A,end +start,b,A,b,end +start,b,A,c,A,b,A,end +start,b,A,c,A,b,end +start,b,A,c,A,c,A,end +start,b,A,c,A,end +start,b,A,end +start,b,d,b,A,c,A,end +start,b,d,b,A,end +start,b,d,b,end +start,b,end \ No newline at end of file diff --git a/src/day4/day4.go b/src/day4/day4.go index 73ba299..d17ad7c 100644 --- a/src/day4/day4.go +++ b/src/day4/day4.go @@ -62,7 +62,7 @@ func parseBoard(input []string) [][]bingoField{ bingoBoard := make([][]bingoField, len(input)) for i, line := range input { numberStrings := strings.Split(line, " ") - numberStrings = helper.Delete(numberStrings, "") + numberStrings = helper.RemoveElementFromStringArray(numberStrings, "") numbers, _ := helper.MapToNumber(numberStrings) row := make([]bingoField, len(numbers)) for i, number := range numbers { diff --git a/src/helper/helper.go b/src/helper/helper.go index c42a0d3..9c3646a 100644 --- a/src/helper/helper.go +++ b/src/helper/helper.go @@ -94,6 +94,18 @@ func Equal(a, b []int) bool { return true } +func EqualStringArray(a, b []string) bool { + if len(a) != len(b) { + return false + } + for i, v := range a { + if v != b[i] { + return false + } + } + return true +} + func Contains(elem int, array []int) bool { for _, val := range array { if val == elem { @@ -103,6 +115,35 @@ func Contains(elem int, array []int) bool { return false } +func ContainsString(elem string, array []string) int { + sum := 0 + for _, val := range array { + if val == elem { + sum++ + } + } + return sum +} + +func ContainsStringArray(elem []string, array [][]string) int { + sum := 0 + for _, val := range array { + if EqualStringArray(elem, val) { + sum++ + } + } + return sum +} + +func ContainsCharacter(input string, characters string) bool { + for _, char := range characters { + if !strings.ContainsRune(input, char) { + return false + } + } + return true +} + func AddNummbers(numbers ...int) int { result := 0 for _, number := range numbers { @@ -121,7 +162,7 @@ func Transpose(in [][]int) [][]int { return out } -func Delete (stringArray []string, selector string) []string { +func RemoveElementFromStringArray (stringArray []string, selector string) []string { var returnString []string for _, str := range stringArray { if str != selector { @@ -158,15 +199,6 @@ func RemoveCharactersFromString(input string, characters string) string { return strings.Map(filter, input) } -func ContainsCharacter(input string, characters string) bool { - for _, char := range characters { - if !strings.ContainsRune(input, char) { - return false - } - } - return true -} - type sortRunes []rune func (s sortRunes) Less(i, j int) bool { @@ -194,7 +226,6 @@ type Pair struct { type PairList []Pair - func RankByValueAmount(wordFrequencies map[string]int) PairList{ pl := make(PairList, len(wordFrequencies)) i := 0