level1+2
This commit is contained in:
commit
0e8ba1987d
|
@ -0,0 +1 @@
|
|||
.vscode
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,58 @@
|
|||
package level1
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func SumGroup(in []int) int {
|
||||
result := 0
|
||||
for _, data := range in {
|
||||
result += data
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func GetBiggestIntGroup(in [][]int) int {
|
||||
max := 0
|
||||
for _, data := range in {
|
||||
total := SumGroup(data)
|
||||
if total > max {
|
||||
max = total
|
||||
}
|
||||
}
|
||||
return max
|
||||
}
|
||||
|
||||
func GetNBiggest(in [][]int, num int) []int {
|
||||
output := make([]int, 0, len(in))
|
||||
for _, data := range in {
|
||||
output = append(output, SumGroup(data))
|
||||
}
|
||||
sort.Slice(output, func(x, y int) bool {
|
||||
return output[x] > output[y]
|
||||
})
|
||||
return output[:num]
|
||||
}
|
||||
|
||||
func GetIntGroups(in []string) [][]int {
|
||||
output := make([][]int, 0, 20)
|
||||
group := make([]int, 0, 10)
|
||||
|
||||
for _, data := range in {
|
||||
if strings.TrimSpace(data) == "" {
|
||||
output = append(output, group)
|
||||
group = make([]int, 0, 10)
|
||||
} else {
|
||||
conv, err := strconv.ParseInt(data, 10, 0)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
continue
|
||||
}
|
||||
group = append(group, int(conv))
|
||||
}
|
||||
}
|
||||
return output
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package level1
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSumGroup(t *testing.T) {
|
||||
input := []int{2, 5, 4, 3}
|
||||
result := SumGroup(input[:])
|
||||
if result != 14 {
|
||||
t.Log("Invalid Result, got ", result)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestBiggestSumGroup(t *testing.T) {
|
||||
input := [][]int{{1, 2}, {3, 4}, {1, 2}, {2, 2}}
|
||||
result := GetBiggestIntGroup(input[:])
|
||||
if result != 7 {
|
||||
t.Log("Invalid Result, got ", result)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
package level2
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
Lose = 0
|
||||
Draw = 3
|
||||
Win = 6
|
||||
Rock = 1
|
||||
Paper = 2
|
||||
Scissors = 3
|
||||
)
|
||||
|
||||
func calcScore(in []string) int {
|
||||
x, y := in[0], in[1]
|
||||
switch {
|
||||
case x == "A" && y == "X":
|
||||
return Draw + Rock
|
||||
case x == "A" && y == "Y":
|
||||
return Win + Paper
|
||||
case x == "A" && y == "Z":
|
||||
return Lose + Scissors
|
||||
case x == "B" && y == "X":
|
||||
return Lose + Rock
|
||||
case x == "B" && y == "Y":
|
||||
return Draw + Paper
|
||||
case x == "B" && y == "Z":
|
||||
return Win + Scissors
|
||||
case x == "C" && y == "X":
|
||||
return Win + Rock
|
||||
case x == "C" && y == "Y":
|
||||
return Lose + Paper
|
||||
case x == "C" && y == "Z":
|
||||
return Draw + Scissors
|
||||
default:
|
||||
log.Panic("Invalid input")
|
||||
return -1
|
||||
}
|
||||
}
|
||||
|
||||
func calcScore2(in []string) int {
|
||||
x, y := in[0], in[1]
|
||||
switch {
|
||||
case x == "A" && y == "X":
|
||||
return Lose + Scissors
|
||||
case x == "A" && y == "Y":
|
||||
return Draw + Rock
|
||||
case x == "A" && y == "Z":
|
||||
return Win + Paper
|
||||
case x == "B" && y == "X":
|
||||
return Lose + Rock
|
||||
case x == "B" && y == "Y":
|
||||
return Draw + Paper
|
||||
case x == "B" && y == "Z":
|
||||
return Win + Scissors
|
||||
case x == "C" && y == "X":
|
||||
return Lose + Paper
|
||||
case x == "C" && y == "Y":
|
||||
return Draw + Scissors
|
||||
case x == "C" && y == "Z":
|
||||
return Win + Rock
|
||||
default:
|
||||
log.Panic("Invalid input")
|
||||
return -1
|
||||
}
|
||||
}
|
||||
|
||||
func TotalScore(in [][]string) int {
|
||||
total := 0
|
||||
for _, data := range in {
|
||||
total += calcScore(data)
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func TotalScore2(in [][]string) int {
|
||||
total := 0
|
||||
for _, data := range in {
|
||||
total += calcScore2(data)
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func ParseInput(in []string) [][]string {
|
||||
output := make([][]string, 0, len(in))
|
||||
for _, data := range in {
|
||||
components := strings.Fields(data)
|
||||
if len(components) != 2 {
|
||||
log.Panic("Invalid string input\n")
|
||||
return output
|
||||
}
|
||||
output = append(output, components)
|
||||
}
|
||||
return output
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"git.jacknet.io/S.D/AdventOfGode2022/level1"
|
||||
"git.jacknet.io/S.D/AdventOfGode2022/level2"
|
||||
)
|
||||
|
||||
func readFile(s string) []string {
|
||||
f, err := os.Open(s)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return make([]string, 0)
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
// read the file line by line using scanner
|
||||
scanner := bufio.NewScanner(f)
|
||||
|
||||
fileData := make([]string, 0, 100)
|
||||
|
||||
for scanner.Scan() {
|
||||
newString := scanner.Text()
|
||||
fileData = append(fileData, newString)
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
log.Fatal(err)
|
||||
return make([]string, 0)
|
||||
}
|
||||
|
||||
return fileData
|
||||
}
|
||||
|
||||
func runLevelOne() {
|
||||
fileInput := readFile("input/input1")
|
||||
intGroups := level1.GetIntGroups(fileInput)
|
||||
biggest := level1.GetNBiggest(intGroups, 1)
|
||||
log.Println(biggest)
|
||||
biggest = level1.GetNBiggest(intGroups, 3)
|
||||
log.Println(biggest)
|
||||
log.Println(level1.SumGroup(biggest))
|
||||
}
|
||||
|
||||
func runLevelTwo() {
|
||||
fileInput := readFile("input/input2")
|
||||
level2Parsed := level2.ParseInput(fileInput)
|
||||
log.Println(level2.TotalScore(level2Parsed))
|
||||
log.Println(level2.TotalScore2(level2Parsed))
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.Println("Hello World!")
|
||||
runLevelOne()
|
||||
runLevelTwo()
|
||||
}
|
Loading…
Reference in New Issue