first commit

This commit is contained in:
onyx-and-iris 2024-12-01 17:50:02 +00:00
commit 25b6db75c8
8 changed files with 272 additions and 0 deletions

29
.gitignore vendored Normal file
View File

@ -0,0 +1,29 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
# Go workspace file
go.work
go.work.sum
# env file
.env
# input files
partial.txt
input.txt

3
day-01/go.mod Normal file
View File

@ -0,0 +1,3 @@
module github.com/onyx-and-iris/aoc2024/day-01
go 1.23.3

View File

@ -0,0 +1,50 @@
package one
import (
"bufio"
"bytes"
"embed"
"io"
"math"
"regexp"
"github.com/onyx-and-iris/aoc2024/day-01/internal/util"
)
//go:embed testdata
var files embed.FS
var reNums = regexp.MustCompile(`[0-9]+`)
func Solve() (int, error) {
data, _ := files.ReadFile("testdata/input.txt")
r := bytes.NewReader(data)
left, right, err := parseLines(r)
if err != nil {
return 0, err
}
var sum int
for i, n := range left {
sum += int(math.Abs(float64(n - right[i])))
}
return sum, nil
}
func parseLines(r io.Reader) ([]int, []int, error) {
var left, right []int
scanner := bufio.NewScanner(r)
for scanner.Scan() {
nums := reNums.FindAllString(scanner.Text(), -1)
left = util.InsertSorted(left, util.MustConv(nums[0]))
right = util.InsertSorted(right, util.MustConv(nums[1]))
}
if err := scanner.Err(); err != nil {
return nil, nil, err
}
return left, right, nil
}

View File

@ -0,0 +1,63 @@
package two
import (
"bufio"
"bytes"
"embed"
"io"
"regexp"
"slices"
"github.com/onyx-and-iris/aoc2024/day-01/internal/util"
)
//go:embed testdata
var files embed.FS
var reNums = regexp.MustCompile(`[0-9]+`)
func Solve() (int, error) {
data, _ := files.ReadFile("testdata/input.txt")
r := bytes.NewReader(data)
left, right, err := parseLines(r)
if err != nil {
return 0, err
}
var similarity int
for k, v := range left {
indx := slices.Index(right, k)
if indx == -1 {
continue
}
var occurrences int
for _, n := range right[indx:] {
if n == k {
occurrences++
}
}
similarity += k * v * occurrences
}
return similarity, nil
}
func parseLines(r io.Reader) (map[int]int, []int, error) {
var left map[int]int = make(map[int]int)
var right []int
scanner := bufio.NewScanner(r)
for scanner.Scan() {
nums := reNums.FindAllString(scanner.Text(), -1)
left[util.MustConv(nums[0])] = left[util.MustConv(nums[0])] + 1
right = util.InsertSorted(right, util.MustConv(nums[1]))
}
if err := scanner.Err(); err != nil {
return nil, nil, err
}
return left, right, nil
}

View File

@ -0,0 +1,20 @@
package util
import (
"cmp"
"slices"
"strconv"
)
func MustConv(s string) int {
n, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return n
}
func InsertSorted[T cmp.Ordered](ts []T, t T) []T {
i, _ := slices.BinarySearch(ts, t)
return slices.Insert(ts, i, t)
}

27
day-01/main.go Normal file
View File

@ -0,0 +1,27 @@
/********************************************************************************
Advent of Code 2024 - day-01
********************************************************************************/
package main
import (
"fmt"
"log"
"github.com/onyx-and-iris/aoc2024/day-01/internal/one"
"github.com/onyx-and-iris/aoc2024/day-01/internal/two"
)
func main() {
val, err := one.Solve()
if err != nil {
log.Fatal(err)
}
fmt.Printf("solution one: %d\n", val)
val, err = two.Solve()
if err != nil {
log.Fatal(err)
}
fmt.Printf("solution two: %d\n", val)
}

25
day-01/makefile Normal file
View File

@ -0,0 +1,25 @@
program = day-01
GO = go
SRC_DIR := src
BIN_DIR := bin
EXE := $(BIN_DIR)/$(program)
.DEFAULT_GOAL := build
.PHONY: fmt vet build clean
fmt:
$(GO) fmt ./...
vet: fmt
$(GO) vet ./...
build: vet | $(BIN_DIR)
$(GO) build -o $(EXE) .
$(BIN_DIR):
@mkdir -p $@
clean:
@rm -rv $(BIN_DIR)

55
scaffold.sh Executable file
View File

@ -0,0 +1,55 @@
#!/usr/bin/env bash
name=day-"$1"
if [ -d day-"$name" ]; then
echo "program $name already exists"
exit 1
fi
mkdir -p "$name"/testdata
touch "$name"/makefile
cat <<EOT >> "$name"/makefile
program = $name
GO = go
SRC_DIR := src
BIN_DIR := bin
EXE := \$(BIN_DIR)/\$(program)
.DEFAULT_GOAL := build
.PHONY: fmt vet build clean
fmt:
\$(GO) fmt ./...
vet: fmt
\$(GO) vet ./...
build: vet | \$(BIN_DIR)
\$(GO) build -o \$(EXE) ./\$(SRC_DIR)
\$(BIN_DIR):
@mkdir -p \$@
clean:
@rm -rv \$(BIN_DIR)
EOT
cat <<EOT >> "$name"/solution.go
/********************************************************************************
Advent of Code 2024 - $name
********************************************************************************/
package main
func main() {
}
EOT
cd "$name" || exit
go mod init github.com/onyx-and-iris/aoc2024/"$name"