-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.go
58 lines (46 loc) · 936 Bytes
/
common.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
package main
import (
"bufio"
"os"
"strconv"
"strings"
)
type Map []Transform
type Transform struct {
Destination, Source, Length int
}
func parse() ([]int, [7]Map) {
scanner := bufio.NewScanner(os.Stdin)
seeds := []int{}
scanner.Scan()
fields := strings.Fields(scanner.Text())
for i := 1; i < len(fields); i++ {
num, _ := strconv.Atoi(fields[i])
seeds = append(seeds, num)
}
return seeds, parseMaps(scanner)
}
func parseMaps(scanner *bufio.Scanner) [7]Map {
maps := [7]Map{}
t := -1
for scanner.Scan() {
line := scanner.Text()
if line == "" {
continue
}
if strings.Contains(line, "map:") {
t++
continue
}
fields := strings.Fields(line)
dest, _ := strconv.Atoi(fields[0])
source, _ := strconv.Atoi(fields[1])
length, _ := strconv.Atoi(fields[2])
maps[t] = append(maps[t], Transform{
Destination: dest,
Source: source,
Length: length,
})
}
return maps
}