Skip to content

Commit 880f7c3

Browse files
committed
feat(2020-15/angch): ez
1 parent 7ea7fc0 commit 880f7c3

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

2020-15/angch/input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
12,20,0,6,1,17,7

2020-15/angch/main.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"log"
6+
"os"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
func do(fileName string) (int, int) {
12+
ret1, ret2 := 0, 0
13+
14+
file, err := os.Open(fileName)
15+
if err != nil {
16+
log.Fatal(err)
17+
}
18+
defer file.Close()
19+
scanner := bufio.NewScanner(file)
20+
21+
nums := make(map[int][]int)
22+
last := 0
23+
pos := 0
24+
for scanner.Scan() {
25+
l := scanner.Text()
26+
a := strings.Split(l, ",")
27+
for k, v := range a {
28+
num, _ := strconv.Atoi(v)
29+
_, ok := nums[num]
30+
if !ok {
31+
nums[num] = make([]int, 0, 2)
32+
}
33+
nums[num] = append(nums[num], k+1)
34+
pos = k + 1
35+
last = num
36+
}
37+
}
38+
39+
out := 0
40+
for {
41+
if len(nums[last]) == 1 {
42+
out = 0
43+
} else {
44+
out = nums[last][len(nums[last])-1] - nums[last][len(nums[last])-2]
45+
}
46+
pos++
47+
48+
_, ok := nums[out]
49+
if !ok {
50+
nums[out] = make([]int, 0, 2)
51+
}
52+
if len(nums[out]) <= 1 {
53+
nums[out] = append(nums[out], pos)
54+
} else {
55+
nums[out][0] = nums[out][1]
56+
nums[out][1] = pos
57+
}
58+
last = out
59+
if pos == 2020 {
60+
ret1 = last
61+
}
62+
if pos >= 30000000 {
63+
// if pos >= 2020 {
64+
break
65+
}
66+
}
67+
ret2 = last
68+
69+
return ret1, ret2
70+
}
71+
72+
func main() {
73+
log.Println(do("test.txt"))
74+
// log.Println(do("test2.txt"))
75+
log.Println(do("input.txt"))
76+
}

0 commit comments

Comments
 (0)