File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ 12,20,0,6,1,17,7
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments