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() { func Run() {
firstSolution() fmt.Printf("1st solution: %d\n", solveFirst())
secondSolution() fmt.Printf("1nd solution: %d\n", solveSecond())
} }
func getNumbers() []int { func readInput() []int {
file, err := os.Open("./pkg/one/input.txt") file, err := os.Open("./pkg/one/input.txt")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@@ -34,11 +34,11 @@ func getNumbers() []int {
return numbers return numbers
} }
func firstSolution() { func solveFirst() int {
p := 0 p := 0
i := 0 i := 0
for k, v := range getNumbers() { for k, v := range readInput() {
if k == 0 { if k == 0 {
p = v p = v
continue continue
@@ -49,14 +49,14 @@ func firstSolution() {
p = v p = v
} }
fmt.Println("First Solution: " + strconv.Itoa(i)) return i
} }
func secondSolution() { func solveSecond() int {
increasedMeasurements := 0 increasedMeasurements := 0
a := make([]int, 3) a := make([]int, 3)
b := make([]int, 3) b := make([]int, 3)
numbers := getNumbers() numbers := readInput()
for i := 0; i < len(numbers); i++ { for i := 0; i < len(numbers); i++ {
if i+4 <= len(numbers) { 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" "strings"
) )
var depth int
var horizontalPosition int
var aim int
func Run() { func Run() {
commands := readInput() commands := readInput()
fmt.Printf("1st final position: %d\n", solve(commands, false)) fmt.Printf("1st final position: %d\n", solve(commands, false))
@@ -20,50 +16,38 @@ func Run() {
} }
func solve(commands []string, alt bool) int { func solve(commands []string, alt bool) int {
depth = 0 depth := 0
horizontalPosition = 0 horizontalPosition := 0
aim = 0 aim := 0
for _, v := range commands { for _, v := range commands {
command := strings.Split(v, " ") command := strings.Split(v, " ")
switch command[0] { switch command[0] {
case "forward": case "forward":
i, _ := strconv.Atoi(command[1]) x, _ := strconv.Atoi(command[1])
forward(i, alt) horizontalPosition += x
if alt {
depth += aim * x
}
case "up": case "up":
i, _ := strconv.Atoi(command[1]) x, _ := strconv.Atoi(command[1])
up(i, alt) if !alt {
depth -= x
} else {
aim -= x
}
case "down": case "down":
i, _ := strconv.Atoi(command[1]) x, _ := strconv.Atoi(command[1])
down(i, alt) if !alt {
depth += x
} else {
aim += x
}
} }
} }
return horizontalPosition * depth 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 { func readInput() []string {
file, err := os.Open("./pkg/two/input.txt") file, err := os.Open("./pkg/two/input.txt")
if err != nil { if err != nil {