Skip to content

Commit 4256a40

Browse files
author
Teiva Harsanyi
committed
Day 19
1 parent 66f5af3 commit 4256a40

29 files changed

+874
-0
lines changed

2016/day15-go/go.mod

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

2016/day15-go/go.sum

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
9+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
11+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
12+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
13+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
15+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
16+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
17+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

2016/day15-go/input.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Disc #1 has 17 positions; at time=0, it is at position 5.
2+
Disc #2 has 19 positions; at time=0, it is at position 8.
3+
Disc #3 has 7 positions; at time=0, it is at position 1.
4+
Disc #4 has 13 positions; at time=0, it is at position 7.
5+
Disc #5 has 5 positions; at time=0, it is at position 1.
6+
Disc #6 has 3 positions; at time=0, it is at position 0.

2016/day15-go/main.go

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"io"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
func fs1(input io.Reader) (int, error) {
11+
scanner := bufio.NewScanner(input)
12+
var discs []Disc
13+
for scanner.Scan() {
14+
s := scanner.Text()
15+
idx := indexAll(s, " ")
16+
positions, err := strconv.Atoi(s[idx[2]+1 : idx[3]])
17+
if err != nil {
18+
return 0, nil
19+
}
20+
position, err := strconv.Atoi(s[idx[10]+1 : len(s)-1])
21+
if err != nil {
22+
return 0, nil
23+
}
24+
discs = append(discs, Disc{positions, position})
25+
}
26+
27+
for i := 0; ; i++ {
28+
if find(0, discs, 0, i+1) {
29+
return i, nil
30+
}
31+
}
32+
}
33+
34+
func find(i int, discs []Disc, previousPosition int, time int) bool {
35+
if i == len(discs) {
36+
return true
37+
}
38+
39+
position := discs[i].getPosition(time)
40+
if i != 0 && position != previousPosition {
41+
return false
42+
}
43+
44+
return find(i+1, discs, position, time+1)
45+
}
46+
47+
type Disc struct {
48+
positions int
49+
position int
50+
}
51+
52+
func (d Disc) getPosition(time int) int {
53+
return (d.position + time) % d.positions
54+
}
55+
56+
func indexAll(s string, search string) []int {
57+
i := 0
58+
var res []int
59+
for i < len(s) {
60+
index := strings.Index(s[i:], search)
61+
if index == -1 {
62+
return res
63+
}
64+
res = append(res, index+i)
65+
i += index + len(search)
66+
}
67+
return res
68+
}
69+
70+
func fs2(input io.Reader) (int, error) {
71+
scanner := bufio.NewScanner(input)
72+
var discs []Disc
73+
for scanner.Scan() {
74+
s := scanner.Text()
75+
idx := indexAll(s, " ")
76+
positions, err := strconv.Atoi(s[idx[2]+1 : idx[3]])
77+
if err != nil {
78+
return 0, nil
79+
}
80+
position, err := strconv.Atoi(s[idx[10]+1 : len(s)-1])
81+
if err != nil {
82+
return 0, nil
83+
}
84+
discs = append(discs, Disc{positions, position})
85+
}
86+
discs = append(discs, Disc{11, 0})
87+
88+
for i := 0; ; i++ {
89+
if find(0, discs, 0, i+1) {
90+
return i, nil
91+
}
92+
}
93+
}

2016/day15-go/main_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestFs1Test(t *testing.T) {
12+
f, err := os.Open("test.txt")
13+
require.NoError(t, err)
14+
v, err := fs1(f)
15+
require.NoError(t, err)
16+
assert.Equal(t, 5, v)
17+
}
18+
19+
func TestFs1Input(t *testing.T) {
20+
f, err := os.Open("input.txt")
21+
require.NoError(t, err)
22+
v, err := fs1(f)
23+
require.NoError(t, err)
24+
assert.Equal(t, 16824, v)
25+
}
26+
27+
func TestFs2Input(t *testing.T) {
28+
f, err := os.Open("input.txt")
29+
require.NoError(t, err)
30+
v, err := fs2(f)
31+
require.NoError(t, err)
32+
assert.Equal(t, 3543984, v)
33+
}

2016/day15-go/test.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Disc #1 has 5 positions; at time=0, it is at position 4.
2+
Disc #2 has 2 positions; at time=0, it is at position 1.

2016/day16-go/go.mod

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

2016/day16-go/go.sum

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
9+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
11+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
12+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
13+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
15+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
16+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
17+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

2016/day16-go/input.txt

Whitespace-only changes.

2016/day16-go/main.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"io"
6+
"strings"
7+
)
8+
9+
func fs(a string, disk int) (string, error) {
10+
for {
11+
a = random(a)
12+
if len(a) >= disk {
13+
break
14+
}
15+
}
16+
17+
return checksum(a[:disk]), nil
18+
}
19+
20+
func random(a string) string {
21+
sb := strings.Builder{}
22+
sb.Grow(len(a))
23+
for i := len(a) - 1; i >= 0; i-- {
24+
c := a[i]
25+
if c == '0' {
26+
sb.WriteByte('1')
27+
} else {
28+
sb.WriteByte('0')
29+
}
30+
}
31+
32+
return a + "0" + sb.String()
33+
}
34+
35+
func checksum(a string) string {
36+
for {
37+
sb := strings.Builder{}
38+
sb.Grow(len(a) / 2)
39+
for i := 0; i < len(a)-1; i += 2 {
40+
c1 := a[i]
41+
c2 := a[i+1]
42+
if c1 == c2 {
43+
sb.WriteByte('1')
44+
} else {
45+
sb.WriteByte('0')
46+
}
47+
}
48+
49+
a = sb.String()
50+
if len(a)%2 != 0 {
51+
return a
52+
}
53+
}
54+
}
55+
56+
func fs2(input io.Reader) (int, error) {
57+
scanner := bufio.NewScanner(input)
58+
for scanner.Scan() {
59+
line := scanner.Text()
60+
_ = line
61+
}
62+
63+
return 42, nil
64+
}

2016/day16-go/main_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestRandom(t *testing.T) {
11+
assert.Equal(t, "100", random("1"))
12+
assert.Equal(t, "001", random("0"))
13+
assert.Equal(t, "11111000000", random("11111"))
14+
assert.Equal(t, "1111000010100101011110000", random("111100001010"))
15+
}
16+
17+
func TestChecksum(t *testing.T) {
18+
assert.Equal(t, "100", checksum("110010110100"))
19+
}
20+
21+
func TestFs1Input(t *testing.T) {
22+
v, err := fs("01111010110010011", 272)
23+
require.NoError(t, err)
24+
assert.Equal(t, "00100111000101111", v)
25+
}
26+
27+
func TestFs2Input(t *testing.T) {
28+
v, err := fs("01111010110010011", 35651584)
29+
require.NoError(t, err)
30+
assert.Equal(t, "11101110011100110", v)
31+
}

2016/day16-go/test.txt

Whitespace-only changes.

2016/day17-go/go.mod

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

2016/day17-go/go.sum

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
9+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
11+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
12+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
13+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
15+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
16+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
17+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

2016/day17-go/input.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)