Day12 with Check for Test Routes

This commit is contained in:
Karl Spickermann 2021-12-12 16:15:16 +01:00
parent d521e18239
commit f670d107e8
8 changed files with 241 additions and 12 deletions

102
src/day12/day12.go Normal file
View File

@ -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
}

25
src/day12/day12Input.txt Normal file
View File

@ -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

7
src/day12/day12Test.txt Normal file
View File

@ -0,0 +1,7 @@
start-A
start-b
A-c
A-b
b-d
A-end
b-end

10
src/day12/day12Test2.txt Normal file
View File

@ -0,0 +1,10 @@
dc-end
HN-start
start-kj
dc-start
dc-HN
LN-dc
HN-end
kj-sa
kj-HN
kj-dc

18
src/day12/day12Test3.txt Normal file
View File

@ -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

View File

@ -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

View File

@ -62,7 +62,7 @@ func parseBoard(input []string) [][]bingoField{
bingoBoard := make([][]bingoField, len(input)) bingoBoard := make([][]bingoField, len(input))
for i, line := range input { for i, line := range input {
numberStrings := strings.Split(line, " ") numberStrings := strings.Split(line, " ")
numberStrings = helper.Delete(numberStrings, "") numberStrings = helper.RemoveElementFromStringArray(numberStrings, "")
numbers, _ := helper.MapToNumber(numberStrings) numbers, _ := helper.MapToNumber(numberStrings)
row := make([]bingoField, len(numbers)) row := make([]bingoField, len(numbers))
for i, number := range numbers { for i, number := range numbers {

View File

@ -94,6 +94,18 @@ func Equal(a, b []int) bool {
return true 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 { func Contains(elem int, array []int) bool {
for _, val := range array { for _, val := range array {
if val == elem { if val == elem {
@ -103,6 +115,35 @@ func Contains(elem int, array []int) bool {
return false 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 { func AddNummbers(numbers ...int) int {
result := 0 result := 0
for _, number := range numbers { for _, number := range numbers {
@ -121,7 +162,7 @@ func Transpose(in [][]int) [][]int {
return out return out
} }
func Delete (stringArray []string, selector string) []string { func RemoveElementFromStringArray (stringArray []string, selector string) []string {
var returnString []string var returnString []string
for _, str := range stringArray { for _, str := range stringArray {
if str != selector { if str != selector {
@ -158,15 +199,6 @@ func RemoveCharactersFromString(input string, characters string) string {
return strings.Map(filter, input) 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 type sortRunes []rune
func (s sortRunes) Less(i, j int) bool { func (s sortRunes) Less(i, j int) bool {
@ -194,7 +226,6 @@ type Pair struct {
type PairList []Pair type PairList []Pair
func RankByValueAmount(wordFrequencies map[string]int) PairList{ func RankByValueAmount(wordFrequencies map[string]int) PairList{
pl := make(PairList, len(wordFrequencies)) pl := make(PairList, len(wordFrequencies))
i := 0 i := 0