Compare commits

..
3 Commits
Author SHA1 Message Date
Daniel Aberger e8bc03c2a6 2nd: small refactor of variables 2021-12-02 15:36:10 +01:00
Daniel Aberger 5a31176884 2nd: refactor 2021-12-02 13:22:34 +01:00
Daniel Aberger 19f9b2999b 1st: refactor 2021-12-02 13:22:27 +01:00
2 changed files with 29 additions and 45 deletions
+9 -9
View File
@@ -9,11 +9,11 @@ import (
)
func Run() {
firstSolution()
secondSolution()
fmt.Printf("1st solution: %d\n", solveFirst())
fmt.Printf("1nd solution: %d\n", solveSecond())
}
func getNumbers() []int {
func readInput() []int {
file, err := os.Open("./pkg/one/input.txt")
if err != nil {
log.Fatal(err)
@@ -34,11 +34,11 @@ func getNumbers() []int {
return numbers
}
func firstSolution() {
func solveFirst() int {
p := 0
i := 0
for k, v := range getNumbers() {
for k, v := range readInput() {
if k == 0 {
p = v
continue
@@ -49,14 +49,14 @@ func firstSolution() {
p = v
}
fmt.Println("First Solution: " + strconv.Itoa(i))
return i
}
func secondSolution() {
func solveSecond() int {
increasedMeasurements := 0
a := make([]int, 3)
b := make([]int, 3)
numbers := getNumbers()
numbers := readInput()
for i := 0; i < len(numbers); i++ {
if i+4 <= len(numbers) {
@@ -76,5 +76,5 @@ func secondSolution() {
}
}
fmt.Println("Second Solution: " + strconv.Itoa(increasedMeasurements))
return increasedMeasurements
}
+20 -36
View File
@@ -9,10 +9,6 @@ import (
"strings"
)
var depth int
var horizontalPosition int
var aim int
func Run() {
commands := readInput()
fmt.Printf("1st final position: %d\n", solve(commands, false))
@@ -20,50 +16,38 @@ func Run() {
}
func solve(commands []string, alt bool) int {
depth = 0
horizontalPosition = 0
aim = 0
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)
x, _ := strconv.Atoi(command[1])
horizontalPosition += x
if alt {
depth += aim * x
}
case "up":
i, _ := strconv.Atoi(command[1])
up(i, alt)
x, _ := strconv.Atoi(command[1])
if !alt {
depth -= x
} else {
aim -= x
}
case "down":
i, _ := strconv.Atoi(command[1])
down(i, alt)
x, _ := strconv.Atoi(command[1])
if !alt {
depth += x
} else {
aim += x
}
}
}
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 {