75 lines
1.3 KiB
Go
75 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
start := time.Now()
|
|
partOne()
|
|
duration := time.Since(start)
|
|
partTwo()
|
|
duration2 := time.Since(start)
|
|
fmt.Printf("p1: %s, p2: %s\n", duration, duration2-duration)
|
|
}
|
|
|
|
func makeScanner(test bool) *bufio.Scanner {
|
|
var f *os.File
|
|
if test {
|
|
f, _ = os.Open("inputs/testinput")
|
|
} else {
|
|
f, _ = os.Open("inputs/input")
|
|
}
|
|
reader := bufio.NewReader(f)
|
|
return bufio.NewScanner(reader)
|
|
}
|
|
|
|
func mustAtoi(line string) int {
|
|
i, _ := strconv.Atoi(line)
|
|
return i
|
|
}
|
|
|
|
func partOne() {
|
|
scanner := makeScanner(false)
|
|
|
|
var up, down, forward int
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
dir, i := strings.Split(line, " ")[0], mustAtoi(strings.Split(line, " ")[1])
|
|
switch dir {
|
|
case "forward":
|
|
forward += i
|
|
case "down":
|
|
down += i
|
|
case "up":
|
|
up += i
|
|
}
|
|
}
|
|
fmt.Println(forward * (down - up))
|
|
}
|
|
|
|
func partTwo() {
|
|
scanner := makeScanner(false)
|
|
|
|
var horizontal, vertical, aim int
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
dir, i := strings.Split(line, " ")[0], mustAtoi(strings.Split(line, " ")[1])
|
|
switch dir {
|
|
case "forward":
|
|
horizontal += i
|
|
vertical += (i * aim)
|
|
case "down":
|
|
aim += i
|
|
case "up":
|
|
aim -= i
|
|
}
|
|
}
|
|
fmt.Println(horizontal * vertical)
|
|
}
|