2021 Day 23 is complete, but not by code...

This commit is contained in:
2021-12-23 09:56:27 -06:00
parent 17bce823c2
commit a2e868bba2
5 changed files with 163 additions and 1 deletions

View File

@@ -1,6 +1,9 @@
package aoc
import "fmt"
import (
"errors"
"fmt"
)
type CoordByteMap struct {
Field map[Coordinate]byte
@@ -32,6 +35,56 @@ func StringSliceToCoordByteMap(input []string) CoordByteMap {
return ret
}
// FindFirst searches left to right, top to bottom for the first
// instance of b.
func (m *CoordByteMap) FindFirst(b byte) (Coordinate, error) {
for y := m.TLY; y <= m.BRY; y++ {
for x := m.TLX; x <= m.BRX; x++ {
c := Coordinate{X: x, Y: y}
if m.Get(c) == b {
return c, nil
}
}
}
return Coordinate{}, errors.New("Not Found")
}
func (m *CoordByteMap) FindAll(b ...byte) []Coordinate {
var ret []Coordinate
for y := m.TLY; y <= m.BRY; y++ {
for x := m.TLX; x <= m.BRX; x++ {
c := Coordinate{X: x, Y: y}
for i := range b {
if m.Get(c) == b[i] {
ret = append(ret, c)
}
}
}
}
return ret
}
func (m *CoordByteMap) FindAllNot(b ...byte) map[Coordinate]byte {
ret := make(map[Coordinate]byte)
for y := m.TLY; y <= m.BRY; y++ {
for x := m.TLX; x <= m.BRX; x++ {
c := Coordinate{X: x, Y: y}
v := m.Get(c)
var is bool
for i := range b {
if v == b[i] {
is = true
break
}
}
if !is {
ret[c] = v
}
}
}
return ret
}
func (m *CoordByteMap) Count(b byte) int {
var ret int
for y := m.TLY; y <= m.BRY; y++ {