Skip to content

Commit 703c4a4

Browse files
author
Teiva Harsanyi
committed
Day 25
1 parent 6499362 commit 703c4a4

File tree

13 files changed

+552
-0
lines changed

13 files changed

+552
-0
lines changed

2017/day24-go/go.mod

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module day24-go
2+
3+
go 1.18
4+
5+
require (
6+
github.com/davecgh/go-spew v1.1.1 // indirect
7+
github.com/pmezard/go-difflib v1.0.0 // indirect
8+
github.com/stretchr/objx v0.5.0 // indirect
9+
github.com/stretchr/testify v1.8.1 // indirect
10+
github.com/teivah/advent-of-code v0.0.3 // indirect
11+
gopkg.in/yaml.v3 v3.0.1 // indirect
12+
)

2017/day24-go/go.sum

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
8+
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
9+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
10+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
11+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
12+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
13+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
14+
github.com/teivah/advent-of-code v0.0.3 h1:0WwyH+3mH2OZJ5NsYJrJIGcX/aB2Tcl6M9fnAiyY92s=
15+
github.com/teivah/advent-of-code v0.0.3/go.mod h1:65W3/TlV/tOdtAKvZHawTrSlBP4giocTo0jYJOp0F2k=
16+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
17+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
18+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
19+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

2017/day24-go/input.txt

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
25/13
2+
4/43
3+
42/42
4+
39/40
5+
17/18
6+
30/7
7+
12/12
8+
32/28
9+
9/28
10+
1/1
11+
16/7
12+
47/43
13+
34/16
14+
39/36
15+
6/4
16+
3/2
17+
10/49
18+
46/50
19+
18/25
20+
2/23
21+
3/21
22+
5/24
23+
46/26
24+
50/19
25+
26/41
26+
1/50
27+
47/41
28+
39/50
29+
12/14
30+
11/19
31+
28/2
32+
38/47
33+
5/5
34+
38/34
35+
39/39
36+
17/34
37+
42/16
38+
32/23
39+
13/21
40+
28/6
41+
6/20
42+
1/30
43+
44/21
44+
11/28
45+
14/17
46+
33/33
47+
17/43
48+
31/13
49+
11/21
50+
31/39
51+
0/9
52+
13/50
53+
10/14
54+
16/10
55+
3/24
56+
7/0
57+
50/50

2017/day24-go/main.go

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
}

2017/day24-go/main_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestFs1Test(t *testing.T) {
13+
f, err := os.Open("test.txt")
14+
require.NoError(t, err)
15+
assert.Equal(t, 31, fs1(f))
16+
}
17+
18+
func TestFs1Input(t *testing.T) {
19+
f, err := os.Open("input.txt")
20+
require.NoError(t, err)
21+
assert.Equal(t, 42, fs1(f))
22+
}
23+
24+
func TestFs2Test(t *testing.T) {
25+
f, err := os.Open("test.txt")
26+
require.NoError(t, err)
27+
assert.Equal(t, 19, fs2(f))
28+
}
29+
30+
func TestFs2Input(t *testing.T) {
31+
f, err := os.Open("input.txt")
32+
require.NoError(t, err)
33+
assert.Equal(t, 1841, fs2(f))
34+
}
35+
36+
func TestPorts_remove(t *testing.T) {
37+
p := &Ports{map[int][]int{1: {1, 2, 3}}}
38+
a := p.remove(1, 1)
39+
fmt.Println(p.ports)
40+
b := p.remove(1, 2)
41+
fmt.Println(p.ports)
42+
c := p.remove(1, 3)
43+
fmt.Println(p.ports)
44+
c()
45+
b()
46+
a()
47+
fmt.Println(p.ports)
48+
}

2017/day24-go/test.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
0/2
2+
2/2
3+
2/3
4+
3/4
5+
3/5
6+
0/1
7+
10/1
8+
9/10

2017/day25-go/go.mod

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module day25-go
2+
3+
go 1.18
4+
5+
require (
6+
github.com/davecgh/go-spew v1.1.1 // indirect
7+
github.com/pmezard/go-difflib v1.0.0 // indirect
8+
github.com/stretchr/objx v0.5.0 // indirect
9+
github.com/stretchr/testify v1.8.1 // indirect
10+
github.com/teivah/advent-of-code v0.0.3 // indirect
11+
gopkg.in/yaml.v3 v3.0.1 // indirect
12+
)

2017/day25-go/go.sum

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
8+
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
9+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
10+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
11+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
12+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
13+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
14+
github.com/teivah/advent-of-code v0.0.3 h1:0WwyH+3mH2OZJ5NsYJrJIGcX/aB2Tcl6M9fnAiyY92s=
15+
github.com/teivah/advent-of-code v0.0.3/go.mod h1:65W3/TlV/tOdtAKvZHawTrSlBP4giocTo0jYJOp0F2k=
16+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
17+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
18+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
19+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

2017/day25-go/input.txt

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
In state A:
3+
If the current value is 0:
4+
- Write the value 1.
5+
- Move one slot to the right.
6+
- Continue with state B.
7+
If the current value is 1:
8+
- Write the value 1.
9+
- Move one slot to the left.
10+
- Continue with state E.
11+
12+
In state B:
13+
If the current value is 0:
14+
- Write the value 1.
15+
- Move one slot to the right.
16+
- Continue with state C.
17+
If the current value is 1:
18+
- Write the value 1.
19+
- Move one slot to the right.
20+
- Continue with state F.
21+
22+
In state C:
23+
If the current value is 0:
24+
- Write the value 1.
25+
- Move one slot to the left.
26+
- Continue with state D.
27+
If the current value is 1:
28+
- Write the value 0.
29+
- Move one slot to the right.
30+
- Continue with state B.
31+
32+
In state D:
33+
If the current value is 0:
34+
- Write the value 1.
35+
- Move one slot to the right.
36+
- Continue with state E.
37+
If the current value is 1:
38+
- Write the value 0.
39+
- Move one slot to the left.
40+
- Continue with state C.
41+
42+
In state E:
43+
If the current value is 0:
44+
- Write the value 1.
45+
- Move one slot to the left.
46+
- Continue with state A.
47+
If the current value is 1:
48+
- Write the value 0.
49+
- Move one slot to the right.
50+
- Continue with state D.
51+
52+
In state F:
53+
If the current value is 0:
54+
- Write the value 1.
55+
- Move one slot to the right.
56+
- Continue with state A.
57+
If the current value is 1:
58+
- Write the value 1.
59+
- Move one slot to the right.
60+
- Continue with state C.

0 commit comments

Comments
 (0)