-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.go
102 lines (85 loc) · 1.32 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
func main() {
lines := readLines("input.txt")
{
fmt.Println("--- Part One ---")
fmt.Println(len(lines))
}
{
fmt.Println("--- Part Two ---")
fmt.Println()
}
}
func readLines(filename string) []string {
file, err := os.Open(filename)
check(err)
defer file.Close()
scanner := bufio.NewScanner(file)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines
}
func readNumbers(filename string) []int {
file, err := os.Open(filename)
check(err)
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanWords)
var numbers []int
for scanner.Scan() {
numbers = append(numbers, toInt(scanner.Text()))
}
return numbers
}
func readFile(filename string) string {
bytes, err := ioutil.ReadFile(filename)
check(err)
return strings.TrimSpace(string(bytes))
}
func toInt(s string) int {
result, err := strconv.Atoi(s)
check(err)
return result
}
func check(err error) {
if err != nil {
panic(err)
}
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
func sign(x int) int {
if x > 0 {
return 1
}
if x < 0 {
return -1
}
return 0
}
func min(x, y int) int {
if y < x {
return y
}
return x
}
func max(x, y int) int {
if y > x {
return y
}
return x
}