From 9f412dbd296eea57b8121d6247a58229da716632 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 10 Dec 2020 09:31:02 -0600 Subject: [PATCH] Day 10 possibly complete but way to resource intensive --- 2020/day10/input | 97 ++++++++++++++++++++++++++++++++++ 2020/day10/main.go | 117 ++++++++++++++++++++++++++++++++++++++++++ 2020/day10/out | 40 +++++++++++++++ 2020/day10/testinput | 11 ++++ 2020/day10/testinput2 | 31 +++++++++++ 5 files changed, 296 insertions(+) create mode 100644 2020/day10/input create mode 100644 2020/day10/main.go create mode 100644 2020/day10/out create mode 100644 2020/day10/testinput create mode 100644 2020/day10/testinput2 diff --git a/2020/day10/input b/2020/day10/input new file mode 100644 index 0000000..5b4b267 --- /dev/null +++ b/2020/day10/input @@ -0,0 +1,97 @@ +8 +131 +91 +35 +47 +116 +105 +121 +56 +62 +94 +72 +13 +82 +156 +102 +12 +59 +31 +138 +46 +120 +7 +127 +126 +111 +2 +123 +22 +69 +18 +157 +75 +149 +88 +81 +23 +98 +132 +1 +63 +142 +37 +133 +61 +112 +122 +128 +155 +145 +139 +66 +42 +134 +24 +60 +9 +28 +17 +29 +101 +148 +96 +68 +25 +19 +6 +67 +113 +55 +40 +135 +97 +79 +48 +159 +14 +43 +86 +36 +41 +85 +87 +119 +30 +108 +80 +152 +158 +151 +32 +78 +150 +95 +3 +52 +49 diff --git a/2020/day10/main.go b/2020/day10/main.go new file mode 100644 index 0000000..8cdcaed --- /dev/null +++ b/2020/day10/main.go @@ -0,0 +1,117 @@ +package main + +import ( + "fmt" + "sort" + + h "git.bullercodeworks.com/brian/adventofcode/helpers" +) + +func main() { + fmt.Println("# Day 10") + inp := h.StdinToIntSlice() + sort.Ints(inp) + jumps := part1(inp) + fmt.Println("## Part 1") + fmt.Println(jumps[1] * jumps[3]) + + fmt.Println("## Part 2") + fmt.Println(part2(inp)) +} + +func part1(inp []int) map[int]int { + jumps := make(map[int]int) + var curr int + var used []int + wrk := make([]int, len(inp)) + copy(wrk, inp) + for len(used) < len(inp) { + var next int + for i := range wrk { + if i == 0 { + next = wrk[i] + } else { + diff := wrk[i] - curr + if diff > 0 && diff < next-curr { + next = wrk[i] + } + } + } + jumps[next-curr]++ + curr = next + used = append(used, curr) + var nextList []int + for i := range wrk { + if wrk[i] != curr { + nextList = append(nextList, wrk[i]) + } + } + wrk = make([]int, len(nextList)) + copy(wrk, nextList) + } + jumps[3]++ + return jumps +} + +var prevs map[int][]int +var psbs map[int][][]int + +func part2(inp []int) int { + psbs = make(map[int][][]int) + psbs[0] = [][]int{{0}} + prevs = make(map[int][]int) + var phone int + for i := range inp { + if inp[i] > phone { + phone = inp[i] + } + } + // Add the outlet + inp = append(inp, 0) + // Add the phone + phone = phone + 3 + inp = append(inp, phone) + for i := range inp { + prevs[inp[i]] = findPrevPoss(inp[i], inp) + } + + total := len(findConfigs(phone)) + return total +} + +func findConfigs(curr int) [][]int { + if p, ok := psbs[curr]; ok { + return p + } else { + var ret [][]int + for _, v := range prevs[curr] { + rec := findConfigs(v) + if len(rec) > 0 { + for _, rv := range rec { + ret = append(ret, append([]int{curr}, rv...)) + } + } else { + ret = [][]int{{curr}} + } + } + psbs[curr] = ret + return ret + } +} + +// Find the options immediately preceeding curr +func findPrevPoss(curr int, inp []int) []int { + if curr == 0 { + return []int{} + } + var next []int + for i := range inp { + if inp[i] >= curr { + continue + } + if curr-inp[i] <= 3 { + next = append(next, inp[i]) + } + } + return next +} diff --git a/2020/day10/out b/2020/day10/out new file mode 100644 index 0000000..80fc522 --- /dev/null +++ b/2020/day10/out @@ -0,0 +1,40 @@ +# Day 10 +## Part 1 +35 +## Part 2 +22 Finding configs +22 Digging +19 Finding configs +19 Digging +16 Finding configs +16 Digging +15 Finding configs +15 Digging +12 Finding configs +12 Digging +10 Finding configs +10 Digging +7 Finding configs +7 Digging +4 Finding configs +4 Digging +1 Finding configs +1 Digging +0 Finding configs +0 Already known [] +5 Finding configs +5 Digging +4 Finding configs +4 Already known [[4 1]] +6 Finding configs +6 Digging +4 Finding configs +4 Already known [[4 1]] +5 Finding configs +5 Already known [[5 4 1]] +11 Finding configs +11 Digging +10 Finding configs +10 Already known [[10 7 4 1] [10 7 5 4 1] [10 7 6 4 1] [10 7 6 5 4 1]] +map[0:[] 1:[[1]] 4:[[4 1]] 5:[[5 4 1]] 6:[[6 4 1] [6 5 4 1]] 7:[[7 4 1] [7 5 4 1] [7 6 4 1] [7 6 5 4 1]] 10:[[10 7 4 1] [10 7 5 4 1] [10 7 6 4 1] [10 7 6 5 4 1]] 11:[[11 10 7 4 1] [11 10 7 5 4 1] [11 10 7 6 4 1] [11 10 7 6 5 4 1]] 12:[[12 10 7 4 1] [12 10 7 5 4 1] [12 10 7 6 4 1] [12 10 7 6 5 4 1] [12 11 10 7 4 1] [12 11 10 7 5 4 1] [12 11 10 7 6 4 1] [12 11 10 7 6 5 4 1]] 15:[[15 12 10 7 4 1] [15 12 10 7 5 4 1] [15 12 10 7 6 4 1] [15 12 10 7 6 5 4 1] [15 12 11 10 7 4 1] [15 12 11 10 7 5 4 1] [15 12 11 10 7 6 4 1] [15 12 11 10 7 6 5 4 1]] 16:[[16 15 12 10 7 4 1] [16 15 12 10 7 5 4 1] [16 15 12 10 7 6 4 1] [16 15 12 10 7 6 5 4 1] [16 15 12 11 10 7 4 1] [16 15 12 11 10 7 5 4 1] [16 15 12 11 10 7 6 4 1] [16 15 12 11 10 7 6 5 4 1]] 19:[[19 16 15 12 10 7 4 1] [19 16 15 12 10 7 5 4 1] [19 16 15 12 10 7 6 4 1] [19 16 15 12 10 7 6 5 4 1] [19 16 15 12 11 10 7 4 1] [19 16 15 12 11 10 7 5 4 1] [19 16 15 12 11 10 7 6 4 1] [19 16 15 12 11 10 7 6 5 4 1]] 22:[[22 19 16 15 12 10 7 4 1] [22 19 16 15 12 10 7 5 4 1] [22 19 16 15 12 10 7 6 4 1] [22 19 16 15 12 10 7 6 5 4 1] [22 19 16 15 12 11 10 7 4 1] [22 19 16 15 12 11 10 7 5 4 1] [22 19 16 15 12 11 10 7 6 4 1] [22 19 16 15 12 11 10 7 6 5 4 1]]] +8 diff --git a/2020/day10/testinput b/2020/day10/testinput new file mode 100644 index 0000000..ec4a03f --- /dev/null +++ b/2020/day10/testinput @@ -0,0 +1,11 @@ +16 +10 +15 +5 +1 +11 +7 +19 +6 +12 +4 diff --git a/2020/day10/testinput2 b/2020/day10/testinput2 new file mode 100644 index 0000000..e6376dc --- /dev/null +++ b/2020/day10/testinput2 @@ -0,0 +1,31 @@ +28 +33 +18 +42 +31 +14 +46 +20 +48 +47 +24 +23 +49 +45 +19 +38 +39 +11 +1 +32 +25 +35 +8 +17 +7 +9 +4 +2 +34 +10 +3