Skip to content

Commit 92ff1cf

Browse files
committed
feat(2020-24/angch): Faster, using neighbourcount
1 parent ebde394 commit 92ff1cf

1 file changed

Lines changed: 34 additions & 13 deletions

File tree

2020-24/angch/main.go

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,52 @@ func do(fileName string) (ret1 int, ret2 int) {
6060
}
6161

6262
// log.Println(hexmap)
63-
64-
for _, v := range hexmap {
65-
if v {
63+
neighbourcount := make(map[Hex]int)
64+
for c, v2 := range hexmap {
65+
if v2 {
6666
ret1++
67+
68+
for _, v := range dir {
69+
v.I += c.I
70+
v.J += c.J
71+
v.K += c.K
72+
73+
neighbourcount[v]++
74+
}
6775
}
6876
}
6977

7078
for day := 1; day <= 100; day++ {
7179
hexmap2 := make(map[Hex]bool)
80+
neighbourcount2 := make(map[Hex]int)
7281
min, max := minmax(hexmap)
7382
for i := min.I - 1; i <= max.I+1; i++ {
7483
for j := min.J - 1; j <= max.J+1; j++ {
7584
for k := min.K - 1; k <= max.K+1; k++ {
7685
c := Hex{i, j, k}
77-
n := neighbour(hexmap, c)
86+
n := neighbourcount[c]
87+
// n := neighbour(hexmap, c)
7888
if hexmap[c] {
7989
if n == 0 || n > 2 {
8090
// hexmap2[c] = false
8191
} else {
8292
hexmap2[c] = true
93+
for _, v := range dir {
94+
v.I += c.I
95+
v.J += c.J
96+
v.K += c.K
97+
neighbourcount2[v]++
98+
}
8399
}
84100
} else {
85101
if n == 2 {
86102
hexmap2[c] = true
103+
for _, v := range dir {
104+
v.I += c.I
105+
v.J += c.J
106+
v.K += c.K
107+
neighbourcount2[v]++
108+
}
87109
} else {
88110
// hexmap2[c] = false
89111
}
@@ -94,14 +116,14 @@ func do(fileName string) (ret1 int, ret2 int) {
94116
hexmap = hexmap2
95117

96118
count := 0
97-
for _, v := range hexmap {
98-
if v {
119+
neighbourcount = neighbourcount2
120+
for _, v2 := range hexmap {
121+
if v2 {
99122
count++
100123
}
101124
}
102125
ret2 = count
103-
log.Println("Day", day, count)
104-
126+
// log.Println("Day", day, count)
105127
}
106128

107129
return ret1, ret2
@@ -146,12 +168,11 @@ func minmax(hexmap map[Hex]bool) (Hex, Hex) {
146168
func neighbour(hexmap map[Hex]bool, c Hex) int {
147169
count := 0
148170
for _, v := range dir {
149-
d := c
150-
d.I += v.I
151-
d.J += v.J
152-
d.K += v.K
171+
v.I += c.I
172+
v.J += c.J
173+
v.K += c.K
153174

154-
if hexmap[d] {
175+
if hexmap[v] {
155176
count++
156177
}
157178
}

0 commit comments

Comments
 (0)