mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2026-04-09 02:23:36 +00:00
refactor using graph struct and adjacency function
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package one
|
||||
|
||||
type direction int
|
||||
|
||||
const (
|
||||
N = iota
|
||||
N direction = iota
|
||||
NE
|
||||
E
|
||||
SE
|
||||
|
||||
17
day-04/internal/one/graph.go
Normal file
17
day-04/internal/one/graph.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package one
|
||||
|
||||
type graph struct {
|
||||
data []string
|
||||
}
|
||||
|
||||
func newGraph() *graph {
|
||||
return &graph{}
|
||||
}
|
||||
|
||||
func (g *graph) valueAt(p point) rune {
|
||||
return rune(g.data[p.y][p.x])
|
||||
}
|
||||
|
||||
func (g *graph) isOutOfBounds(p point) bool {
|
||||
return p.x < 0 || p.y < 0 || p.y >= len(g.data) || p.x >= len(g.data[p.y])
|
||||
}
|
||||
@@ -1,58 +1,14 @@
|
||||
package one
|
||||
|
||||
type neighbour struct {
|
||||
x int
|
||||
y int
|
||||
direction int
|
||||
}
|
||||
|
||||
func newNeighbour(direction, x, y int) neighbour {
|
||||
switch direction {
|
||||
case N:
|
||||
return neighbour{x, y + 1, direction}
|
||||
case NE:
|
||||
return neighbour{x + 1, y + 1, direction}
|
||||
case E:
|
||||
return neighbour{x + 1, y, direction}
|
||||
case SE:
|
||||
return neighbour{x + 1, y - 1, direction}
|
||||
case S:
|
||||
return neighbour{x, y - 1, direction}
|
||||
case SW:
|
||||
return neighbour{x - 1, y - 1, direction}
|
||||
case W:
|
||||
return neighbour{x - 1, y, direction}
|
||||
case NW:
|
||||
return neighbour{x - 1, y + 1, direction}
|
||||
default:
|
||||
return neighbour{}
|
||||
func neighbours(p point) [8]point {
|
||||
return [8]point{
|
||||
{y: p.y - 1, x: p.x, direction: N},
|
||||
{y: p.y - 1, x: p.x + 1, direction: NE},
|
||||
{y: p.y, x: p.x + 1, direction: E},
|
||||
{y: p.y + 1, x: p.x + 1, direction: SE},
|
||||
{y: p.y + 1, x: p.x, direction: S},
|
||||
{y: p.y + 1, x: p.x - 1, direction: SW},
|
||||
{y: p.y, x: p.x - 1, direction: W},
|
||||
{y: p.y - 1, x: p.x - 1, direction: NW},
|
||||
}
|
||||
}
|
||||
|
||||
type neighbours struct {
|
||||
N neighbour
|
||||
NE neighbour
|
||||
E neighbour
|
||||
SE neighbour
|
||||
S neighbour
|
||||
SW neighbour
|
||||
W neighbour
|
||||
NW neighbour
|
||||
}
|
||||
|
||||
func newNeighbours(x, y int) neighbours {
|
||||
return neighbours{
|
||||
newNeighbour(N, x, y),
|
||||
newNeighbour(NE, x, y),
|
||||
newNeighbour(E, x, y),
|
||||
newNeighbour(SE, x, y),
|
||||
newNeighbour(S, x, y),
|
||||
newNeighbour(SW, x, y),
|
||||
newNeighbour(W, x, y),
|
||||
newNeighbour(NW, x, y),
|
||||
}
|
||||
}
|
||||
|
||||
func (n neighbours) all() [8]neighbour {
|
||||
return [8]neighbour{n.N, n.NE, n.E, n.SE, n.S, n.SW, n.W, n.NW}
|
||||
}
|
||||
|
||||
10
day-04/internal/one/point.go
Normal file
10
day-04/internal/one/point.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package one
|
||||
|
||||
type point struct {
|
||||
x, y int
|
||||
direction direction
|
||||
}
|
||||
|
||||
func newPoint(x, y int) point {
|
||||
return point{x: x, y: y}
|
||||
}
|
||||
@@ -3,30 +3,28 @@ package one
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/onyx-and-iris/aoc2024/day-04/internal/util"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func Solve(buf []byte) (int, error) {
|
||||
r := bytes.NewReader(buf)
|
||||
lines, err := util.ReadLines(r)
|
||||
graph, err := readLines(r)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var sum int
|
||||
|
||||
for i := 0; i < len(lines); i++ {
|
||||
for j := 0; j < len(lines[i]); j++ {
|
||||
neighbours := newNeighbours(j, i)
|
||||
for _, n := range neighbours.all() {
|
||||
if n.x < 0 || n.y < 0 || n.y >= len(lines) || n.x >= len(lines[i]) {
|
||||
continue
|
||||
}
|
||||
for i := 0; i < len(graph.data); i++ {
|
||||
for j := 0; j < len(graph.data[i]); j++ {
|
||||
current := newPoint(j, i)
|
||||
if graph.valueAt(current) == 'X' {
|
||||
for _, n := range neighbours(current) {
|
||||
if graph.isOutOfBounds(n) {
|
||||
continue
|
||||
}
|
||||
|
||||
if lines[i][j] == 'X' {
|
||||
if checkNeighbours(n, "MAS", lines) {
|
||||
if checkNeighbours(graph, n, "MAS") {
|
||||
sum++
|
||||
}
|
||||
}
|
||||
@@ -37,19 +35,19 @@ func Solve(buf []byte) (int, error) {
|
||||
return sum, nil
|
||||
}
|
||||
|
||||
func checkNeighbours(n neighbour, word string, lines []string) bool {
|
||||
func checkNeighbours(graph *graph, n point, word string) bool {
|
||||
if len(word) == 0 {
|
||||
log.Debug("we found a full XMAS")
|
||||
return true
|
||||
}
|
||||
|
||||
if n.x < 0 || n.y < 0 || n.y >= len(lines) || n.x >= len(lines[n.y]) {
|
||||
if graph.isOutOfBounds(n) {
|
||||
return false
|
||||
}
|
||||
|
||||
if lines[n.y][n.x] != word[0] {
|
||||
if graph.valueAt(n) != rune(word[0]) {
|
||||
return false
|
||||
}
|
||||
|
||||
return checkNeighbours(newNeighbour(n.direction, n.x, n.y), word[1:], lines)
|
||||
return checkNeighbours(graph, neighbours(n)[n.direction], word[1:])
|
||||
}
|
||||
|
||||
21
day-04/internal/one/util.go
Normal file
21
day-04/internal/one/util.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package one
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
)
|
||||
|
||||
func readLines(r io.Reader) (*graph, error) {
|
||||
graph := newGraph()
|
||||
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
graph.data = append(graph.data, scanner.Text())
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return graph, nil
|
||||
}
|
||||
Reference in New Issue
Block a user