|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "io" |
| 6 | + |
| 7 | + lib "github.com/teivah/advent-of-code" |
| 8 | +) |
| 9 | + |
| 10 | +func fs1(input io.Reader) int { |
| 11 | + scanner := bufio.NewScanner(input) |
| 12 | + ports := make(map[int][]int) |
| 13 | + for scanner.Scan() { |
| 14 | + line := scanner.Text() |
| 15 | + del := lib.NewDelimiter(line, "/") |
| 16 | + |
| 17 | + in := del.GetInt(0) |
| 18 | + out := del.GetInt(1) |
| 19 | + ports[in] = append(ports[in], out) |
| 20 | + ports[out] = append(ports[out], in) |
| 21 | + } |
| 22 | + |
| 23 | + p := &Ports{ports} |
| 24 | + return best(p, 0, 0) |
| 25 | +} |
| 26 | + |
| 27 | +func copy(s []int) []int { |
| 28 | + res := make([]int, len(s)) |
| 29 | + for i := 0; i < len(s); i++ { |
| 30 | + res[i] = s[i] |
| 31 | + } |
| 32 | + return res |
| 33 | +} |
| 34 | + |
| 35 | +func best(p *Ports, lastOut int, cur int) int { |
| 36 | + v, exists := p.ports[lastOut] |
| 37 | + if !exists { |
| 38 | + return cur |
| 39 | + } |
| 40 | + |
| 41 | + outs := copy(v) |
| 42 | + max := cur |
| 43 | + for _, out := range outs { |
| 44 | + a := p.remove(lastOut, out) |
| 45 | + b := p.remove(out, lastOut) |
| 46 | + max = lib.Max(max, best(p, out, cur+lastOut+out)) |
| 47 | + b() |
| 48 | + a() |
| 49 | + } |
| 50 | + return max |
| 51 | +} |
| 52 | + |
| 53 | +type Ports struct { |
| 54 | + ports map[int][]int |
| 55 | +} |
| 56 | + |
| 57 | +func (p *Ports) remove(in, out int) func() { |
| 58 | + if len(p.ports[in]) == 1 { |
| 59 | + delete(p.ports, in) |
| 60 | + return func() { |
| 61 | + p.ports[in] = []int{out} |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + res := make([]int, 0, len(p.ports[in])-1) |
| 66 | + |
| 67 | + i := 0 |
| 68 | + s := p.ports[in] |
| 69 | + for ; ; i++ { |
| 70 | + if s[i] != out { |
| 71 | + res = append(res, s[i]) |
| 72 | + continue |
| 73 | + } |
| 74 | + i++ |
| 75 | + break |
| 76 | + } |
| 77 | + for ; i < len(s); i++ { |
| 78 | + res = append(res, s[i]) |
| 79 | + } |
| 80 | + |
| 81 | + p.ports[in] = res |
| 82 | + return func() { |
| 83 | + p.ports[in] = append(p.ports[in], out) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func fs2(input io.Reader) int { |
| 88 | + scanner := bufio.NewScanner(input) |
| 89 | + ports := make(map[int][]int) |
| 90 | + for scanner.Scan() { |
| 91 | + line := scanner.Text() |
| 92 | + del := lib.NewDelimiter(line, "/") |
| 93 | + |
| 94 | + in := del.GetInt(0) |
| 95 | + out := del.GetInt(1) |
| 96 | + ports[in] = append(ports[in], out) |
| 97 | + ports[out] = append(ports[out], in) |
| 98 | + } |
| 99 | + |
| 100 | + p := &Ports{ports} |
| 101 | + _, strength := strengthLongest(p, 0, 0, 0) |
| 102 | + return strength |
| 103 | +} |
| 104 | + |
| 105 | +func strengthLongest(p *Ports, lastOut int, curLength, curStrength int) (int, int) { |
| 106 | + v, exists := p.ports[lastOut] |
| 107 | + if !exists { |
| 108 | + return curLength, curStrength |
| 109 | + } |
| 110 | + |
| 111 | + outs := copy(v) |
| 112 | + maxLength := 0 |
| 113 | + maxStrength := 0 |
| 114 | + for _, out := range outs { |
| 115 | + a := p.remove(lastOut, out) |
| 116 | + b := p.remove(out, lastOut) |
| 117 | + l, s := strengthLongest(p, out, curLength+2, curStrength+lastOut+out) |
| 118 | + if l > maxLength { |
| 119 | + maxLength = l |
| 120 | + maxStrength = s |
| 121 | + } else if l == maxLength { |
| 122 | + if s > maxStrength { |
| 123 | + maxLength = l |
| 124 | + maxStrength = s |
| 125 | + } |
| 126 | + } |
| 127 | + b() |
| 128 | + a() |
| 129 | + } |
| 130 | + return maxLength, maxStrength |
| 131 | +} |
0 commit comments