jlelse
/
aoc21
Archived
1
Fork 0
This commit is contained in:
Jan-Lukas Else 2021-12-12 08:47:47 +01:00
parent e47b886324
commit 333a4c9675
6 changed files with 151 additions and 0 deletions

22
12/input.txt Normal file
View File

@ -0,0 +1,22 @@
zi-end
XR-start
zk-zi
TS-zk
zw-vl
zk-zw
end-po
ws-zw
TS-ws
po-TS
po-YH
po-xk
zi-ws
zk-end
zi-XR
XR-zk
vl-TS
start-zw
vl-start
XR-zw
XR-vl
XR-ws

59
12/main.go Normal file
View File

@ -0,0 +1,59 @@
package main
import (
"log"
"os"
"strings"
"github.com/thoas/go-funk"
)
func main() {
inputBytes, _ := os.ReadFile("input.txt")
input := strings.Split(string(inputBytes), "\n")
log.Println("Puzzle 1:", puzzle1(input))
log.Println("Puzzle 2:", puzzle2(input))
}
func puzzle1(input []string) int {
return do(input, false)
}
func puzzle2(input []string) int {
return do(input, true)
}
func do(input []string, puzzle2 bool) int {
connections := map[string][]string{}
// Read input
for _, line := range input {
split := strings.Split(line, "-")
connections[split[0]] = append(connections[split[0]], split[1])
connections[split[1]] = append(connections[split[1]], split[0])
}
// Find all routes from start to end
return len(route(puzzle2, connections, []string{"start"}, false))
}
func route(puzzle2 bool, connections map[string][]string, visited []string, smallTwice bool) (routes [][]string) {
// Find all routes from last visited to end
for _, next := range connections[visited[len(visited)-1]] {
// Check if next is end
if next == "end" {
routes = append(routes, append(visited, next))
continue
}
// Can visit a single small cave twice, but all remaining caves once
if next[0] >= 'a' && next[0] <= 'z' && funk.ContainsString(visited, next) {
if next != "start" && puzzle2 && !smallTwice {
routes = append(routes, route(puzzle2, connections, append(visited, next), true)...)
}
continue
}
// Check next possible routes
routes = append(routes, route(puzzle2, connections, append(visited, next), smallTwice)...)
}
// Return all found routes
return routes
}

35
12/main_test.go Normal file
View File

@ -0,0 +1,35 @@
package main
import (
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func Test(t *testing.T) {
// Test 1
inputBytes, _ := os.ReadFile("test1.txt")
input := strings.Split(string(inputBytes), "\n")
assert.Equal(t, 10, puzzle1(input))
assert.Equal(t, 36, puzzle2(input))
// Test 2
inputBytes, _ = os.ReadFile("test2.txt")
input = strings.Split(string(inputBytes), "\n")
assert.Equal(t, 19, puzzle1(input))
assert.Equal(t, 103, puzzle2(input))
// Test 3
inputBytes, _ = os.ReadFile("test3.txt")
input = strings.Split(string(inputBytes), "\n")
assert.Equal(t, 226, puzzle1(input))
assert.Equal(t, 3509, puzzle2(input))
}

7
12/test1.txt Normal file
View File

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

10
12/test2.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
12/test3.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