Skip to content

Commit 4041195

Browse files
committed
feat(2020-05/angch): More optimization
1 parent e5e13c2 commit 4041195

1 file changed

Lines changed: 30 additions & 10 deletions

File tree

2020-15/angch/main.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ type Vec struct {
7272
Two int32
7373
}
7474

75-
func do2(input []int) (int, int) {
76-
ret1, ret2 := 0, 0
77-
75+
func do2(input []int) (ret1 int, ret2 int) {
7876
nums := make([]Vec, 30000000)
7977
last, pos := int32(0), int32(0)
8078
for _, v := range input {
@@ -83,9 +81,8 @@ func do2(input []int) (int, int) {
8381
nums[last] = Vec{One: int32(pos)}
8482
}
8583

86-
// k := nums[last]
84+
k := nums[last]
8785
for {
88-
k := nums[last]
8986
if k.Two == 0 {
9087
last = 0
9188
} else {
@@ -101,29 +98,52 @@ func do2(input []int) (int, int) {
10198

10299
k = nums[last]
103100
if k.One == 0 {
104-
nums[last] = Vec{
101+
k = Vec{
105102
One: pos,
106103
}
107104
} else if k.Two == 0 {
108-
nums[last] = Vec{
105+
k = Vec{
109106
One: pos - k.One,
110107
Two: pos,
111108
}
112109
} else {
113-
nums[last] = Vec{
110+
k = Vec{
114111
One: pos - k.Two,
115112
Two: pos,
116113
}
117114
}
115+
nums[last] = k
118116
}
119117
ret2 = int(last)
118+
return
119+
}
120120

121-
return ret1, ret2
121+
func do3(input []int) (ret1 int, ret2 int) {
122+
nums := make([]int32, 30000000)
123+
last, pos := int32(0), int32(0)
124+
for _, v := range input {
125+
pos++
126+
last = int32(v)
127+
nums[last] = pos
128+
}
129+
130+
for prev := int32(0); pos < 30000000; pos++ {
131+
if pos == 2020 {
132+
ret1 = int(last)
133+
}
134+
prev, nums[last] = nums[last], pos
135+
if prev == 0 {
136+
last = 0
137+
} else {
138+
last = pos - prev
139+
}
140+
}
141+
return ret1, int(last)
122142
}
123143

124144
func main() {
125145
// log.Println(do("test.txt"))
126146
// log.Println(do("test2.txt"))
127147
// log.Println(do("input.txt"))
128-
log.Println(do2([]int{12, 20, 0, 6, 1, 17, 7}))
148+
log.Println(do3([]int{12, 20, 0, 6, 1, 17, 7}))
129149
}

0 commit comments

Comments
 (0)