Add Go
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-12-03 21:50:29 +01:00
parent d8d3821113
commit f228b7b494
9 changed files with 299 additions and 3 deletions

56
data/Y22/day1/day1.go Normal file
View File

@ -0,0 +1,56 @@
package Y22
import (
"adventofcode/src-go/helpers"
"sort"
"strconv"
)
func Day1() helpers.DayResults {
day1Example := helpers.ReadFileByLine("data/Y22/day1/example.txt")
day1 := helpers.ReadFileByLine("data/Y22/day1/data.txt")
calorieCount := calculateCalories(day1Example)
part1Example := calorieCount[len(calorieCount)-1]
part2Example := calorieCount[len(calorieCount)-1] + calorieCount[len(calorieCount)-2] + calorieCount[len(calorieCount)-3]
calorieCount = calculateCalories(day1)
part1Result := calorieCount[len(calorieCount)-1]
part2Result := calorieCount[len(calorieCount)-1] + calorieCount[len(calorieCount)-2] + calorieCount[len(calorieCount)-3]
results := helpers.DayResults{
Day: 1,
Year: 2022,
Part1Example: part1Example,
Part1: part1Result,
Part2Example: part2Example,
Part2: part2Result,
}
return results
}
func calculateCalories(data []string) []int {
var calorieCount []int
total := 0
for _, line := range data {
if line == "" {
calorieCount = append(calorieCount, total)
total = 0
continue
}
val, err := strconv.Atoi(line)
if err != nil {
panic(err)
}
total += val
}
sort.Slice(calorieCount, func(i, j int) bool {
return calorieCount[i] < calorieCount[j]
})
return calorieCount
}

93
data/Y22/day2/day2.go Normal file
View File

@ -0,0 +1,93 @@
package Y22
import (
"adventofcode/src-go/helpers"
"strings"
)
const (
aRock string = "A"
aPaper string = "B"
aScissors string = "C"
bRock string = "X"
bPaper string = "Y"
bScissors string = "Z"
resultLose string = "X"
resultDraw string = "Y"
resultWin string = "Z"
)
var scoreTable = map[string]map[string]int{
aRock: {
bRock: 4,
bPaper: 8,
bScissors: 3,
},
aPaper: {
bRock: 1,
bPaper: 5,
bScissors: 9,
},
aScissors: {
bRock: 7,
bPaper: 2,
bScissors: 6,
},
}
var winOrLoseTable = map[string]map[string]string{
aRock: {
resultWin: bPaper,
resultDraw: bRock,
resultLose: bScissors,
},
aPaper: {
resultWin: bScissors,
resultDraw: bPaper,
resultLose: bRock,
},
aScissors: {
resultWin: bRock,
resultDraw: bScissors,
resultLose: bPaper,
},
}
func Day2() helpers.DayResults {
day2Example := helpers.ReadFileByLine("data/Y22/day2/example.txt")
day2 := helpers.ReadFileByLine("data/Y22/day2/data.txt")
results := helpers.DayResults{
Day: 2,
Year: 2022,
Part1Example: part1(day2Example),
Part1: part1(day2),
Part2Example: part2(day2Example),
Part2: part2(day2),
}
return results
}
func part1(input []string) int {
score := 0
for _, line := range input {
data := strings.Split(line, " ")
score += scoreTable[data[0]][data[1]]
}
return score
}
func part2(input []string) int {
score := 0
for _, line := range input {
data := strings.Split(line, " ")
scoreValue := winOrLoseTable[data[0]][data[1]]
score += scoreTable[data[0]][scoreValue]
}
return score
}

15
data/Y22/y22.go Normal file
View File

@ -0,0 +1,15 @@
package Y22
import (
Y22D1 "adventofcode/data/Y22/day1"
Y22D2 "adventofcode/data/Y22/day2"
"adventofcode/src-go/helpers"
)
func Day1() helpers.DayResults {
return Y22D1.Day1()
}
func Day2() helpers.DayResults {
return Y22D2.Day2()
}