blob: 1550bcb928a0815e20d6997bebaada7654994e14 (
plain)
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
|
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
func main() {
f, err := os.Open("input")
if err != nil {
log.Fatalf("%+v", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
pos := 0
depth := 0
for scanner.Scan() {
elts := strings.Split(scanner.Text(), " ")
i, err := strconv.Atoi(elts[1])
if err != nil {
log.Fatalf("%+v", err)
}
switch elts[0] {
case "forward":
pos += i
case "down":
depth += i
case "up":
depth -= i
default:
panic(elts[0])
}
}
fmt.Println(pos * depth)
}
|