2025 Day 7 Complete!
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"slices"
|
||||
)
|
||||
|
||||
type CoordByteMap struct {
|
||||
@@ -93,14 +94,14 @@ func (m *CoordByteMap) GetCol(x int) []byte {
|
||||
|
||||
func (m *CoordByteMap) AddRow(row []byte) {
|
||||
y := m.BRY + 1
|
||||
for x := 0; x < len(row); x++ {
|
||||
for x := range row {
|
||||
m.Put(Coordinate{X: x + m.TLX, Y: y}, row[x])
|
||||
}
|
||||
}
|
||||
|
||||
func (m *CoordByteMap) AddCol(col []byte) {
|
||||
x := m.BRX + 1
|
||||
for y := 0; y < len(col); y++ {
|
||||
for y := range col {
|
||||
m.Put(Coordinate{X: x, Y: y + m.TLY}, col[y])
|
||||
}
|
||||
}
|
||||
@@ -308,20 +309,29 @@ func (m *CoordByteMap) FindAll(b ...byte) []Coordinate {
|
||||
return ret
|
||||
}
|
||||
|
||||
func (m *CoordByteMap) FindAllOnRow(row int, b ...byte) []Coordinate {
|
||||
var ret []Coordinate
|
||||
if row < m.TLY || row > m.BRX {
|
||||
return ret
|
||||
}
|
||||
for x := m.TLX; x <= m.BRX; x++ {
|
||||
c := Coordinate{X: x, Y: row}
|
||||
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 {
|
||||
if !slices.Contains(b, v) {
|
||||
ret[c] = v
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,10 @@ package aoc
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -65,7 +65,7 @@ func Gcd(x, y int) int {
|
||||
// Lcm finds the least common multiple, using gcd
|
||||
func Lcm(a, b int, integers ...int) int {
|
||||
result := a * b / Gcd(a, b)
|
||||
for i := 0; i < len(integers); i++ {
|
||||
for i := range integers {
|
||||
result = Lcm(result, integers[i])
|
||||
}
|
||||
return result
|
||||
@@ -92,8 +92,8 @@ func ArgIsSet(a string) bool {
|
||||
// GetArgValue returns the argument with the asked for key or ""
|
||||
func GetArgValue(a string) string {
|
||||
for i := range os.Args {
|
||||
if strings.HasPrefix(os.Args[i], a+"=") {
|
||||
return strings.TrimPrefix(os.Args[i], a+"=")
|
||||
if ret, ok := strings.CutPrefix(os.Args[i], a+"="); ok {
|
||||
return ret
|
||||
}
|
||||
}
|
||||
return ""
|
||||
@@ -206,8 +206,7 @@ func FileToString(fn string) string {
|
||||
func FileToBytes(fn string) []byte {
|
||||
var c []byte
|
||||
var err error
|
||||
c, err = ioutil.ReadFile(fn)
|
||||
if err != nil {
|
||||
if c, err = os.ReadFile(fn); err != nil {
|
||||
fmt.Println("Unable to read file: " + fn)
|
||||
os.Exit(1)
|
||||
}
|
||||
@@ -322,24 +321,16 @@ func StringSliceIndex(h []string, n string) int {
|
||||
|
||||
// StringSliceContains takes a string slice and a string and return true
|
||||
// if the string is in the slice
|
||||
// Created before we had a slices.Contains. Still here for old solutions
|
||||
func StringSliceContains(h []string, n string) bool {
|
||||
for _, v := range h {
|
||||
if v == n {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
return slices.Contains(h, n)
|
||||
}
|
||||
|
||||
// IntSliceContains takes an int slice and an int and return true
|
||||
// if the int is in the slice
|
||||
// Created before we had a slices.Contains. Still here for old solutions
|
||||
func IntSliceContains(h []int, n int) bool {
|
||||
for _, v := range h {
|
||||
if v == n {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
return slices.Contains(h, n)
|
||||
}
|
||||
|
||||
// AppendString returns a slice of strings that are base+group[...]
|
||||
@@ -375,7 +366,7 @@ func IntersectStringSlice(list1, list2 []string) []string {
|
||||
// Unique removes all duplicates
|
||||
func UniqueStringSlice(list []string) []string {
|
||||
var ret []string
|
||||
for k := 0; k < len(list); k++ {
|
||||
for k := range list {
|
||||
var dupe bool
|
||||
for j := k; j < len(list); j++ {
|
||||
if list[k] == list[j] {
|
||||
|
||||
Reference in New Issue
Block a user