From 333a4c96752b9925a1290eedb062b179f75b72df Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Sun, 12 Dec 2021 08:47:47 +0100 Subject: [PATCH] Day 12 --- 12/input.txt | 22 ++++++++++++++++++ 12/main.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 12/main_test.go | 35 +++++++++++++++++++++++++++++ 12/test1.txt | 7 ++++++ 12/test2.txt | 10 +++++++++ 12/test3.txt | 18 +++++++++++++++ 6 files changed, 151 insertions(+) create mode 100644 12/input.txt create mode 100644 12/main.go create mode 100644 12/main_test.go create mode 100644 12/test1.txt create mode 100644 12/test2.txt create mode 100644 12/test3.txt diff --git a/12/input.txt b/12/input.txt new file mode 100644 index 0000000..2705c4a --- /dev/null +++ b/12/input.txt @@ -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 \ No newline at end of file diff --git a/12/main.go b/12/main.go new file mode 100644 index 0000000..e93e0d6 --- /dev/null +++ b/12/main.go @@ -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 +} diff --git a/12/main_test.go b/12/main_test.go new file mode 100644 index 0000000..a607d8c --- /dev/null +++ b/12/main_test.go @@ -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)) +} diff --git a/12/test1.txt b/12/test1.txt new file mode 100644 index 0000000..898cd56 --- /dev/null +++ b/12/test1.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/12/test2.txt b/12/test2.txt new file mode 100644 index 0000000..ef30b81 --- /dev/null +++ b/12/test2.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/12/test3.txt b/12/test3.txt new file mode 100644 index 0000000..da6e083 --- /dev/null +++ b/12/test3.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