Day 1 & Day 2

This commit is contained in:
Karl Spickermann 2022-12-03 03:40:46 +01:00
commit fb57a80ae3
10 changed files with 2637 additions and 0 deletions

8
.idea/.gitignore vendored Normal file

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

9
.idea/AOC2022.iml Normal file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml Normal file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/AOC2022.iml" filepath="$PROJECT_DIR$/.idea/AOC2022.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/day1" vcs="Git" />
</component>
</project>

1
day1 Submodule

@ -0,0 +1 @@
Subproject commit 19acbd7f9a47614490ff9edf6f5ea6ee44a5f3d2

60
day2/day2.go Normal file

@ -0,0 +1,60 @@
package main
import (
"AOC2022/helper"
"fmt"
"os"
)
func main() {
args := os.Args[1:]
rounds := helper.ReadTextFile(args[0])
sum := 0
for _, round := range rounds {
sum += roundResult(int(round[0])-64, int(round[2])-87)
}
fmt.Println(sum)
sum = 0
for _, round := range rounds {
opponent := int(round[0]) - 64
neededResult := int(round[2]) - 87
move := generateCorrectMove(opponent, neededResult)
sum += roundResult(opponent, move)
}
fmt.Println(sum)
}
func roundResult(opponent, player int) int {
weakness := getWeakness(opponent)
if player == weakness {
return player + 6
}
if opponent == player {
return player + 3
}
return player + 0
}
func getWeakness(input int) int {
tmpWeakness := (input + 1) % 4
if tmpWeakness == 0 {
return 1
}
return tmpWeakness
}
func generateCorrectMove(opponent, neededResult int) int {
weakness := getWeakness(opponent)
if neededResult == 3 {
return weakness
}
if neededResult == 1 {
possibleMove := []int{1, 2, 3}
for _, x := range possibleMove {
if x != weakness && x != opponent {
return x
}
}
}
return opponent
}

2500
day2/input Normal file

File diff suppressed because it is too large Load Diff

3
day2/testInput Normal file

@ -0,0 +1,3 @@
A Y
B X
C Z

3
go.mod Normal file

@ -0,0 +1,3 @@
module AOC2022
go 1.18

39
helper/helper.go Normal file

@ -0,0 +1,39 @@
package helper
import (
"os"
"strings"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func RemoveError[T any](value T, e error) T {
check(e)
return value
}
func ReadTextFile(filePath string) []string {
file, err := os.ReadFile(filePath)
check(err)
return strings.Split(string(file), "\r\n")
}
func FindMax[T int | int64](slice []T) (m T) {
for i, e := range slice {
if i == 0 || e > m {
m = e
}
}
return
}
func Sum[T int | int64](slice []T) (s T) {
for _, e := range slice {
s += e
}
return
}