Initial commit
This commit is contained in:
56
Y22/day1.go
Normal file
56
Y22/day1.go
Normal file
@ -0,0 +1,56 @@
|
||||
package Y22
|
||||
|
||||
import (
|
||||
"adventofcode/helpers"
|
||||
"sort"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Day1() helpers.DayResults {
|
||||
day1Example := helpers.ReadFileByLine("inputs/Y22/day1-example.txt")
|
||||
day1 := helpers.ReadFileByLine("inputs/Y22/day1.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
Y22/day2.go
Normal file
93
Y22/day2.go
Normal file
@ -0,0 +1,93 @@
|
||||
package Y22
|
||||
|
||||
import (
|
||||
"adventofcode/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("inputs/Y22/day2-example.txt")
|
||||
day2 := helpers.ReadFileByLine("inputs/Y22/day2.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
|
||||
}
|
Reference in New Issue
Block a user