feat: add day 5 2024
This commit is contained in:
parent
77a0669f45
commit
aa0a2b7b7d
28
aoc2024/day05/example.txt
Normal file
28
aoc2024/day05/example.txt
Normal file
@ -0,0 +1,28 @@
|
||||
47|53
|
||||
97|13
|
||||
97|61
|
||||
97|47
|
||||
75|29
|
||||
61|13
|
||||
75|53
|
||||
29|13
|
||||
97|29
|
||||
53|29
|
||||
61|53
|
||||
97|53
|
||||
61|29
|
||||
47|13
|
||||
75|47
|
||||
97|75
|
||||
47|61
|
||||
75|61
|
||||
47|29
|
||||
75|13
|
||||
53|13
|
||||
|
||||
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
|
1379
aoc2024/day05/input.txt
Normal file
1379
aoc2024/day05/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
83
aoc2024/day05/main.go
Normal file
83
aoc2024/day05/main.go
Normal file
@ -0,0 +1,83 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
aoc "go.sour.is/advent-of-code"
|
||||
)
|
||||
|
||||
// var log = aoc.Log
|
||||
|
||||
func main() { aoc.MustResult(aoc.Runner(run)) }
|
||||
|
||||
type result struct {
|
||||
valuePT1 int
|
||||
valuePT2 int
|
||||
}
|
||||
|
||||
func (r result) String() string { return fmt.Sprintf("%#v", r) }
|
||||
|
||||
func run(scan *bufio.Scanner) (*result, error) {
|
||||
|
||||
ordering := make(map[int]aoc.Set[int])
|
||||
var updates [][]int
|
||||
|
||||
for scan.Scan() {
|
||||
txt := scan.Text()
|
||||
|
||||
if txt == "" {
|
||||
break
|
||||
}
|
||||
|
||||
var key, val int
|
||||
_, err := fmt.Sscanf(txt, "%d|%d", &val, &key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, ok := ordering[key]; !ok {
|
||||
ordering[key] = aoc.NewSet[int]()
|
||||
}
|
||||
s := ordering[key]
|
||||
s.Add(val)
|
||||
}
|
||||
|
||||
var sum int
|
||||
var sum2 int
|
||||
|
||||
outer:
|
||||
for scan.Scan() {
|
||||
txt := scan.Text()
|
||||
|
||||
arr := aoc.SliceMap(aoc.Atoi, strings.Split(txt, ",")...)
|
||||
|
||||
for i, v := range arr {
|
||||
m, ok := ordering[v]
|
||||
if (i > 0 && !ok) || !m.ContainsAll(arr[:i]...) {
|
||||
sort.SliceStable(arr, func(i, j int) bool {
|
||||
if a, ok := ordering[arr[i]]; ok && a.Has(arr[j]) {
|
||||
return true
|
||||
}
|
||||
|
||||
if b, ok := ordering[arr[j]]; ok && b.Has(arr[i]) {
|
||||
return false
|
||||
}
|
||||
|
||||
return false
|
||||
})
|
||||
|
||||
sum2 += arr[len(arr)/2]
|
||||
|
||||
continue outer
|
||||
}
|
||||
}
|
||||
|
||||
sum += arr[len(arr)/2]
|
||||
updates = append(updates, arr)
|
||||
}
|
||||
|
||||
return &result{sum, sum2}, nil
|
||||
}
|
41
aoc2024/day05/main_test.go
Normal file
41
aoc2024/day05/main_test.go
Normal file
@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
_ "embed"
|
||||
|
||||
"github.com/matryer/is"
|
||||
)
|
||||
|
||||
//go:embed example.txt
|
||||
var example []byte
|
||||
|
||||
//go:embed input.txt
|
||||
var input []byte
|
||||
|
||||
func TestExample(t *testing.T) {
|
||||
is := is.New(t)
|
||||
scan := bufio.NewScanner(bytes.NewReader(example))
|
||||
|
||||
result, err := run(scan)
|
||||
is.NoErr(err)
|
||||
|
||||
t.Log(result)
|
||||
is.Equal(result.valuePT1, 143)
|
||||
is.Equal(result.valuePT2, 123)
|
||||
}
|
||||
|
||||
func TestSolution(t *testing.T) {
|
||||
is := is.New(t)
|
||||
scan := bufio.NewScanner(bytes.NewReader(input))
|
||||
|
||||
result, err := run(scan)
|
||||
is.NoErr(err)
|
||||
|
||||
t.Log(result)
|
||||
is.Equal(result.valuePT1, 7307)
|
||||
is.Equal(result.valuePT2, 4713)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user