Compare commits

..
7 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
Daniel Aberger 4e18054987 2nd: remove redundancy 2021-12-02 12:56:44 +01:00
Daniel Aberger d33f37336a add 2nd december solution 2021-12-02 12:56:44 +01:00
Daniel Aberger 2a0771a313 1st: remove obsolete code 2021-12-02 12:56:38 +01:00
Daniel Aberger 5cf912ae02 add 1st december 2021-12-01 11:41:37 +01:00
6 changed files with 3163 additions and 0 deletions
+14
View File
@@ -0,0 +1,14 @@
package main
import (
"adventofcode/pkg/one"
"adventofcode/pkg/two"
"fmt"
)
func main() {
fmt.Println("Day 1")
one.Run()
fmt.Println("Day 2")
two.Run()
}
+3
View File
@@ -0,0 +1,3 @@
module adventofcode
go 1.17
+2000
View File
File diff suppressed because it is too large Load Diff
+80
View File
@@ -0,0 +1,80 @@
package one
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
)
func Run() {
fmt.Printf("1st solution: %d\n", solveFirst())
fmt.Printf("1nd solution: %d\n", solveSecond())
}
func readInput() []int {
file, err := os.Open("./pkg/one/input.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
numbers := make([]int, 0)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
num, err := strconv.Atoi(scanner.Text())
if err != nil {
log.Fatal(err)
}
numbers = append(numbers, num)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return numbers
}
func solveFirst() int {
p := 0
i := 0
for k, v := range readInput() {
if k == 0 {
p = v
continue
}
if p < v {
i++
}
p = v
}
return i
}
func solveSecond() int {
increasedMeasurements := 0
a := make([]int, 3)
b := make([]int, 3)
numbers := readInput()
for i := 0; i < len(numbers); i++ {
if i+4 <= len(numbers) {
a = numbers[i : i+3]
b = numbers[i+1 : i+4]
aSum := 0
bSum := 0
for _, v := range a {
aSum += v
}
for _, v := range b {
bSum += v
}
if bSum > aSum {
increasedMeasurements++
}
}
}
return increasedMeasurements
}
+1000
View File
File diff suppressed because it is too large Load Diff
+66
View File
@@ -0,0 +1,66 @@
package two
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
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":
x, _ := strconv.Atoi(command[1])
horizontalPosition += x
if alt {
depth += aim * x
}
case "up":
x, _ := strconv.Atoi(command[1])
if !alt {
depth -= x
} else {
aim -= x
}
case "down":
x, _ := strconv.Atoi(command[1])
if !alt {
depth += x
} else {
aim += x
}
}
}
return horizontalPosition * depth
}
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
}