-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
60 lines (52 loc) · 1.07 KB
/
utils.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
package aoc2020
import (
"bufio"
log "github.com/sirupsen/logrus"
"os"
"strconv"
)
func readFile(file *os.File) []string {
scanner := bufio.NewScanner(file)
var lines []string
for scanner.Scan() {
num := scanner.Text()
lines = append(lines, num)
}
return lines
}
func RemoveDuplicates(elements []string) []string {
encountered := map[string]bool{}
out := []string{}
for v := range elements {
if encountered[elements[v]] == true {
} else {
encountered[elements[v]] = true
out = append(out, elements[v])
}
}
return out
}
func GetLines(filepath string) []string {
inputFile, err := os.Open("input")
if err != nil {
log.Infof("Could not read file: %v", err)
}
return readFile(inputFile)
}
func GetLinesInt(filepath string) []int {
inputFile, err := os.Open("input")
if err != nil {
log.Infof("Could not read file: %v", err)
}
scanner := bufio.NewScanner(inputFile)
var lines []int
for scanner.Scan() {
str := scanner.Text()
num, err := strconv.Atoi(str)
if err != nil {
log.WithError(err)
}
lines = append(lines, num)
}
return lines
}