From ca45fb7c17d17be53be097ab21b8a5449b66ac6a Mon Sep 17 00:00:00 2001 From: Dylan Butler Date: Wed, 4 Dec 2024 18:08:37 +1100 Subject: [PATCH] 2024 day 4 update --- 2024/02/02.go | 11 +++++------ 2024/04/01.go | 4 +--- pkg/grid/grid.go | 1 + pkg/lists/queue.go | 1 + pkg/ll/ll.go | 1 + pkg/space/space.go | 4 ++++ 6 files changed, 13 insertions(+), 9 deletions(-) diff --git a/2024/02/02.go b/2024/02/02.go index 5f9410c..c5392ac 100644 --- a/2024/02/02.go +++ b/2024/02/02.go @@ -24,15 +24,14 @@ func solve(input [][]int) int { } func isSafe(ints []int) bool { - increasing := ints[1] > ints[0] + shouldIncrease := ints[1] > ints[0] for i := 1; i < len(ints); i++ { - if ints[i] == ints[i-1] { + distance := math.Abs(ints[i] - ints[i-1]) + if distance < 1 || distance > 3 { return false } - if increasing != (ints[i] > ints[i-1]) { - return false - } - if math.Abs(ints[i]-ints[i-1]) > 3 { + increases := ints[i] > ints[i-1] + if shouldIncrease != increases { return false } } diff --git a/2024/04/01.go b/2024/04/01.go index ab3112e..716cdd0 100644 --- a/2024/04/01.go +++ b/2024/04/01.go @@ -14,15 +14,13 @@ func solve(input space.Grid[byte]) int { count := 0 for cell := range input.Cells() { for _, dir := range slices.Concat(space.Directions, space.Diagonals) { - cell := cell found := true for i := range word { - nextCell := input.Get(cell) + nextCell := input.Get(cell.Move(dir.Multiply(i))) if nextCell == nil || *nextCell != word[i] { found = false break } - cell = cell.Move(dir) } if found { count++ diff --git a/pkg/grid/grid.go b/pkg/grid/grid.go index 9e787b9..3efac26 100644 --- a/pkg/grid/grid.go +++ b/pkg/grid/grid.go @@ -4,6 +4,7 @@ import ( "slices" ) +// Deprecated. Use space.Grid instead. type Grid[T any] map[[2]int]*T func (g Grid[T]) Set(x, y int, cell T) { diff --git a/pkg/lists/queue.go b/pkg/lists/queue.go index 7d30827..5711bc2 100644 --- a/pkg/lists/queue.go +++ b/pkg/lists/queue.go @@ -28,6 +28,7 @@ func (q *Queue[T]) Seq(yield func(T) bool) { } } +// TODO: fix the failing cases when using ll //type Queue[T any] struct { // linked Linked[T] //} diff --git a/pkg/ll/ll.go b/pkg/ll/ll.go index d11b8e9..57f4975 100644 --- a/pkg/ll/ll.go +++ b/pkg/ll/ll.go @@ -1,5 +1,6 @@ package ll +// Deprecated. Use lists.DoublyLinked instead. type Double[T any] struct { Val T Prev *Double[T] diff --git a/pkg/space/space.go b/pkg/space/space.go index 8afbd09..f24f689 100644 --- a/pkg/space/space.go +++ b/pkg/space/space.go @@ -38,6 +38,10 @@ func (d *Direction) Add(b Direction) Direction { return Direction{d[0] + b[0], d[1] + b[1]} } +func (d *Direction) Multiply(n int) Direction { + return Direction{d[0] * n, d[1] * n} +} + var ( North = Direction{0, -1} South = Direction{0, 1}