-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (45 loc) · 1000 Bytes
/
main.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
func main() {
var horiz int64 = 0
var depth int64 = 0
var aim int64 = 0
file, err := os.Open("../input.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
d := strings.Split(scanner.Text(), " ")
value, err := strconv.ParseInt(d[1], 10, 64)
if err != nil {
log.Fatal(err)
}
switch d[0] {
case "up":
//depth = depth - value
aim = aim - value
case "down":
//depth = depth + value
aim = aim + value
case "forward":
horiz = horiz + value
depth = depth + (aim * value)
default:
log.Fatalf("Cound not parse input %s", scanner.Text())
}
fmt.Printf("Current instruction is %s by %d | horizontal:%d depth:%d\n", d[0], value, horiz, depth)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
fmt.Printf("Final Position is horizontal:%d depth:%d with a product of %d", horiz, depth, horiz*depth)
}