02: part1+2

This commit is contained in:
ptrcnull 2021-12-04 07:24:29 +01:00
parent 0fb16f2822
commit 2545346566
3 changed files with 1064 additions and 0 deletions

1000
2/input Normal file

File diff suppressed because it is too large Load diff

31
2/part1.go Normal file
View file

@ -0,0 +1,31 @@
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
depth := 0
position := 0
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
tokens := strings.Split(s.Text(), " ")
direction := tokens[0]
value, _ := strconv.Atoi(tokens[1])
switch direction {
case "forward":
position += value
case "up":
depth -= value
case "down":
depth += value
}
}
fmt.Println(depth * position)
}

33
2/part2.go Normal file
View file

@ -0,0 +1,33 @@
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
depth := 0
position := 0
aim := 0
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
tokens := strings.Split(s.Text(), " ")
direction := tokens[0]
value, _ := strconv.Atoi(tokens[1])
switch direction {
case "forward":
position += value
depth += aim * value
case "up":
aim -= value
case "down":
aim += value
}
}
fmt.Println(depth * position)
}