Archived
add 2nd december solution
This commit is contained in:
+8
-1
@@ -1,7 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "adventofcode/pkg/one"
|
import (
|
||||||
|
"adventofcode/pkg/one"
|
||||||
|
"adventofcode/pkg/two"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
fmt.Println("Day 1")
|
||||||
one.Run()
|
one.Run()
|
||||||
|
fmt.Println("Day 2")
|
||||||
|
two.Run()
|
||||||
}
|
}
|
||||||
|
|||||||
+1000
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,92 @@
|
|||||||
|
package two
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var depth int = 0
|
||||||
|
var horizontalPosition int = 0
|
||||||
|
var aim int = 0
|
||||||
|
|
||||||
|
func Run() {
|
||||||
|
commands := readInput()
|
||||||
|
for _, v := range commands {
|
||||||
|
command := strings.Split(v, " ")
|
||||||
|
switch command[0] {
|
||||||
|
case "forward":
|
||||||
|
i, _ := strconv.Atoi(command[1])
|
||||||
|
forward(i, false)
|
||||||
|
case "up":
|
||||||
|
i, _ := strconv.Atoi(command[1])
|
||||||
|
up(i, false)
|
||||||
|
case "down":
|
||||||
|
i, _ := strconv.Atoi(command[1])
|
||||||
|
down(i, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("1st final position: %d\n", horizontalPosition*depth)
|
||||||
|
|
||||||
|
depth = 0
|
||||||
|
horizontalPosition = 0
|
||||||
|
|
||||||
|
for _, v := range commands {
|
||||||
|
command := strings.Split(v, " ")
|
||||||
|
switch command[0] {
|
||||||
|
case "forward":
|
||||||
|
i, _ := strconv.Atoi(command[1])
|
||||||
|
forward(i, true)
|
||||||
|
case "up":
|
||||||
|
i, _ := strconv.Atoi(command[1])
|
||||||
|
up(i, true)
|
||||||
|
case "down":
|
||||||
|
i, _ := strconv.Atoi(command[1])
|
||||||
|
down(i, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("2nd final position: %d\n", horizontalPosition*depth)
|
||||||
|
}
|
||||||
|
|
||||||
|
func up(d int, alt bool) {
|
||||||
|
if !alt {
|
||||||
|
depth -= d
|
||||||
|
} else {
|
||||||
|
aim -= d
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func down(d int, alt bool) {
|
||||||
|
if !alt {
|
||||||
|
depth += d
|
||||||
|
} else {
|
||||||
|
aim += d
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func forward(d int, alt bool) {
|
||||||
|
horizontalPosition += d
|
||||||
|
if alt {
|
||||||
|
depth += aim * d
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func readInput() []string {
|
||||||
|
file, err := os.Open("./pkg/two/input.txt")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
commands := make([]string, 0)
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
commands = append(commands, scanner.Text())
|
||||||
|
}
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
return commands
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user