This commit is contained in:
David 2021-12-02 12:59:40 -05:00
parent 0b9bdf682d
commit 4b22135336

74
02/main.go Normal file
View File

@ -0,0 +1,74 @@
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)
}