Solve 2024 day 5 in go

This commit is contained in:
SebastianStork 2025-01-11 14:19:16 +01:00
parent ea82a650e0
commit d8a167ef27
4 changed files with 1527 additions and 0 deletions

3
2024/go/day-05/go.mod Normal file
View file

@ -0,0 +1,3 @@
module day-05
go 1.23

1353
2024/go/day-05/input Normal file

File diff suppressed because it is too large Load diff

100
2024/go/day-05/main.go Normal file
View file

@ -0,0 +1,100 @@
package main
import (
"fmt"
"log"
"os"
"slices"
"strconv"
"strings"
)
func main() {
rules, updates, err := readInput()
if err != nil {
log.Fatalln(err)
}
correctSum, correctedSum := calculateMiddlePageSums(rules, updates)
// Part one
fmt.Println("Sum of correct middle pages:", correctSum)
// Part two
fmt.Println("Sum of corrected middle pages:", correctedSum)
}
func readInput() (rules map[int][]int, updates [][]int, err error) {
content, err := os.ReadFile("input")
if err != nil {
return
}
lines := strings.Split(strings.TrimSpace(string(content)), "\n")
rules = make(map[int][]int)
isRules := true
for _, line := range lines {
if line == "" {
isRules = false
continue
}
if isRules {
parts := strings.Split(line, "|")
num1, _ := strconv.Atoi(parts[0])
num2, _ := strconv.Atoi(parts[1])
rules[num1] = append(rules[num1], num2)
} else {
parts := strings.Split(line, ",")
var update []int
for _, part := range parts {
num, _ := strconv.Atoi(part)
update = append(update, num)
}
updates = append(updates, update)
}
}
return
}
func calculateMiddlePageSums(rules map[int][]int, updates [][]int) (correctSum, correctedSum int) {
for _, update := range updates {
if isCorrectlyOrdered(rules, update) {
correctSum += middlePage(update)
} else {
correctedSum += middlePage(correctOrder(rules, update))
}
}
return
}
func isCorrectlyOrdered(rules map[int][]int, pages []int) bool {
return slices.IsSortedFunc(pages, comparePages(rules))
}
func correctOrder(rules map[int][]int, pages []int) []int {
slices.SortFunc(pages, comparePages(rules))
return pages
}
func comparePages(rules map[int][]int) func(int, int) int {
return func(firstPage, secondPage int) int {
for _, ruleNum := range rules[firstPage] {
if secondPage == ruleNum {
return -1
}
}
return 1
}
}
func middlePage(pages []int) int {
return pages[len(pages)/2]
}

View file

@ -0,0 +1,71 @@
package main
import (
"fmt"
"testing"
)
var rules = map[int][]int{
29: {13},
47: {53, 13, 61, 29},
53: {29, 13},
61: {13, 53, 29},
75: {29, 53, 47, 61, 13},
97: {13, 61, 47, 29, 53, 75},
}
var updates = [][]int{
{75, 47, 61, 53, 29},
{97, 61, 53, 29, 13},
{75, 29, 13},
{75, 97, 47, 61, 53},
{61, 13, 29},
{97, 13, 75, 29, 47},
}
func TestIsCorrectlyOrdered(t *testing.T) {
tests := []struct {
input []int
want bool
}{
{updates[0], true},
{updates[1], true},
{updates[2], true},
{updates[3], false},
{updates[4], false},
{updates[5], false},
}
for _, test := range tests {
testName := fmt.Sprint(test.input)
t.Run(testName, func(t *testing.T) {
got := isCorrectlyOrdered(rules, test.input)
if got != test.want {
t.Errorf("got %t, want %t", got, test.want)
}
})
}
}
func TestSumOfMiddlePages(t *testing.T) {
correctSum, correctedSum := calculateMiddlePageSums(rules, updates)
t.Run("correctSum", func(t *testing.T) {
want := 143
got := correctSum
if got != want {
t.Errorf("got %d, want %d", got, want)
}
})
t.Run("correctedSum", func(t *testing.T) {
want := 123
got := correctedSum
if got != want {
t.Errorf("got %d, want %d", got, want)
}
})
}