aboutsummaryrefslogtreecommitdiff
path: root/2021/02/second.go
blob: e6d0e9432c5647e7affe76aff122dd81bafd4390 (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
43
44
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)

	aim := 0
	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
			depth += i * aim
		case "down":
			aim += i
		case "up":
			aim -= i
		default:
			panic(elts[0])
		}
	}
	fmt.Println(pos * depth)
}