Archived
Compare commits
3
Commits
e05bebcb74
...
4e18054987
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4e18054987 | ||
|
|
d33f37336a | ||
|
|
2a0771a313 |
+8
-1
@@ -1,7 +1,14 @@
|
||||
package main
|
||||
|
||||
import "adventofcode/pkg/one"
|
||||
import (
|
||||
"adventofcode/pkg/one"
|
||||
"adventofcode/pkg/two"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Day 1")
|
||||
one.Run()
|
||||
fmt.Println("Day 2")
|
||||
two.Run()
|
||||
}
|
||||
|
||||
@@ -58,9 +58,6 @@ func secondSolution() {
|
||||
b := make([]int, 3)
|
||||
numbers := getNumbers()
|
||||
|
||||
a = numbers[0:3]
|
||||
b = numbers[1:4]
|
||||
|
||||
for i := 0; i < len(numbers); i++ {
|
||||
if i+4 <= len(numbers) {
|
||||
a = numbers[i : i+3]
|
||||
|
||||
+1000
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,82 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var depth int
|
||||
var horizontalPosition int
|
||||
var aim int
|
||||
|
||||
func Run() {
|
||||
commands := readInput()
|
||||
fmt.Printf("1st final position: %d\n", solve(commands, false))
|
||||
fmt.Printf("2nd final position: %d\n", solve(commands, true))
|
||||
}
|
||||
|
||||
func solve(commands []string, alt bool) int {
|
||||
depth = 0
|
||||
horizontalPosition = 0
|
||||
aim = 0
|
||||
|
||||
for _, v := range commands {
|
||||
command := strings.Split(v, " ")
|
||||
switch command[0] {
|
||||
case "forward":
|
||||
i, _ := strconv.Atoi(command[1])
|
||||
forward(i, alt)
|
||||
case "up":
|
||||
i, _ := strconv.Atoi(command[1])
|
||||
up(i, alt)
|
||||
case "down":
|
||||
i, _ := strconv.Atoi(command[1])
|
||||
down(i, alt)
|
||||
}
|
||||
}
|
||||
return 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