Archived
Compare commits
4
Commits
main
..
e05bebcb74
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e05bebcb74 | ||
|
|
188e213068 | ||
|
|
5b5790e2e2 | ||
|
|
5cf912ae02 |
+14
@@ -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()
|
||||||
|
}
|
||||||
+2000
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,80 @@
|
|||||||
|
package one
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Run() {
|
||||||
|
firstSolution()
|
||||||
|
secondSolution()
|
||||||
|
}
|
||||||
|
|
||||||
|
func getNumbers() []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 firstSolution() {
|
||||||
|
p := 0
|
||||||
|
i := 0
|
||||||
|
|
||||||
|
for k, v := range getNumbers() {
|
||||||
|
if k == 0 {
|
||||||
|
p = v
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if p < v {
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
p = v
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("First Solution: " + strconv.Itoa(i))
|
||||||
|
}
|
||||||
|
|
||||||
|
func secondSolution() {
|
||||||
|
increasedMeasurements := 0
|
||||||
|
a := make([]int, 3)
|
||||||
|
b := make([]int, 3)
|
||||||
|
numbers := getNumbers()
|
||||||
|
|
||||||
|
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++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Second Solution: " + strconv.Itoa(increasedMeasurements))
|
||||||
|
}
|
||||||
+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