mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-04-18 10:43:47 +01:00
Compare commits
No commits in common. "4013cd6c7fea0d22c713e48c080925e8deb67b4d" and "dda597a577268bda46e88c7209c74c59eee7b7e3" have entirely different histories.
4013cd6c7f
...
dda597a577
@ -1,9 +1,7 @@
|
|||||||
package one
|
package one
|
||||||
|
|
||||||
type direction int
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
N direction = iota
|
N = iota
|
||||||
NE
|
NE
|
||||||
E
|
E
|
||||||
SE
|
SE
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
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,14 +1,58 @@
|
|||||||
package one
|
package one
|
||||||
|
|
||||||
func neighbours(p point) [8]point {
|
type neighbour struct {
|
||||||
return [8]point{
|
x int
|
||||||
{p.x, p.y - 1, N},
|
y int
|
||||||
{p.x + 1, p.y - 1, NE},
|
direction int
|
||||||
{p.x + 1, p.y, E},
|
}
|
||||||
{p.x + 1, p.y + 1, SE},
|
|
||||||
{p.x, p.y + 1, S},
|
func newNeighbour(direction, x, y int) neighbour {
|
||||||
{p.x - 1, p.y + 1, SW},
|
switch direction {
|
||||||
{p.x - 1, p.y, W},
|
case N:
|
||||||
{p.x - 1, p.y - 1, NW},
|
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{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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}
|
||||||
|
}
|
||||||
|
@ -1,10 +0,0 @@
|
|||||||
package one
|
|
||||||
|
|
||||||
type point struct {
|
|
||||||
x, y int
|
|
||||||
direction direction
|
|
||||||
}
|
|
||||||
|
|
||||||
func newPoint(x, y int) point {
|
|
||||||
return point{x: x, y: y}
|
|
||||||
}
|
|
@ -3,28 +3,30 @@ package one
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|
||||||
|
"github.com/onyx-and-iris/aoc2024/day-04/internal/util"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Solve(buf []byte) (int, error) {
|
func Solve(buf []byte) (int, error) {
|
||||||
r := bytes.NewReader(buf)
|
r := bytes.NewReader(buf)
|
||||||
graph, err := readLines(r)
|
lines, err := util.ReadLines(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var sum int
|
var sum int
|
||||||
|
|
||||||
for i := 0; i < len(graph.data); i++ {
|
for i := 0; i < len(lines); i++ {
|
||||||
for j := 0; j < len(graph.data[i]); j++ {
|
for j := 0; j < len(lines[i]); j++ {
|
||||||
current := newPoint(j, i)
|
neighbours := newNeighbours(j, i)
|
||||||
if graph.valueAt(current) == 'X' {
|
for _, n := range neighbours.all() {
|
||||||
for _, n := range neighbours(current) {
|
if n.x < 0 || n.y < 0 || n.y >= len(lines) || n.x >= len(lines[i]) {
|
||||||
if graph.isOutOfBounds(n) {
|
continue
|
||||||
continue
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if checkNeighbours(graph, n, "MAS") {
|
if lines[i][j] == 'X' {
|
||||||
|
if checkNeighbours(n, "MAS", lines) {
|
||||||
sum++
|
sum++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -35,19 +37,19 @@ func Solve(buf []byte) (int, error) {
|
|||||||
return sum, nil
|
return sum, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkNeighbours(graph *graph, n point, word string) bool {
|
func checkNeighbours(n neighbour, word string, lines []string) bool {
|
||||||
if len(word) == 0 {
|
if len(word) == 0 {
|
||||||
log.Debug("we found a full XMAS")
|
log.Debug("we found a full XMAS")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if graph.isOutOfBounds(n) {
|
if n.x < 0 || n.y < 0 || n.y >= len(lines) || n.x >= len(lines[n.y]) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if graph.valueAt(n) != rune(word[0]) {
|
if lines[n.y][n.x] != word[0] {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return checkNeighbours(graph, neighbours(n)[n.direction], word[1:])
|
return checkNeighbours(newNeighbour(n.direction, n.x, n.y), word[1:], lines)
|
||||||
}
|
}
|
||||||
|
@ -1,21 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
@ -1,9 +1,7 @@
|
|||||||
package two
|
package two
|
||||||
|
|
||||||
type direction int
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
NW direction = iota
|
NW = iota
|
||||||
NE
|
NE
|
||||||
SE
|
SE
|
||||||
SW
|
SW
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
package two
|
|
||||||
|
|
||||||
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,10 +1,50 @@
|
|||||||
package two
|
package two
|
||||||
|
|
||||||
func neighbours(p point) [4]point {
|
type neighbour struct {
|
||||||
return [4]point{
|
x int
|
||||||
{p.x - 1, p.y - 1, NW},
|
y int
|
||||||
{p.x + 1, p.y - 1, NE},
|
direction int
|
||||||
{p.x + 1, p.y + 1, SE},
|
}
|
||||||
{p.x - 1, p.y + 1, SW},
|
|
||||||
|
func newNeighbour(direction, x, y int) neighbour {
|
||||||
|
switch direction {
|
||||||
|
case NW:
|
||||||
|
return neighbour{x - 1, y + 1, direction}
|
||||||
|
case NE:
|
||||||
|
return neighbour{x + 1, y + 1, direction}
|
||||||
|
case SE:
|
||||||
|
return neighbour{x + 1, y - 1, direction}
|
||||||
|
case SW:
|
||||||
|
return neighbour{x - 1, y - 1, direction}
|
||||||
|
default:
|
||||||
|
return neighbour{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n neighbour) outOfBounds(lines []string) bool {
|
||||||
|
return n.x < 0 || n.y < 0 || n.y >= len(lines) || n.x >= len(lines[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n neighbour) value(lines []string) rune {
|
||||||
|
return rune(lines[n.y][n.x])
|
||||||
|
}
|
||||||
|
|
||||||
|
type neighbours struct {
|
||||||
|
NW neighbour
|
||||||
|
NE neighbour
|
||||||
|
SE neighbour
|
||||||
|
SW neighbour
|
||||||
|
}
|
||||||
|
|
||||||
|
func newNeighbours(x, y int) neighbours {
|
||||||
|
return neighbours{
|
||||||
|
newNeighbour(NW, x, y),
|
||||||
|
newNeighbour(NE, x, y),
|
||||||
|
newNeighbour(SE, x, y),
|
||||||
|
newNeighbour(SW, x, y),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n neighbours) all() [4]neighbour {
|
||||||
|
return [4]neighbour{n.NW, n.NE, n.SE, n.SW}
|
||||||
|
}
|
||||||
|
@ -1,10 +0,0 @@
|
|||||||
package two
|
|
||||||
|
|
||||||
type point struct {
|
|
||||||
x, y int
|
|
||||||
direction direction
|
|
||||||
}
|
|
||||||
|
|
||||||
func newPoint(x, y int) point {
|
|
||||||
return point{x: x, y: y}
|
|
||||||
}
|
|
@ -3,28 +3,29 @@ package two
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
|
"github.com/onyx-and-iris/aoc2024/day-04/internal/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Solve(buf []byte) (int, error) {
|
func Solve(buf []byte) (int, error) {
|
||||||
r := bytes.NewReader(buf)
|
r := bytes.NewReader(buf)
|
||||||
graph, err := readLines(r)
|
lines, err := util.ReadLines(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var sum int
|
var sum int
|
||||||
|
|
||||||
for i := 0; i < len(graph.data); i++ {
|
for i := 0; i < len(lines); i++ {
|
||||||
for j := 0; j < len(graph.data[i]); j++ {
|
for j := 0; j < len(lines[i]); j++ {
|
||||||
current := newPoint(j, i)
|
if lines[i][j] == 'A' {
|
||||||
if graph.valueAt(current) == 'A' {
|
neighbours := newNeighbours(j, i)
|
||||||
if func() bool {
|
if func() bool {
|
||||||
for _, n := range neighbours(current) {
|
for _, n := range neighbours.all() {
|
||||||
if graph.isOutOfBounds(n) {
|
if n.outOfBounds(lines) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
if !slices.Contains([]rune{'M', 'S'}, n.value(lines)) {
|
||||||
if !slices.Contains([]rune{'M', 'S'}, graph.valueAt(n)) {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -33,13 +34,12 @@ func Solve(buf []byte) (int, error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
ns := neighbours(current)
|
|
||||||
matrix := newMatrix(
|
matrix := newMatrix(
|
||||||
graph.valueAt(current),
|
rune(lines[i][j]),
|
||||||
graph.valueAt(ns[NW]),
|
neighbours.NW.value(lines),
|
||||||
graph.valueAt(ns[NE]),
|
neighbours.NE.value(lines),
|
||||||
graph.valueAt(ns[SE]),
|
neighbours.SE.value(lines),
|
||||||
graph.valueAt(ns[SW]),
|
neighbours.SW.value(lines),
|
||||||
)
|
)
|
||||||
|
|
||||||
if matrix.validate() {
|
if matrix.validate() {
|
||||||
|
@ -1,21 +0,0 @@
|
|||||||
package two
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
21
day-04/internal/util/util.go
Normal file
21
day-04/internal/util/util.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ReadLines(r io.Reader) ([]string, error) {
|
||||||
|
lines := []string{}
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(r)
|
||||||
|
for scanner.Scan() {
|
||||||
|
lines = append(lines, scanner.Text())
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return []string{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines, nil
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user