1
2
3
4
5
6
7
8
9
10
11 package main
12
13 import (
14 "bufio"
15 "fmt"
16 "io"
17 "os"
18 "strconv"
19 "strings"
20 )
21
22 type point struct {
23 x, y int
24 }
25
26 type line struct {
27 begin, end point
28 }
29
30 var zero int
31 var sink int
32
33
34 func tinycall() {
35 }
36
37 func ensure(n int, sl []int) []int {
38 for len(sl) <= n {
39 sl = append(sl, 0)
40 }
41 return sl
42 }
43
44 var cannedInput string = `1
45 1
46 1
47 2
48 2
49 2
50 4
51 4
52 5
53 `
54
55 func test() {
56
57 l := line{point{1 + zero, 2 + zero}, point{3 + zero, 4 + zero}}
58 tinycall()
59 dx := l.end.x - l.begin.x
60 dy := l.end.y - l.begin.y
61 sink = dx + dy
62
63 hist := make([]int, 7)
64 var reader io.Reader = strings.NewReader(cannedInput)
65 if len(os.Args) > 1 {
66 var err error
67 reader, err = os.Open(os.Args[1])
68 if err != nil {
69 fmt.Fprintf(os.Stderr, "There was an error opening %s: %v\n", os.Args[1], err)
70 return
71 }
72 }
73 scanner := bufio.NewScanner(reader)
74 for scanner.Scan() {
75 s := scanner.Text()
76 i, err := strconv.ParseInt(s, 10, 64)
77 if err != nil {
78 fmt.Fprintf(os.Stderr, "There was an error: %v\n", err)
79 return
80 }
81 hist = ensure(int(i), hist)
82 hist[int(i)]++
83 }
84 t := 0
85 n := 0
86 for i, a := range hist {
87 if a == 0 {
88 continue
89 }
90 t += i * a
91 n += a
92 fmt.Fprintf(os.Stderr, "%d\t%d\t%d\t%d\t%d\n", i, a, n, i*a, t)
93 }
94 }
95
96 func main() {
97 growstack()
98 test()
99 }
100
101 var snk string
102
103
104 func growstack() {
105 snk = fmt.Sprintf("%#v,%#v,%#v", 1, true, "cat")
106 }
107
View as plain text