mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2026-04-09 02:23:36 +00:00
add day-20
This commit is contained in:
38
day-20/internal/two/graph.go
Normal file
38
day-20/internal/two/graph.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/onyx-and-iris/aoc2024/day-20/internal/point"
|
||||
|
||||
"github.com/elliotchance/orderedmap/v3"
|
||||
)
|
||||
|
||||
type graph struct {
|
||||
start, end point.Point
|
||||
data []string
|
||||
}
|
||||
|
||||
func newGraph(data []string) *graph {
|
||||
return &graph{data: data}
|
||||
}
|
||||
|
||||
func (g *graph) String() string {
|
||||
return strings.Join(g.data, "\n")
|
||||
}
|
||||
|
||||
func (g *graph) valueAt(p point.Point) rune {
|
||||
return rune(g.data[p.Y][p.X])
|
||||
}
|
||||
|
||||
func (g *graph) debug(path *orderedmap.OrderedMap[point.Point, int]) string {
|
||||
temp := slices.Clone(g.data)
|
||||
for n := range path.Keys() {
|
||||
if g.valueAt(n) == 'X' {
|
||||
continue
|
||||
}
|
||||
temp[n.Y] = replaceAtIndex(temp[n.Y], 'O', n.X)
|
||||
}
|
||||
return strings.Join(temp, "\n")
|
||||
}
|
||||
38
day-20/internal/two/solve.go
Normal file
38
day-20/internal/two/solve.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math"
|
||||
|
||||
"github.com/onyx-and-iris/aoc2024/day-20/internal/one"
|
||||
"github.com/onyx-and-iris/aoc2024/day-20/internal/point"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func Solve(buf []byte) (int, error) {
|
||||
r := bytes.NewReader(buf)
|
||||
graph, err := parseLines(r)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
log.Debugf("\n%s\n", graph.debug(one.Path))
|
||||
|
||||
var count int
|
||||
for pair1 := one.Path.Front(); pair1 != nil; pair1 = pair1.Next() {
|
||||
for pair2 := one.Path.Front(); pair2 != nil; pair2 = pair2.Next() {
|
||||
if manhatten(pair1.Key, pair2.Key) > 20 ||
|
||||
pair1.Value-pair2.Value-manhatten(pair1.Key, pair2.Key) < 100 {
|
||||
continue
|
||||
}
|
||||
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func manhatten(a, b point.Point) int {
|
||||
return int(math.Abs(float64(a.X)-float64(b.X)) + math.Abs(float64(a.Y)-float64(b.Y)))
|
||||
}
|
||||
15
day-20/internal/two/solve_internal_test.go
Normal file
15
day-20/internal/two/solve_internal_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//go:embed testdata/input.txt
|
||||
var data []byte
|
||||
|
||||
func BenchmarkSolve(b *testing.B) {
|
||||
os.Stdout, _ = os.Open(os.DevNull)
|
||||
Solve(data)
|
||||
}
|
||||
44
day-20/internal/two/util.go
Normal file
44
day-20/internal/two/util.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/onyx-and-iris/aoc2024/day-20/internal/point"
|
||||
)
|
||||
|
||||
func parseLines(r io.Reader) (*graph, error) {
|
||||
graph := newGraph(make([]string, 0))
|
||||
|
||||
var linecount int
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
if strings.Contains(line, "S") {
|
||||
indx := strings.Index(line, "S")
|
||||
graph.start = point.New(indx, linecount)
|
||||
}
|
||||
if strings.Contains(line, "E") {
|
||||
indx := strings.Index(line, "E")
|
||||
graph.end = point.New(indx, linecount)
|
||||
}
|
||||
|
||||
graph.data = append(graph.data, line)
|
||||
|
||||
linecount++
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return graph, nil
|
||||
}
|
||||
|
||||
func replaceAtIndex(s string, r rune, i int) string {
|
||||
out := []rune(s)
|
||||
out[i] = r
|
||||
return string(out)
|
||||
}
|
||||
Reference in New Issue
Block a user