2021-12-12 16:15:16 +01:00
package main
import (
"AOC2021/src/helper"
"fmt"
"os"
"strings"
"unicode"
)
func main ( ) {
args := os . Args [ 1 : ]
input , err := helper . GetInput ( args [ 0 ] )
if err != nil {
fmt . Println ( err )
}
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 ] )
}
2021-12-12 16:35:15 +01:00
if splitLine [ 1 ] != "start" && splitLine [ 0 ] != "end" {
routeMap [ splitLine [ 0 ] ] = append ( routeMap [ splitLine [ 0 ] ] , splitLine [ 1 ] )
}
2021-12-12 16:15:16 +01:00
}
for key , val := range routeMap {
fmt . Printf ( "%s : %s \n" , key , val )
}
//part1
allRoutesCounter := 0
2021-12-12 16:16:32 +01:00
getRoute ( & routeMap , [ ] string { "start" } , & allRoutesCounter , 1 )
2021-12-12 16:15:16 +01:00
fmt . Println ( allRoutesCounter )
//part2
allRoutesCounter = 0
2021-12-12 16:16:32 +01:00
getRoute ( & routeMap , [ ] string { "start" } , & allRoutesCounter , 2 )
2021-12-12 16:15:16 +01:00
fmt . Println ( allRoutesCounter )
}
2021-12-12 16:16:32 +01:00
func getRoute ( routeMap * map [ string ] [ ] string , drivenRoute [ ] string , allRoutesCounter * int , part int ) {
2021-12-12 16:15:16 +01:00
if drivenRoute [ len ( drivenRoute ) - 1 ] == "end" {
* allRoutesCounter ++
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 )
2021-12-12 16:16:32 +01:00
getRoute ( routeMap , newDrivenRoute , allRoutesCounter , part )
2021-12-12 16:15:16 +01:00
}
}
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
}