Compare commits

...
3 Commits
Author SHA1 Message Date
Daniel Aberger e05bebcb74 2nd december: remove redundancy 2021-12-02 12:54:11 +01:00
Daniel Aberger 188e213068 add 2nd december solution 2021-12-02 12:47:13 +01:00
Daniel Aberger 5b5790e2e2 one: remove obsolete code 2021-12-01 12:07:46 +01:00
4 changed files with 1090 additions and 4 deletions
+8 -1
View File
@@ -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()
} }
-3
View File
@@ -58,9 +58,6 @@ func secondSolution() {
b := make([]int, 3) b := make([]int, 3)
numbers := getNumbers() numbers := getNumbers()
a = numbers[0:3]
b = numbers[1:4]
for i := 0; i < len(numbers); i++ { for i := 0; i < len(numbers); i++ {
if i+4 <= len(numbers) { if i+4 <= len(numbers) {
a = numbers[i : i+3] a = numbers[i : i+3]
+1000
View File
File diff suppressed because it is too large Load Diff
+82
View File
@@ -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
}