day 2
This commit is contained in:
parent
0b9bdf682d
commit
4b22135336
74
02/main.go
Normal file
74
02/main.go
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user