add day-25 part1

This commit is contained in:
onyx-and-iris 2024-12-26 19:00:37 +00:00
parent 942d2a8456
commit dda597a577
11 changed files with 264 additions and 0 deletions

1
day-25/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
internal/two

41
day-25/cmd/cli/main.go Normal file
View File

@ -0,0 +1,41 @@
/********************************************************************************
Advent of Code 2024 - day-25
********************************************************************************/
package main
import (
"embed"
"flag"
"fmt"
"slices"
log "github.com/sirupsen/logrus"
problems "github.com/onyx-and-iris/aoc2024/day-25"
)
//go:embed testdata
var files embed.FS
func main() {
filename := flag.String("f", "input.txt", "input file")
loglevel := flag.Int("l", int(log.InfoLevel), "log level")
flag.Parse()
if slices.Contains(log.AllLevels, log.Level(*loglevel)) {
log.SetLevel(log.Level(*loglevel))
}
data, err := files.ReadFile(fmt.Sprintf("testdata/%s", *filename))
if err != nil {
log.Fatal(err)
}
one, two, err := problems.Solve(data)
if err != nil {
log.Fatal(err)
}
fmt.Printf("solution one: %d\nsolution two: %d\n", one, two)
}

7
day-25/go.mod Normal file
View File

@ -0,0 +1,7 @@
module github.com/onyx-and-iris/aoc2024/day-25
go 1.23.3
require github.com/sirupsen/logrus v1.9.3
require golang.org/x/sys v0.12.0 // indirect

16
day-25/go.sum Normal file
View File

@ -0,0 +1,16 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -0,0 +1,55 @@
package one
import (
"strings"
)
type kindOfSchematic int
const (
Lock kindOfSchematic = iota
Key
)
type schematic struct {
kind kindOfSchematic
heights []int
data []string
}
func newSchematic(buf [][]byte) *schematic {
var kind kindOfSchematic
data := make([]string, len(buf))
heights := make([]int, len(buf[0]))
for i, line := range buf {
data[i] = string(line)
if i == 0 {
if allInString(data[i], '#') {
kind = Lock
} else if allInString(data[i], '.') {
kind = Key
}
}
if kind == Lock && i == 0 {
continue
}
if kind == Key && i == len(buf)-1 {
continue
}
for j, r := range data[i] {
if r == '#' {
heights[j]++
}
}
}
return &schematic{kind, heights, data}
}
func (s *schematic) String() string {
return strings.Join(s.data, "\n")
}

View File

@ -0,0 +1,34 @@
package one
import (
"bytes"
log "github.com/sirupsen/logrus"
)
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
maxHeight, schematics, err := parseLines(r)
if err != nil {
return 0, err
}
var count int
for _, lock := range schematics[Lock] {
for _, key := range schematics[Key] {
if func() bool {
for i := range key.heights {
if key.heights[i]+lock.heights[i] > maxHeight {
return false
}
}
return true
}() {
count++
log.Debugf("lock: %v and key: %v", lock.heights, key.heights)
}
}
}
return count, nil
}

View File

@ -0,0 +1,15 @@
package one
import (
_ "embed"
"os"
"testing"
)
//go:embed testdata/input.txt
var data []byte
func BenchmarkSolve(b *testing.B) {
os.Stdout, _ = os.Open(os.DevNull)
Solve(data)
}

View File

@ -0,0 +1,30 @@
package one
import (
"bytes"
"io"
)
func parseLines(r io.Reader) (int, map[kindOfSchematic][]*schematic, error) {
raw, _ := io.ReadAll(r)
rawSchematics := bytes.Split(raw, []byte("\n\n"))
schematics := map[kindOfSchematic][]*schematic{Lock: {}, Key: {}}
for _, rawSchematic := range rawSchematics {
s := newSchematic(bytes.Split(rawSchematic, []byte("\n")))
schematics[s.kind] = append(schematics[s.kind], s)
}
maxHeight := len(schematics[Lock][0].heights)
return maxHeight, schematics, nil
}
func allInString(s string, r rune) bool {
for _, c := range s {
if c != r {
return false
}
}
return true
}

30
day-25/makefile Normal file
View File

@ -0,0 +1,30 @@
program = day-25
GO = go
SRC_DIR := src
BIN_DIR := bin
EXE := $(BIN_DIR)/$(program)
.DEFAULT_GOAL := build
.PHONY: fmt vet build bench clean
fmt:
$(GO) fmt ./...
vet: fmt
$(GO) vet ./...
build: vet | $(BIN_DIR)
$(GO) build -o $(EXE) ./$(SRC_DIR)
bench:
$(GO) test ./internal/one/ -bench=. > internal/one/benchmark
$(GO) test ./internal/two/ -bench=. > internal/two/benchmark
$(GO) test . -count=10 -bench=. > benchmark
$(BIN_DIR):
@mkdir -p $@
clean:
@rm -rv $(BIN_DIR)

20
day-25/solve.go Normal file
View File

@ -0,0 +1,20 @@
package daytwentyfive
import (
"github.com/onyx-and-iris/aoc2024/day-25/internal/one"
"github.com/onyx-and-iris/aoc2024/day-25/internal/two"
)
func Solve(buf []byte) (int, int, error) {
answerOne, err := one.Solve(buf)
if err != nil {
return 0, 0, err
}
answerTwo, err := two.Solve(buf)
if err != nil {
return 0, 0, err
}
return answerOne, answerTwo, nil
}

View File

@ -0,0 +1,15 @@
package daytwentyfive
import (
_ "embed"
"os"
"testing"
)
//go:embed testdata/input.txt
var data []byte
func BenchmarkSolve(b *testing.B) {
os.Stdout, _ = os.Open(os.DevNull)
Solve(data)
}