02: part1+2
This commit is contained in:
parent
0fb16f2822
commit
2545346566
3 changed files with 1064 additions and 0 deletions
31
2/part1.go
Normal file
31
2/part1.go
Normal 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
33
2/part2.go
Normal 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)
|
||||
}
|
Loading…
Reference in a new issue