much better day13 after looking at others
This commit is contained in:
59
2023/go/day13b/day13.go
Normal file
59
2023/go/day13b/day13.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package day13
|
||||
|
||||
import (
|
||||
_ "adventofcode2023/utils"
|
||||
"adventofcode2023/utils/inputs"
|
||||
_ "fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Part1(input string) int {
|
||||
return symmetry(input, 0)
|
||||
}
|
||||
|
||||
func Part2(input string) int {
|
||||
return symmetry(input, 1)
|
||||
}
|
||||
|
||||
func symmetry(input string, smudges int) int {
|
||||
ans := 0
|
||||
|
||||
patterns := strings.Split(input, "\n\n")
|
||||
for _, pattern := range patterns {
|
||||
grid := inputs.ToGrid2D[rune](pattern, "\n", "", ' ', func(c string) rune { return rune(c[0])})
|
||||
G := grid.Matrix()
|
||||
R := len(G)
|
||||
C := len(G[0])
|
||||
// vertical symmetry
|
||||
for c:=0;c<C-1;c++ {
|
||||
badness := 0
|
||||
for dc:=0;dc<C;dc++ {
|
||||
left := c-dc
|
||||
right := c+1+dc
|
||||
if left < 0 || left > right || right > C -1 { break }
|
||||
for r:=0;r<R;r++ {
|
||||
if G[r][left] != G[r][right] {
|
||||
badness += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
if badness == smudges { ans += c+1 }
|
||||
}
|
||||
// horizontal symmetry
|
||||
for r:=0;r<R-1;r++ {
|
||||
badness := 0
|
||||
for dr:=0;dr<R;dr++ {
|
||||
up := r-dr
|
||||
down := r+1+dr
|
||||
if up < 0 || up > down || down > R -1 { break }
|
||||
for c:=0;c<C;c++ {
|
||||
if G[up][c] != G[down][c] {
|
||||
badness += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
if badness == smudges { ans += 100*(r+1) }
|
||||
}
|
||||
}
|
||||
return ans
|
||||
}
|
||||
47
2023/go/day13b/day13_test.go
Normal file
47
2023/go/day13b/day13_test.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package day13
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPart1(t *testing.T) {
|
||||
r := Part1(
|
||||
`#.##..##.
|
||||
..#.##.#.
|
||||
##......#
|
||||
##......#
|
||||
..#.##.#.
|
||||
..##..##.
|
||||
#.#.##.#.
|
||||
|
||||
#...##..#
|
||||
#....#..#
|
||||
..##..###
|
||||
#####.##.
|
||||
#####.##.
|
||||
..##..###
|
||||
#....#..#`)
|
||||
require.Equal(t, 405, r)
|
||||
}
|
||||
|
||||
func TestPart2(t *testing.T) {
|
||||
r := Part2(
|
||||
`#.##..##.
|
||||
..#.##.#.
|
||||
##......#
|
||||
##......#
|
||||
..#.##.#.
|
||||
..##..##.
|
||||
#.#.##.#.
|
||||
|
||||
#...##..#
|
||||
#....#..#
|
||||
..##..###
|
||||
#####.##.
|
||||
#####.##.
|
||||
..##..###
|
||||
#....#..#`)
|
||||
require.Equal(t, 400, r)
|
||||
}
|
||||
1377
2023/go/day13b/input.txt
Normal file
1377
2023/go/day13b/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
@@ -39,6 +39,9 @@ func (g *Grid[T]) SizeX() int {
|
||||
func (g *Grid[T]) SizeY() int {
|
||||
return g.sizeY
|
||||
}
|
||||
func (g *Grid[T]) Matrix() [][]T {
|
||||
return g.matrix
|
||||
}
|
||||
|
||||
func (g *Grid[T]) Get(x, y int) T {
|
||||
if x < 0 || x >= g.sizeX {
|
||||
|
||||
Reference in New Issue
Block a user