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:
86
day-20/internal/one/graph.go
Normal file
86
day-20/internal/one/graph.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package one
|
||||
|
||||
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) isOutOfBounds(p point.Point) bool {
|
||||
return p.X < 1 || p.Y < 1 || p.Y >= len(g.data)-1 || p.X >= len(g.data[p.Y])-1
|
||||
}
|
||||
|
||||
func (g *graph) valueAt(p point.Point) rune {
|
||||
return rune(g.data[p.Y][p.X])
|
||||
}
|
||||
|
||||
func (g *graph) isValidCheatPosition(p point.Point) bool {
|
||||
allowed := []rune{'.', 'S', 'E'}
|
||||
|
||||
ns := neighbours(p)
|
||||
|
||||
return slices.Contains(allowed, g.valueAt(ns[N])) && slices.Contains(allowed, g.valueAt(ns[S])) ||
|
||||
slices.Contains(allowed, g.valueAt(ns[W])) && slices.Contains(allowed, g.valueAt(ns[E]))
|
||||
}
|
||||
|
||||
func (g *graph) getConnectingpoint(dir direction, p point.Point) point.Point {
|
||||
ns := neighbours(p)
|
||||
|
||||
return ns[dir]
|
||||
}
|
||||
|
||||
func (g *graph) set(p point.Point, value rune) {
|
||||
g.data[p.Y] = replaceAtIndex(g.data[p.Y], value, p.X)
|
||||
}
|
||||
|
||||
func (g *graph) path(
|
||||
current point.Point,
|
||||
om *orderedmap.OrderedMap[point.Point, int],
|
||||
dist int,
|
||||
) *orderedmap.OrderedMap[point.Point, int] {
|
||||
if current == g.end {
|
||||
return om
|
||||
}
|
||||
|
||||
var n point.Point
|
||||
for _, n = range neighbours(current) {
|
||||
_, ok := om.Get(n)
|
||||
if ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if g.valueAt(n) == '.' || g.valueAt(n) == 'E' {
|
||||
break
|
||||
}
|
||||
}
|
||||
om.Set(n, dist)
|
||||
|
||||
return g.path(n, om, dist+1)
|
||||
}
|
||||
|
||||
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) == cheatChar {
|
||||
continue
|
||||
}
|
||||
temp[n.Y] = replaceAtIndex(temp[n.Y], 'O', n.X)
|
||||
}
|
||||
return strings.Join(temp, "\n")
|
||||
}
|
||||
21
day-20/internal/one/neighbours.go
Normal file
21
day-20/internal/one/neighbours.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package one
|
||||
|
||||
import "github.com/onyx-and-iris/aoc2024/day-20/internal/point"
|
||||
|
||||
type direction int
|
||||
|
||||
const (
|
||||
N direction = iota
|
||||
E
|
||||
S
|
||||
W
|
||||
)
|
||||
|
||||
func neighbours(p point.Point) [4]point.Point {
|
||||
return [4]point.Point{
|
||||
point.New(p.X, p.Y-1), // N
|
||||
point.New(p.X+1, p.Y), // E
|
||||
point.New(p.X, p.Y+1), // S
|
||||
point.New(p.X-1, p.Y), // W
|
||||
}
|
||||
}
|
||||
72
day-20/internal/one/solve.go
Normal file
72
day-20/internal/one/solve.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package one
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math"
|
||||
|
||||
"github.com/onyx-and-iris/aoc2024/day-20/internal/point"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/elliotchance/orderedmap/v3"
|
||||
)
|
||||
|
||||
const cheatChar = 'X'
|
||||
|
||||
var Path *orderedmap.OrderedMap[point.Point, int]
|
||||
|
||||
func Solve(buf []byte) (int, error) {
|
||||
r := bytes.NewReader(buf)
|
||||
graph, err := parseLines(r)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
Path = orderedmap.NewOrderedMap[point.Point, int]()
|
||||
Path.Set(graph.start, 0)
|
||||
Path = graph.path(graph.start, Path, 1)
|
||||
log.Debugf("initial path: %d\n", Path.Len()-1)
|
||||
log.Debugf("Unique path:\n%s\n", graph.debug(Path))
|
||||
|
||||
visited := make(map[point.Point]struct{})
|
||||
|
||||
var sum int
|
||||
for point := range Path.Keys() {
|
||||
for dir, n := range neighbours(point) {
|
||||
if graph.isOutOfBounds(n) {
|
||||
continue
|
||||
}
|
||||
|
||||
if graph.valueAt(n) != '#' {
|
||||
continue
|
||||
}
|
||||
|
||||
if _, ok := visited[n]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if graph.isValidCheatPosition(n) {
|
||||
b := graph.getConnectingpoint(direction(dir), n)
|
||||
graph.set(n, 'X')
|
||||
|
||||
v1, ok := Path.Get(point)
|
||||
if !ok {
|
||||
log.Fatalf("%v not in path", point)
|
||||
}
|
||||
v2, ok := Path.Get(b)
|
||||
if !ok {
|
||||
log.Fatalf("%v not in path", b)
|
||||
}
|
||||
diff := int(math.Abs(float64(v1-v2))) - 1
|
||||
|
||||
log.Debugf("diff: %d\n", diff)
|
||||
if diff >= 100 {
|
||||
sum++
|
||||
}
|
||||
|
||||
visited[n] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sum, nil
|
||||
}
|
||||
15
day-20/internal/one/solve_internal_test.go
Normal file
15
day-20/internal/one/solve_internal_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package one
|
||||
|
||||
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/one/util.go
Normal file
44
day-20/internal/one/util.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package one
|
||||
|
||||
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