Compare commits

...

46 Commits

Author SHA1 Message Date
cacd140961 ^^ 2024-01-02 09:20:51 +00:00
0f357df19d Merge branch 'main' of https://github.com/onyx-and-iris/aoc2023 2024-01-02 09:19:19 +00:00
5b225a9166 adjust bound factory method to set open end of interval 2024-01-02 09:18:14 +00:00
9ba5ad695e add mutex lock 2024-01-02 06:58:03 +00:00
cc452c76a4 fix day-5 p2 2024-01-01 11:32:43 +00:00
73298dae8d move area calculation into a function
fix mustConvHex docstring
2023-12-24 10:50:41 +00:00
35776a5470 upd docstrings 2023-12-23 11:58:21 +00:00
2814b12750 rename var move for p2 2023-12-22 22:23:43 +00:00
e541631a35 rename var move in runner()
uncomment debug logs
2023-12-22 22:03:12 +00:00
038dd531d9 day-18 2023-12-22 21:57:00 +00:00
497da642b2 day-17 2023-12-22 21:56:07 +00:00
58f36581f0 upd makefiles 2023-12-18 19:12:56 +00:00
cb3506cfbc upd docstring 2023-12-17 20:49:11 +00:00
171e50ebe4 split runners a little more 2023-12-17 17:51:09 +00:00
3a3af64df9 remove redundant expression 2023-12-17 15:53:24 +00:00
801ed17bf1 speed up p2... 2023-12-17 15:49:04 +00:00
6dae7502db day-16 2023-12-17 15:31:11 +00:00
6dc1631eb3 add hasher tests 2023-12-16 00:30:07 +00:00
e5a75872fe add docstring 2023-12-16 00:23:25 +00:00
7faa19e159 label already r trimmed 2023-12-16 00:21:45 +00:00
e7635b5e4c remove add, remove functions. 2023-12-15 19:51:15 +00:00
20713a9967 remove assignment 2023-12-15 18:06:56 +00:00
927e78aa3a upd log level 2023-12-15 17:21:33 +00:00
8655265908 day-15 2023-12-15 17:19:33 +00:00
e29029c688 upd go.mod 2023-12-15 12:49:30 +00:00
b2819a20dd remove run scripts, add makefiles 2023-12-15 12:32:39 +00:00
6a65b04ee8 remove redundant constant 2023-12-15 01:41:04 +00:00
92ff666ae2 fix index 2023-12-15 01:37:15 +00:00
219bb3884b reword docstring. 2023-12-15 01:16:30 +00:00
621a0dbd28 fix docstring 2023-12-15 01:04:22 +00:00
eab63b0b36 copy image from cachedImages 2023-12-15 00:53:14 +00:00
94ffd625c9 typo 2023-12-14 22:49:30 +00:00
b55019149b fix wording 2023-12-14 22:48:29 +00:00
e8f7c2a63d add one docstrings 2023-12-14 22:24:56 +00:00
b98379ca2b day-14 2023-12-14 22:22:41 +00:00
0c26ef875a remove debug print 2023-12-14 12:51:04 +00:00
b878eb9df6 remove unnecessary assignment 2023-12-14 04:24:57 +00:00
3d42da7862 fix test name 2023-12-14 03:47:43 +00:00
0fc0c49387 day-13 2023-12-14 03:44:14 +00:00
8b61fa2011 day-12 2023-12-14 03:44:07 +00:00
0f254a243e add run script 2023-12-12 16:16:42 +00:00
e1829e595a add scaffold.sh to .gitignore 2023-12-12 15:55:24 +00:00
a67a87ad96 upd go.mod 2023-12-11 20:39:38 +00:00
35e74a90b3 add docstring to parseInput 2023-12-11 19:43:43 +00:00
8e70c5565f remove dist assignment 2023-12-11 19:41:32 +00:00
23be028cd3 refactor range checks
remove unnecessary print
2023-12-11 19:34:14 +00:00
98 changed files with 2178 additions and 204 deletions

5
.gitignore vendored
View File

@@ -23,4 +23,7 @@ go.work.sum
# input files
test*.txt
input.txt
input.txt
# scaffold
scaffold.sh

View File

@@ -1,6 +1,6 @@
module github.com/onyx-and-iris/aoc2023/day-1
go 1.20
go 1.21.5
require github.com/stretchr/testify v1.8.4

10
day-1/makefile Normal file
View File

@@ -0,0 +1,10 @@
TEST="test.txt"
INPUT="input.txt"
test:
go run . < $(TEST)
run:
go run . < $(INPUT)
all: test

View File

@@ -1,5 +0,0 @@
#!/usr/bin/env bash
INPUT="input.txt"
cat $INPUT | go run .

10
day-10/makefile Normal file
View File

@@ -0,0 +1,10 @@
TEST="test.txt"
INPUT="input.txt"
test:
go run . < $(TEST)
run:
go run . < $(INPUT)
all: test

View File

@@ -1,5 +0,0 @@
#!/usr/bin/env bash
INPUT="input.txt"
cat $INPUT | go run .

View File

@@ -2,7 +2,6 @@ module github.com/onyx-and-iris/aoc2023/day-11
go 1.21.5
require (
github.com/samber/lo v1.39.0 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
)
require github.com/sirupsen/logrus v1.9.3
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect

View File

@@ -1,4 +1,15 @@
github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
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 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/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=

10
day-11/makefile Normal file
View File

@@ -0,0 +1,10 @@
TEST="test.txt"
INPUT="input.txt"
test:
go run . < $(TEST)
run:
go run . < $(INPUT)
all: test

View File

@@ -37,29 +37,15 @@ func shortestRoute(a, b coords, factor int) int {
vert := int(math.Abs(float64(b.Y - a.Y)))
for _, row := range empty["row"] {
if b.Y > a.Y {
if row >= a.Y && row < b.Y {
log.Debug("empty row, adding to vert")
vert += (factor - 1)
}
} else {
if row >= b.Y && row < a.Y {
log.Debug("empty row, adding to vert")
vert += (factor - 1)
}
if row >= a.Y && row < b.Y || row >= b.Y && row < a.Y {
log.Debug("empty row, adding to vert")
vert += (factor - 1)
}
}
for _, col := range empty["col"] {
if b.X > a.X {
if col >= a.X && col < b.X {
log.Debug("empty col, adding to horz")
horz += (factor - 1)
}
} else {
if col >= b.X && col < a.X {
log.Debug("empty col, adding to horz")
horz += (factor - 1)
}
if col >= a.X && col < b.X || col >= b.X && col < a.X {
log.Debug("empty col, adding to horz")
horz += (factor - 1)
}
}
@@ -78,9 +64,8 @@ func one(lines []string) int {
continue
}
dist := shortestRoute(galaxies[i].coords, galaxies[j].coords, 2)
compared = append(compared, []int{galaxies[i].identifier, galaxies[j].identifier})
sum += dist
sum += shortestRoute(galaxies[i].coords, galaxies[j].coords, 2)
}
}

View File

@@ -10,9 +10,8 @@ func two(lines []string) int {
continue
}
dist := shortestRoute(galaxies[i].coords, galaxies[j].coords, 1000000)
compared = append(compared, []int{galaxies[i].identifier, galaxies[j].identifier})
sum += dist
sum += shortestRoute(galaxies[i].coords, galaxies[j].coords, 1000000)
}
}

View File

@@ -2,7 +2,6 @@ package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
@@ -25,6 +24,8 @@ func readlines() []string {
return lines
}
// parseInput stores coordinates of galaxies
// stores empty row and column indexes
func parseInput(lines []string) {
x := 0
runes = make([][]rune, len(lines))
@@ -32,7 +33,6 @@ func parseInput(lines []string) {
for j, r := range line {
runes[i] = append(runes[i], r)
if r == '#' {
fmt.Println(x, j, i)
galaxies = append(galaxies, newGalaxy(x, j, i))
x++
}

10
day-12/go.mod Normal file
View File

@@ -0,0 +1,10 @@
module github.com/onyx-and-iris/aoc2023/day-12
go 1.21.5
require github.com/sirupsen/logrus v1.9.3
require (
github.com/stretchr/testify v1.8.4 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)

17
day-12/go.sum Normal file
View File

@@ -0,0 +1,17 @@
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/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/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/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

10
day-12/makefile Normal file
View File

@@ -0,0 +1,10 @@
TEST="test.txt"
INPUT="input.txt"
test:
go run . < $(TEST)
run:
go run . < $(INPUT)
all: test

113
day-12/one.go Normal file
View File

@@ -0,0 +1,113 @@
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
)
const (
OPERATIONAL = iota
DAMAGED
UNKNOWN
)
var records []record
type record struct {
format []int
springs []spring
}
func newRecord() record {
return record{}
}
// raw returns the state of all springs for this record as int array
func (r record) raw() []int {
vals := make([]int, len(r.springs))
for j, spring := range r.springs {
vals[j] = spring.state
}
return vals
}
type spring struct {
state int
}
func newSpring(state int) spring {
return spring{state: state}
}
// tracker keeps track of results for function calls
type tracker struct {
checked map[string]int
}
func newTracker() tracker {
return tracker{checked: make(map[string]int)}
}
func (t tracker) add(key string, res int) {
t.checked[key] = res
}
func validate(springs, format []int) bool {
return !contains(springs[:format[0]], OPERATIONAL) && (format[0] == len(springs) || springs[format[0]] != DAMAGED)
}
// count returns the number of arrangements possible for a set of springs
func count(springs, format []int) int {
if len(springs) == 0 {
if len(format) == 0 {
return 1
}
return 0
}
if len(format) == 0 {
if contains(springs, DAMAGED) {
return 0
}
return 1
}
identifier := fmt.Sprintf("%v%v", format, springs)
if res, ok := fnTracker.checked[fmt.Sprintf("%v%v", format, springs)]; ok {
log.Debug("returning cached value for ", identifier)
return res
}
result := 0
if springs[0] == OPERATIONAL || springs[0] == UNKNOWN {
result += count(springs[1:], format)
}
if springs[0] == DAMAGED || springs[0] == UNKNOWN {
if format[0] < len(springs) && validate(springs, format) {
result += count(springs[format[0]+1:], format[1:])
} else if format[0] == len(springs) && validate(springs, format) {
result += count(springs[format[0]:], format[1:])
}
}
fnTracker.add(identifier, result)
return result
}
var fnTracker tracker
// one returns the sum of all arrangement counts
func one(lines []string) int {
parselines(lines)
fnTracker = newTracker()
sum := 0
for _, record := range records {
raw := record.raw()
sum += count(raw, record.format)
}
return sum
}

22
day-12/solution.go Normal file
View File

@@ -0,0 +1,22 @@
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
)
func init() {
log.SetLevel(log.InfoLevel)
}
func main() {
lines := readlines()
ans := one(lines)
fmt.Printf("solution one: %d\n", ans)
ans = two(lines)
fmt.Printf("solution two: %d\n", ans)
}

36
day-12/two.go Normal file
View File

@@ -0,0 +1,36 @@
package main
// multiplyBy unfolds all records by a given factor
func multiplyRawBy(factor int, original []int) []int {
multiplied := []int{}
for i := 0; i < factor; i++ {
multiplied = append(multiplied, original...)
if i < factor-1 {
multiplied = append(multiplied, UNKNOWN)
}
}
return multiplied
}
// multiplyBy unfolds all records by a given factor
func multiplyFormatBy(factor int, original []int) []int {
multiplied := []int{}
for i := 0; i < factor; i++ {
multiplied = append(multiplied, original...)
}
return multiplied
}
// two returns the sum of all unfolded arrangement counts
func two(lines []string) int {
fnTracker = newTracker()
sum := 0
for _, record := range records {
raw := record.raw()
sum += count(multiplyRawBy(5, raw), multiplyFormatBy(5, record.format))
}
return sum
}

71
day-12/util.go Normal file
View File

@@ -0,0 +1,71 @@
package main
import (
"bufio"
"log"
"os"
"strconv"
"strings"
"unicode"
)
// readlines reads lines from stdin.
// returns input as an array of strings
func readlines() []string {
lines := []string{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return lines
}
func parselines(lines []string) {
f := func(c rune) bool {
return !unicode.IsDigit(c)
}
records = make([]record, len(lines))
for i, line := range lines {
record := newRecord()
for _, r := range line {
switch r {
case '.':
record.springs = append(record.springs, newSpring(OPERATIONAL))
case '#':
record.springs = append(record.springs, newSpring(DAMAGED))
case '?':
record.springs = append(record.springs, newSpring(UNKNOWN))
}
}
record.format = convertToInts(strings.FieldsFunc(line, f))
records[i] = record
}
}
// convertToInts converts a string representing ints to an array of ints
func convertToInts(data []string) []int {
nums := []int{}
for _, elem := range data {
n, _ := strconv.Atoi(elem)
nums = append(nums, n)
}
return nums
}
// contains returns true if a slice of elements contains a given element
func contains[T comparable](elems []T, v T) bool {
for _, s := range elems {
if v == s {
return true
}
}
return false
}

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

@@ -0,0 +1,3 @@
module github.com/onyx-and-iris/aoc2023/day-13
go 1.21.5

34
day-13/image.go Normal file
View File

@@ -0,0 +1,34 @@
package main
type imgs struct {
img []img
}
func newImages() imgs {
return imgs{img: make([]img, 0)}
}
type img struct {
raw []string
}
func newImg() img {
return img{raw: make([]string, 0)}
}
// transposed rotates an image rightwards ninety degrees
func (i img) transposed() []string {
transposed := []string{}
for x := 0; x < len(i.raw[0]); x++ {
buf := ""
for j := len(i.raw) - 1; j >= 0; j-- {
buf += string(i.raw[j][x])
}
transposed = append(transposed, buf)
}
return transposed
}
var images imgs

10
day-13/makefile Normal file
View File

@@ -0,0 +1,10 @@
TEST="test.txt"
INPUT="input.txt"
test:
go run . < $(TEST)
run:
go run . < $(INPUT)
all: test

60
day-13/one.go Normal file
View File

@@ -0,0 +1,60 @@
package main
import (
"strings"
log "github.com/sirupsen/logrus"
)
// findReflection returns the reflection point for an image
func findReflection(image []string) (int, bool) {
walkToEdge := func(lower, upper int) bool {
for lower >= 0 && upper < len(image) && strings.Compare(image[lower], image[upper]) == 0 {
lower--
upper++
}
lower++
return (lower == 0 || upper == len(image))
}
for i := 0; i < len(image)-1; i++ {
if strings.Compare(image[i], image[i+1]) == 0 {
log.Debug("start point: ", image[i], " vs ", image[i+1])
if walkToEdge(i, i+1) {
return i, true
}
}
}
return 0, false
}
// horizontalReflection returns the reflection point of a horizontal mirror
func horizontalReflection(image img, fn func(image []string) (int, bool)) (int, bool) {
return fn(image.raw)
}
// verticalReflection returns the reflection point of a vertical mirror
func verticalReflection(image img, fn func(image []string) (int, bool)) (int, bool) {
return fn(image.transposed())
}
// one returns a calculation based on reflection points for all images.
func one(lines []string) int {
parselines(lines)
sum := 0
for _, image := range images.img {
log.Debug("checking for horizontal reflection")
n, ok := horizontalReflection(image, findReflection)
if ok {
sum += 100 * (n + 1)
}
log.Debug("checking for vertical reflection")
n, ok = verticalReflection(image, findReflection)
if ok {
sum += (n + 1)
}
}
return sum
}

19
day-13/one_test.go Normal file
View File

@@ -0,0 +1,19 @@
package main
import (
"testing"
"github.com/go-playground/assert"
)
func TestTranspose(t *testing.T) {
//t.Skip("skipping test")
img := newImg()
img.raw = []string{"#.##", "..#.", "##.."}
expected := []string{"#.#", "#..", ".##", "..#"}
t.Run("Should flip the image ninety degrees rightwards", func(t *testing.T) {
assert.Equal(t, expected, img.transposed())
})
}

21
day-13/solution.go Normal file
View File

@@ -0,0 +1,21 @@
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
)
func init() {
log.SetLevel(log.InfoLevel)
}
func main() {
lines := readlines()
ans := one(lines)
fmt.Printf("solution one: %d\n", ans)
ans = two(lines)
fmt.Printf("solution two: %d\n", ans)
}

45
day-13/two.go Normal file
View File

@@ -0,0 +1,45 @@
package main
import (
log "github.com/sirupsen/logrus"
)
// findReflectionWithDifference returns the reflection point for an image with one smudge
func findReflectionWithDifference(image []string) (int, bool) {
walkToEdge := func(lower, upper int) bool {
diffs := 0
for lower >= 0 && upper < len(image) {
diffs += numDiffs(image[lower], image[upper])
lower--
upper++
}
return diffs == 1
}
for i := 0; i < len(image)-1; i++ {
log.Debug("start point: ", image[i], " vs ", image[i+1])
if walkToEdge(i, i+1) {
return i, true
}
}
return 0, false
}
// two returns a calculation based on reflection points for all images with one smudge
func two(lines []string) int {
sum := 0
for _, image := range images.img {
log.Debug("checking for horizontal reflection")
n, ok := horizontalReflection(image, findReflectionWithDifference)
if ok {
sum += 100 * (n + 1)
}
log.Debug("checking for vertical reflection")
n, ok = verticalReflection(image, findReflectionWithDifference)
if ok {
sum += (n + 1)
}
}
return sum
}

57
day-13/util.go Normal file
View File

@@ -0,0 +1,57 @@
package main
import (
"bufio"
"log"
"os"
)
// readlines reads lines from stdin.
// it returns them as an array of strings
func readlines() []string {
lines := []string{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return lines
}
// parselines stores each image into an images struct
func parselines(lines []string) {
addImage := func(i int) int {
image := newImg()
for ; i < len(lines); i++ {
if len(lines[i]) == 0 {
break
}
image.raw = append(image.raw, lines[i])
}
images.img = append(images.img, image)
return i
}
images = newImages()
for i := 0; i < len(lines); i++ {
next := addImage(i)
i = next
}
}
// numDiffs returns the number of difference between two strings
func numDiffs(a, b string) int {
diff := 0
for i := range a {
if a[i] != b[i] {
diff++
}
}
return diff
}

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

@@ -0,0 +1,3 @@
module github.com/onyx-and-iris/aoc2023/day-14
go 1.21.5

35
day-14/image.go Normal file
View File

@@ -0,0 +1,35 @@
package main
import "fmt"
type img struct {
raw []string
}
func newImg(sz int) img {
return img{raw: make([]string, sz)}
}
// transposed rotates an image rightwards ninety degrees
func (i img) transposed() []string {
transposed := []string{}
for x := 0; x < len(i.raw[0]); x++ {
buf := ""
for j := len(i.raw) - 1; j >= 0; j-- {
buf += string(i.raw[j][x])
}
transposed = append(transposed, buf)
}
return transposed
}
// String implements the fmt.Stringer interface
func (i img) String() string {
out := ""
for _, line := range i.raw {
out += fmt.Sprintf("%s\n", line)
}
return out
}

10
day-14/makefile Normal file
View File

@@ -0,0 +1,10 @@
TEST="test.txt"
INPUT="input.txt"
test:
go run . < $(TEST)
run:
go run . < $(INPUT)
all: test

58
day-14/one.go Normal file
View File

@@ -0,0 +1,58 @@
package main
// getload returns the load of all boulders for a single image
func getload(raw []string) int {
load := 0
for _, line := range raw {
for i, r := range line {
if r == 'O' {
load += (i + 1)
}
}
}
return load
}
// spacesToRight determines the number of spaces right of a boulder
func spacesToRight(i int, line string) int {
num := 0
outer:
for i += 1; i < len(line); i++ {
switch line[i] {
case '.':
num++
case '#', 'O':
break outer
}
}
return num
}
// rollRight rolls a boulder right until there's no space
func rollRight(line string) string {
for i := len(line) - 1; i >= 0; i-- {
if line[i] == 'O' {
n := spacesToRight(i, line)
if n == 0 {
continue
}
line = replaceAtIndex(line, '.', i)
line = replaceAtIndex(line, 'O', i+n)
}
}
return line
}
// one returns the load of all boulders after a single image transposition+roll
func one(lines []string) int {
image := newImg(len(lines))
copy(image.raw, lines)
for i, line := range image.transposed() {
image.raw[i] = rollRight(line)
}
return getload(image.raw)
}

21
day-14/solution.go Normal file
View File

@@ -0,0 +1,21 @@
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
)
func init() {
log.SetLevel(log.InfoLevel)
}
func main() {
lines := readlines()
ans := one(lines)
fmt.Printf("solution one: %d\n", ans)
ans = two(lines)
fmt.Printf("solution two: %d\n", ans)
}

47
day-14/two.go Normal file
View File

@@ -0,0 +1,47 @@
package main
const CYCLES = 1000000000
// cycleOnce transposes and rolls a single image four times (N,W,S,E)
func cycleOnce(image img) {
for i := 0; i < 4; i++ {
for i, line := range image.transposed() {
image.raw[i] = rollRight(line)
}
}
}
// cycleMany cycles a single image for a given number of iterations
// it caches seen images as well as their location in the period
// finally it copies the cached image with idx matching the interval to image.raw
func cycleMany(image img, iterations int) {
cachedIndexes := make(map[string]int)
cachedImages := make(map[int][]string)
i, start := 0, 0
for ; i < iterations; i++ {
cycleOnce(image)
if idx, ok := cachedIndexes[image.String()]; ok { // we found first repeated image
start = idx
i++
break
}
cachedIndexes[image.String()] = i + 1
cachedImages[i+1] = make([]string, len(image.raw))
copy(cachedImages[i+1], image.raw)
}
period := i - start // length of a full period
copy(image.raw, cachedImages[start+((iterations-i-1)%period)+1]) // copy cachedImage into image.raw
}
// two returns the load of all boulders after 1000000000 cycles
func two(lines []string) int {
image := newImg(len(lines))
copy(image.raw, lines)
cycleMany(image, CYCLES)
return getload(image.transposed())
}

31
day-14/util.go Normal file
View File

@@ -0,0 +1,31 @@
package main
import (
"bufio"
"log"
"os"
)
// readlines reads lines from stdin.
// it returns them as an array of strings
func readlines() []string {
lines := []string{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return lines
}
// replaceAtIndex replaces a character in a string at index i
func replaceAtIndex(in string, r rune, i int) string {
out := []rune(in)
out[i] = r
return string(out)
}

16
day-15/go.mod Normal file
View File

@@ -0,0 +1,16 @@
module github.com/onyx-and-iris/aoc2023/day-15
go 1.21.5
require (
github.com/elliotchance/orderedmap/v2 v2.2.0
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.7.1
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)

19
day-15/go.sum Normal file
View File

@@ -0,0 +1,19 @@
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/elliotchance/orderedmap/v2 v2.2.0 h1:7/2iwO98kYT4XkOjA9mBEIwvi4KpGB4cyHeOFOnj4Vk=
github.com/elliotchance/orderedmap/v2 v2.2.0/go.mod h1:85lZyVbpGaGvHvnKa7Qhx7zncAdBIBq6u56Hb1PRU5Q=
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/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
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=

30
day-15/hasher.go Normal file
View File

@@ -0,0 +1,30 @@
package main
// hasher defines the methods required to hash a string
type hasher struct {
next int
}
// newHasher returns a hasher type
func newHasher() hasher {
return hasher{}
}
// run processes each step of the hasher for each char in a string
func (h hasher) run(in string) int {
hash := 0
for _, r := range in {
h.next = hash + int(r)
hash = h.multiply().modulus()
}
return hash
}
func (h hasher) multiply() hasher {
h.next *= 17
return h
}
func (h hasher) modulus() int {
return h.next % 256
}

29
day-15/hasher_test.go Normal file
View File

@@ -0,0 +1,29 @@
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestHasher(t *testing.T) {
//t.Skip("skipping test")
hash := newHasher()
t.Run("Should produce a hash value 30", func(t *testing.T) {
assert.Equal(t, 30, hash.run("rn=1"))
})
t.Run("Should produce a hash value 253", func(t *testing.T) {
assert.Equal(t, 253, hash.run("cm-"))
})
t.Run("Should produce a hash value 0", func(t *testing.T) {
assert.Equal(t, 0, hash.run("rn"))
})
t.Run("Should produce a hash value 3", func(t *testing.T) {
assert.Equal(t, 3, hash.run("pc"))
})
}

10
day-15/makefile Normal file
View File

@@ -0,0 +1,10 @@
TEST="test.txt"
INPUT="input.txt"
test:
go run . < $(TEST)
run:
go run . < $(INPUT)
all: test

18
day-15/one.go Normal file
View File

@@ -0,0 +1,18 @@
package main
import "strings"
var lenses = []string{}
// one returns the sum of all hashed values
func one(lines []string) int {
lenses = strings.Split(lines[0], ",")
hash := newHasher()
sum := 0
for _, lense := range lenses {
sum += hash.run(lense)
}
return sum
}

22
day-15/solution.go Normal file
View File

@@ -0,0 +1,22 @@
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
)
func init() {
log.SetLevel(log.InfoLevel)
}
func main() {
lines := readlines()
ans := one(lines)
fmt.Printf("solution one: %d\n", ans)
ans = two(lines)
fmt.Printf("solution two: %d\n", ans)
}

46
day-15/two.go Normal file
View File

@@ -0,0 +1,46 @@
package main
import (
"strings"
"github.com/elliotchance/orderedmap/v2"
)
var boxes = map[int]*orderedmap.OrderedMap[string, int]{}
// two returns the sum of all lense configurations
func two(lines []string) int {
hash := newHasher()
for _, lense := range lenses {
label, focalLength := func() (string, int) {
x := strings.Split(lense, "=")
if len(x) == 2 {
return x[0], mustConv(x[1])
}
return strings.TrimRight(x[0], "-"), 0
}()
boxId := hash.run(label)
_, ok := boxes[boxId]
if !ok {
boxes[boxId] = orderedmap.NewOrderedMap[string, int]()
}
if strings.Contains(lense, "=") {
boxes[boxId].Set(label, focalLength)
} else {
boxes[boxId].Delete(label)
}
}
sum := 0
for id, box := range boxes {
for index, lense := range box.Keys() {
focalLength, _ := box.Get(lense)
sum += (id + 1) * (index + 1) * focalLength
}
}
return sum
}

35
day-15/util.go Normal file
View File

@@ -0,0 +1,35 @@
package main
import (
"bufio"
"log"
"os"
"strconv"
)
// readlines reads lines from stdin.
// it returns them as an array of strings
func readlines() []string {
lines := []string{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return lines
}
// mustConv converts string to int
// it will panic if an error occurs
func mustConv(s string) int {
n, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return n
}

30
day-16/debug.go Normal file
View File

@@ -0,0 +1,30 @@
package main
import "fmt"
func printDebug(move *mover, lines []string) int {
num := 0
for i, line := range lines {
for j, r := range line {
inNodes := func(c coords) bool {
for _, node := range move.nodes {
if c.X == node.X && c.Y == node.Y {
return true
}
}
return false
}(newCoords(j, i))
if inNodes {
fmt.Printf("#")
num++
//} else if r == '|' || r == '\\' || r == '/' || r == '-' {
//fmt.Printf(".")
} else {
fmt.Printf("%c", r)
}
}
fmt.Println()
}
return num
}

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

@@ -0,0 +1,3 @@
module github.com/onyx-and-iris/aoc2023/day-16
go 1.21.5

10
day-16/makefile Normal file
View File

@@ -0,0 +1,10 @@
TEST="test.txt"
INPUT="input.txt"
test:
go run . < $(TEST)
run:
go run . < $(INPUT)
all: test

64
day-16/mover.go Normal file
View File

@@ -0,0 +1,64 @@
package main
import (
"fmt"
)
type coords struct {
X int
Y int
}
func newCoords(x, y int) coords {
return coords{X: x, Y: y}
}
// node represents a single node with coordinates and direction
type node struct {
coords
direction int
}
func newNode(x, y int, direction int) node {
return node{coords: newCoords(x, y), direction: direction}
}
// String implements the fmt.Stringer interface
func (n node) String() string {
return fmt.Sprintf("%v%s", n.coords, dirs[n.direction])
}
type mover struct {
node
nodes []node
}
// newMover sets the start coordinates and direction
// it returns a mover type
func newMover(n node) *mover {
return &mover{node: n, nodes: make([]node, 0)}
}
// direction returns the current node direction
func (m *mover) direction() int {
return m.node.direction
}
// setDirection sets the current node direction
func (m *mover) setDirection(val int) {
m.node.direction = val
}
// move shifts the X,Y coordinate by one depending on direction
func (m *mover) move() {
switch m.node.direction {
case N:
m.Y--
case S:
m.Y++
case W:
m.X--
case E:
m.X++
}
}

125
day-16/one.go Normal file
View File

@@ -0,0 +1,125 @@
package main
import (
"math"
log "github.com/sirupsen/logrus"
)
const (
SPACE = '.'
V_MIRROR = '|'
H_MIRROR = '-'
F_MIRROR = '/'
B_MIRROR = '\\'
)
const (
N = iota
S
W
E
)
// only for debugging
var steps int
var dirs = []string{"N", "S", "W", "E"}
func runner(mover *mover, lines []string) {
for steps < math.MaxInt && mover.Y >= 0 && mover.Y < len(lines) && mover.X >= 0 && mover.X < len(lines[mover.Y]) {
if nodeInNodes(mover.node, mover.nodes) {
log.Debug(mover.node, " in nodes, breaking.")
break
}
mover.nodes = append(mover.nodes, mover.node)
switch lines[mover.Y][mover.X] {
case SPACE: // '.'
log.Debug("we have space and direction is ", dirs[mover.direction()])
mover.move()
case F_MIRROR: // '/'
log.Debug("we have forward mirror and direction is ", dirs[mover.direction()])
switch mover.direction() {
case N:
mover.setDirection(E)
case S:
mover.setDirection(W)
case W:
mover.setDirection(S)
case E:
mover.setDirection(N)
}
log.Debug("step: ", steps, " ", string(F_MIRROR), " direction changed to ", dirs[mover.direction()])
mover.move()
case B_MIRROR: // '\'
log.Debug("we have backwards mirror and direction is ", dirs[mover.direction()])
switch mover.direction() {
case N:
mover.setDirection(W)
case S:
mover.setDirection(E)
case W:
mover.setDirection(N)
case E:
mover.setDirection(S)
}
log.Debug("step: ", steps, " ", string(B_MIRROR), " direction changed to ", dirs[mover.direction()])
mover.move()
case V_MIRROR: // '|'
log.Debug("we have vertical mirror and direction is ", dirs[mover.direction()])
if mover.direction() == N || mover.direction() == S {
mover.move()
continue
}
if mover.direction() == W || mover.direction() == E {
c := mover.coords
mover.setDirection(N)
mover.move()
runner(mover, lines)
mover.coords = c
mover.setDirection(S)
mover.move()
runner(mover, lines)
}
case H_MIRROR: // '-'
log.Debug("we have horizontal mirror and direction is ", dirs[mover.direction()])
if mover.direction() == W || mover.direction() == E {
mover.move()
continue
}
if mover.direction() == N || mover.direction() == S {
c := mover.coords
mover.setDirection(W)
mover.move()
runner(mover, lines)
mover.coords = c
mover.setDirection(E)
mover.move()
runner(mover, lines)
}
default:
log.Fatal("unknown node")
}
}
steps++
}
// one
func one(lines []string) int {
mover := newMover(newNode(0, 0, E))
runner(mover, lines)
if log.GetLevel() == log.DebugLevel {
n := printDebug(mover, lines)
log.Debug("total: ", n)
}
return uniqueNodes(mover)
}

21
day-16/solution.go Normal file
View File

@@ -0,0 +1,21 @@
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
)
func init() {
log.SetLevel(log.InfoLevel)
}
func main() {
lines := readlines()
ans := one(lines)
fmt.Printf("solution one: %d\n", ans)
ans = two(lines)
fmt.Printf("solution two: %d\n", ans)
}

77
day-16/two.go Normal file
View File

@@ -0,0 +1,77 @@
package main
import "sync"
// returns the number of unique nodes (reducing multiple nodes with different directions to one)
func uniqueNodes(mover *mover) int {
uniqueCoords := []coords{}
for _, node := range mover.nodes {
if !coordInCoords(node.coords, uniqueCoords) {
uniqueCoords = append(uniqueCoords, node.coords)
}
}
return len(uniqueCoords)
}
// spawn invoked a single runner with a single mover
func spawn(i, j, direction int, lines []string) int {
m := newMover(newNode(i, j, direction))
runner(m, lines)
return uniqueNodes(m)
}
var wg sync.WaitGroup
// two returns the highest energized value for any beam spawn point/direction
func two(lines []string) int {
res := 0
n := 0
for i := 0; i < len(lines[0]); i++ {
wg.Add(1)
go func(x int) {
defer wg.Done()
n = spawn(x, 0, S, lines)
if n > res {
res = n
}
}(i)
wg.Add(1)
go func(x int) {
defer wg.Done()
n = spawn(x, len(lines[0])-1, N, lines)
if n > res {
res = n
}
}(i)
}
for i := 0; i < len(lines); i++ {
wg.Add(1)
go func(y int) {
defer wg.Done()
n = spawn(0, y, E, lines)
if n > res {
res = n
}
}(i)
wg.Add(1)
go func(y int) {
defer wg.Done()
n = spawn(len(lines[0])-1, y, W, lines)
if n > res {
res = n
}
}(i)
}
wg.Wait()
return res
}

45
day-16/util.go Normal file
View File

@@ -0,0 +1,45 @@
package main
import (
"bufio"
"log"
"os"
)
// readlines reads lines from stdin.
// it returns them as an array of strings
func readlines() []string {
lines := []string{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return lines
}
// nodeInNodes returns true if node n is in nodes
// X, Y coords and direction must match
func nodeInNodes(n node, nodes []node) bool {
for _, node := range nodes {
if n.X == node.X && n.Y == node.Y && n.direction == node.direction {
return true
}
}
return false
}
// coordInCoords returns true if coords c is in coords
func coordInCoords(c coords, coords []coords) bool {
for _, coord := range coords {
if c.X == coord.X && c.Y == coord.Y {
return true
}
}
return false
}

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

@@ -0,0 +1,3 @@
module github.com/onyx-and-iris/aoc2023/day-17
go 1.21.5

10
day-17/makefile Normal file
View File

@@ -0,0 +1,10 @@
TEST="test.txt"
INPUT="input.txt"
test:
go run . < $(TEST)
run:
go run . < $(INPUT)
all: test

32
day-17/node.go Normal file
View File

@@ -0,0 +1,32 @@
package main
import "fmt"
type coords struct {
X int
Y int
}
func newCoords(x, y int) coords {
return coords{X: x, Y: y}
}
// node represents a single point on the graph
type node struct {
cost int
distance int
directionX int
directionY int
coords
index int
}
func newNode(cost, distance, directionX, directionY, x, y int) *node {
c := newCoords(x, y)
return &node{cost: cost, distance: distance, directionX: directionX, directionY: directionY, coords: c}
}
// String implements the fmt.Stringer interface
func (n node) String() string {
return fmt.Sprintf("%d%d%d%v", n.distance, n.directionX, n.directionY, n.coords)
}

114
day-17/one.go Normal file
View File

@@ -0,0 +1,114 @@
package main
import (
"container/heap"
log "github.com/sirupsen/logrus"
)
type option func(*dijkstra)
func WithMinDistance(distance int) option {
return func(d *dijkstra) {
d.minDistance = distance
}
}
func WithMaxDistance(distance int) option {
return func(d *dijkstra) {
d.maxDistance = distance
}
}
type dijkstra struct {
graph [][]int
minDistance int
maxDistance int
}
func newDijkstra(graph [][]int, opts ...option) *dijkstra {
d := &dijkstra{graph: graph}
for _, opt := range opts {
opt(d)
}
return d
}
func (d dijkstra) initialize(start coords) *pqueue {
pq := newPriorityQueue()
heap.Init(pq)
// we don't encounter heat loss for start point unless we enter this block again
heap.Push(pq, newNode(0, 0, 0, 0, start.X, start.Y))
return pq
}
// run performs the lowest cost dijkstra algorithm from start to end
func (d dijkstra) run(start, end coords) int {
pq := d.initialize(start)
visited := map[string]bool{}
for pq.Len() > 0 {
cost, node := func() (int, *node) {
x := heap.Pop(pq).(*node)
return x.cost, x
}()
// we reached final location, return its lowest cost
if node.X == end.X && node.Y == end.Y && node.distance >= d.minDistance {
log.Debug("returning lowest cost with min distance >= ", d.minDistance)
return node.cost
}
if _, ok := visited[node.String()]; ok {
continue
}
visited[node.String()] = true
var neighbours = [][]int{{0, -1}, {0, 1}, {-1, 0}, {1, 0}} // N, S, W, E
for _, n := range neighbours {
nextX := node.X + n[0]
nextY := node.Y + n[1]
if nextY < 0 || nextY >= len(d.graph) || nextX < 0 || nextX >= len(d.graph[nextY]) {
continue
}
if node.directionX == -n[0] && node.directionY == -n[1] { // are we going backwards?
continue
}
var distance = 1
if node.directionX == n[0] || node.directionY == n[1] { // same direction
distance = node.distance + 1
} else {
if node.distance < d.minDistance {
continue
}
}
if distance > d.maxDistance {
continue
}
new_cost := cost + d.graph[nextY][nextX]
heap.Push(pq, newNode(new_cost, distance, n[0], n[1], nextX, nextY))
}
}
return 0
}
// one returns the lowest cost path for the given graph from start to end coords
func one(lines []string) int {
graph := buildGraph(lines)
start := newCoords(0, 0)
end := newCoords(len(graph[0])-1, len(graph)-1)
dijkstra := newDijkstra(graph, WithMaxDistance(3))
cost := dijkstra.run(start, end)
return cost
}

49
day-17/pqueue.go Normal file
View File

@@ -0,0 +1,49 @@
package main
import (
"container/heap"
)
// pqueue represents a min priority queue
// it implements the heap.Interface interface
type pqueue []*node
func newPriorityQueue() *pqueue {
pq := make(pqueue, 0)
return &pq
}
func (pq pqueue) Len() int {
return len(pq)
}
func (pq *pqueue) Push(x interface{}) {
n := len(*pq)
node := x.(*node)
node.index = n
*pq = append(*pq, node)
}
func (pq *pqueue) Pop() interface{} {
old := *pq
n := len(old)
node := old[n-1]
node.index = -1
*pq = old[0 : n-1]
return node
}
func (pq *pqueue) Update(node *node, value int) {
node.cost = value
heap.Fix(pq, node.index)
}
func (pq pqueue) Less(i, j int) bool {
return pq[i].cost < pq[j].cost
}
func (pq pqueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].index = i
pq[j].index = j
}

27
day-17/pqueue_test.go Normal file
View File

@@ -0,0 +1,27 @@
package main
import (
"container/heap"
"testing"
"github.com/go-playground/assert/v2"
)
func TestPriorityQueue(t *testing.T) {
//t.Skip("skipping test")
pq := newPriorityQueue()
heap.Push(pq, newNode(30, 0, 0, 0, 0, 0))
heap.Push(pq, newNode(10, 0, 0, 0, 8, 0))
heap.Push(pq, newNode(20, 0, 0, 0, 13, 0))
t.Run("Should create a queue size 3", func(t *testing.T) {
assert.Equal(t, 3, pq.Len())
})
item := heap.Pop(pq).(*node)
t.Run("Should return item with cost 10", func(t *testing.T) {
assert.Equal(t, 10, item.cost)
})
}

21
day-17/solution.go Normal file
View File

@@ -0,0 +1,21 @@
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
)
func init() {
log.SetLevel(log.InfoLevel)
}
func main() {
lines := readlines()
ans := one(lines)
fmt.Printf("solution one: %d\n", ans)
ans = two(lines)
fmt.Printf("solution two: %d\n", ans)
}

14
day-17/two.go Normal file
View File

@@ -0,0 +1,14 @@
package main
// two returns the lowest cost path for a given graph from start to end coords
// with a min/max distance set
func two(lines []string) int {
graph := buildGraph(lines)
start := newCoords(0, 0)
end := newCoords(len(graph[0])-1, len(graph)-1)
dijkstra := newDijkstra(graph, WithMinDistance(4), WithMaxDistance(10))
cost := dijkstra.run(start, end)
return cost
}

26
day-17/two_test.go Normal file
View File

@@ -0,0 +1,26 @@
package main
import (
_ "embed"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var (
//go:embed test2.txt
testInput2 []byte
)
func TestDjistraWithMinDistance(t *testing.T) {
//t.Skip("skipping test")
input := strings.Split(string(testInput2), "\n")
cost := two(input)
t.Run("Should return a lowest cost of 71", func(t *testing.T) {
assert.Equal(t, 71, cost)
})
}

38
day-17/util.go Normal file
View File

@@ -0,0 +1,38 @@
package main
import (
"bufio"
"log"
"os"
)
// readlines reads lines from stdin.
// it returns them as an array of strings
func readlines() []string {
lines := []string{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return lines
}
// buildGraph parses lines into costs for graph
func buildGraph(lines []string) [][]int {
graph := make([][]int, len(lines))
for i, line := range lines {
graph[i] = make([]int, len(line))
for j, r := range line {
graph[i][j] = int(r - '0')
}
}
return graph
}

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

@@ -0,0 +1,3 @@
module github.com/onyx-and-iris/aoc2023/day-18
go 1.21.5

37
day-18/imager.go Normal file
View File

@@ -0,0 +1,37 @@
package main
type coords struct {
X int
Y int
}
func newCoords(x, y int) coords {
return coords{X: x, Y: y}
}
type imager struct {
point coords
space []coords
}
// newImager returns an imager type
func newImager() *imager {
return &imager{point: newCoords(0, 0), space: make([]coords, 0)}
}
// add appends new coordinates to all points that describe the polygon
func (i *imager) add(direction string, count int) {
for j := 0; j < count; j++ {
switch direction {
case "U":
i.point.Y--
case "D":
i.point.Y++
case "L":
i.point.X--
case "R":
i.point.X++
}
i.space = append(i.space, newCoords(i.point.X, i.point.Y))
}
}

10
day-18/makefile Normal file
View File

@@ -0,0 +1,10 @@
TEST="test.txt"
INPUT="input.txt"
test:
go run . < $(TEST)
run:
go run . < $(INPUT)
all: test

29
day-18/one.go Normal file
View File

@@ -0,0 +1,29 @@
package main
import (
"regexp"
)
var r = regexp.MustCompile(`(?P<direction>[A-Z]) (?P<count>[0-9]+) \((?P<colour>.*)\)`)
func fromRegex(imager *imager, lines []string) {
for _, line := range lines {
direction, count := func() (string, int) {
x := getParams(r, line)
return x["direction"], mustConv(x["count"])
}()
imager.add(direction, count)
}
}
func buildImage(withParser func(imager *imager, lines []string), imager *imager, lines []string) {
withParser(imager, lines)
}
// one returns the area of the polygon described by imager.space
func one(lines []string) int {
imager := newImager()
buildImage(fromRegex, imager, lines)
return calculateArea(imager.space)
}

22
day-18/solution.go Normal file
View File

@@ -0,0 +1,22 @@
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
)
func init() {
log.SetLevel(log.InfoLevel)
}
func main() {
lines := readlines()
ans := one(lines)
fmt.Printf("solution one: %d\n", ans)
ans = two(lines)
fmt.Printf("solution two: %d\n", ans)
}

26
day-18/two.go Normal file
View File

@@ -0,0 +1,26 @@
package main
import (
"strings"
)
func fromHex(imager *imager, lines []string) {
for _, line := range lines {
direction, count := func() (string, int) {
var dirs = []string{"R", "D", "L", "U"}
m := getParams(r, line)
code := strings.TrimLeft(m["colour"], "#")
return dirs[mustConv(string(code[len(code)-1]))], mustConvHex(code[:len(code)-1])
}()
imager.add(direction, count)
}
}
// two returns the area of the polygon described by imager.space
// it uses the hex codes to define points
func two(lines []string) int {
imager := newImager()
buildImage(fromHex, imager, lines)
return calculateArea(imager.space)
}

73
day-18/util.go Normal file
View File

@@ -0,0 +1,73 @@
package main
import (
"bufio"
"log"
"math"
"os"
"regexp"
"strconv"
)
// readlines reads lines from stdin.
// it returns them as an array of strings
func readlines() []string {
lines := []string{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return lines
}
func getParams(rexp *regexp.Regexp, url string) map[string]string {
match := rexp.FindStringSubmatch(url)
m := make(map[string]string)
for i, name := range rexp.SubexpNames() {
if i > 0 && i <= len(match) {
m[name] = match[i]
}
}
return m
}
// mustConv converts string to int
// it will panic if an error occurs
func mustConv(s string) int {
n, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return n
}
// mustConvHex converts a hex string to int
// it will panic if an error occurs
func mustConvHex(s string) int {
n, err := strconv.ParseInt(s, 16, 64)
if err != nil {
panic(err)
}
return int(n)
}
// calculateArea returns the area of the polygon described by imager.space
func calculateArea(space []coords) int {
area := 0
for i := 0; i < len(space); i++ {
next := space[(i+1)%len(space)]
area += space[i].X*next.Y - space[i].Y*next.X
}
// add perimeter to area within perimeter
area = len(space) + (int(math.Abs(float64(area))) / 2)
return area - len(space)/2 + 1
}

View File

@@ -1,3 +1,3 @@
module github.com/onyx-and-iris/aoc2023/day-2
go 1.20
go 1.21.5

10
day-2/makefile Normal file
View File

@@ -0,0 +1,10 @@
TEST="test.txt"
INPUT="input.txt"
test:
go run . < $(TEST)
run:
go run . < $(INPUT)
all: test

View File

@@ -1,5 +0,0 @@
#!/usr/bin/env bash
INPUT="input.txt"
cat $INPUT | go run .

View File

@@ -1,5 +1,5 @@
module github.com/onyx-and-iris/aoc2023/day-3
go 1.20
go 1.21.5
require github.com/go-playground/assert/v2 v2.2.0

10
day-3/makefile Normal file
View File

@@ -0,0 +1,10 @@
TEST="test.txt"
INPUT="input.txt"
test:
go run . < $(TEST)
run:
go run . < $(INPUT)
all: test

View File

@@ -1,5 +0,0 @@
#!/usr/bin/env bash
INPUT="input.txt"
cat $INPUT | go run .

View File

@@ -1,5 +1,5 @@
module github.com/onyx-and-iris/aoc2023/day-4
go 1.20
go 1.21.5
require github.com/go-playground/assert v1.2.1

10
day-4/makefile Normal file
View File

@@ -0,0 +1,10 @@
TEST="test.txt"
INPUT="input.txt"
test:
go run . < $(TEST)
run:
go run . < $(INPUT)
all: test

View File

@@ -1,5 +0,0 @@
#!/usr/bin/env bash
INPUT="input.txt"
cat $INPUT | go run .

View File

@@ -1,19 +1,22 @@
package main
type Data struct {
type data struct {
dest int
source int
offset int
}
var seeds = []int{}
var dataMap = map[string][]Data{
"seed-to-soil": make([]Data, 0),
"soil-to-fertilizer": make([]Data, 0),
"fertilizer-to-water": make([]Data, 0),
"water-to-light": make([]Data, 0),
"light-to-temperature": make([]Data, 0),
"temperature-to-humidity": make([]Data, 0),
"humidity-to-location": make([]Data, 0),
func newData(nums ...int) data {
return data{dest: nums[0], source: nums[1], offset: nums[2]}
}
func (d data) transform(start, end int) (int, int) {
f := func(x int) int {
return x - d.source + d.dest
}
return f(start), f(end - 1)
}
var dataMap = map[string][]data{}
var identifiers = []string{}

View File

@@ -1,6 +1,6 @@
module github.com/onyx-and-iris/aoc2023/day-5
go 1.20
go 1.21.5
require github.com/sirupsen/logrus v1.9.3

10
day-5/makefile Normal file
View File

@@ -0,0 +1,10 @@
TEST="test.txt"
INPUT="input.txt"
test:
go run . < $(TEST)
run:
go run . < $(INPUT)
all: test

View File

@@ -2,43 +2,36 @@ package main
import (
"math"
log "github.com/sirupsen/logrus"
)
var identifiers = []string{"seed-to-soil", "soil-to-fertilizer", "fertilizer-to-water", "water-to-light", "light-to-temperature", "temperature-to-humidity", "humidity-to-location"}
var seeds = []int{}
// next recursively calculates each destination for each set of data in dataMap
func next(i int, datapoint int) int {
if i == len(identifiers) {
return datapoint
}
dest := func() int {
datas := dataMap[identifiers[i]]
dest := 0
for _, data := range datas {
if datapoint >= data.source && datapoint <= data.source+data.offset {
dest = data.dest + (datapoint - data.source)
break
}
dest := 0
for _, data := range dataMap[identifiers[i]] {
if datapoint >= data.source && datapoint <= data.source+data.offset {
dest = data.dest + (datapoint - data.source)
break
}
if dest == 0 {
dest = datapoint
}
return dest
}()
//log.Debug(identifiers[i], ": ", dest)
}
if dest == 0 {
dest = datapoint
}
return next(i+1, dest)
}
// one returns the lowest location
// one returns the lowest location for any seed in seeds
func one(lines []string) (int, error) {
lowest := math.MaxInt
parseLines(lines)
lowest := math.MaxInt
for _, seed := range seeds {
location := next(0, seed)
log.Info(location)
if location < lowest {
lowest = location
}

51
day-5/queue.go Normal file
View File

@@ -0,0 +1,51 @@
package main
import (
log "github.com/sirupsen/logrus"
)
// queue represents a FIFO queue of bounds
type queue struct {
size int
elements []bound
}
// newQueue returns a queue type
// it initializes the queue size and elements
func newQueue(size int, elems []bound) queue {
return queue{size: size, elements: elems}
}
// enqueue adds an item to the queue
func (q *queue) enqueue(elem bound) {
if q.size >= 0 && q.getLength() == q.size {
log.Info("Queue Overflow")
return
}
q.elements = append(q.elements, elem)
}
// dequeue pops an element from the start of the queue
func (q *queue) dequeue() bound {
if q.isEmpty() {
log.Info("Queue UnderFlow")
return bound{}
}
element := q.elements[0]
if q.getLength() == 1 {
q.elements = nil
return element
}
q.elements = q.elements[1:]
return element
}
// getLength returns the number of items in the queue
func (q *queue) getLength() int {
return len(q.elements)
}
// isEmpty returns true if no items are in the queue
func (q *queue) isEmpty() bool {
return len(q.elements) == 0
}

View File

@@ -1,5 +0,0 @@
#!/usr/bin/env bash
INPUT="input.txt"
cat $INPUT | go run .

View File

@@ -7,7 +7,7 @@ import (
)
func init() {
log.SetLevel(log.DebugLevel)
log.SetLevel(log.InfoLevel)
}
func main() {
@@ -19,9 +19,6 @@ func main() {
}
fmt.Printf("solution one: %d\n", ans)
ans, err = two(lines)
if err != nil {
log.Fatal(err)
}
ans = two(lines)
fmt.Printf("solution two: %d\n", ans)
}

View File

@@ -1,78 +1,89 @@
package main
import (
"fmt"
"math"
"sync"
"sync/atomic"
"time"
log "github.com/sirupsen/logrus"
)
type WaitGroupCount struct {
sync.WaitGroup
count int64
}
var wg sync.WaitGroup
var mu sync.Mutex
func (wg *WaitGroupCount) Add(delta int) {
atomic.AddInt64(&wg.count, int64(delta))
wg.WaitGroup.Add(delta)
}
func (wg *WaitGroupCount) Done() {
atomic.AddInt64(&wg.count, -1)
wg.WaitGroup.Done()
}
func (wg *WaitGroupCount) GetCount() int {
return int(atomic.LoadInt64(&wg.count))
}
var wg = WaitGroupCount{}
//var checked = make([]bound, 0)
const UNLIMITED = -1
// bound represents the lower and upper limits of a single range
type bound struct {
start int
end int
lower, upper int
}
var bounds = []bound{}
// newBound returns a bound type
// it defines an open ended interval
func newBound(lower, upper int) bound {
return bound{lower: lower, upper: upper - 1}
}
// two returns the lowest location
func two(lines []string) (int, error) {
lowest := math.MaxInt
for i := 0; i < len(seeds); i += 2 {
bounds = append(bounds, bound{start: seeds[i], end: seeds[i] + seeds[i+1]})
// nextTransform recursively calculates each new set of seed ranges for each set of data in dataMap
func nextTransform(i int, in []bound) []bound {
if i == len(identifiers) {
return in
}
startTime := time.Now()
q := newQueue(UNLIMITED, in)
in = make([]bound, 0)
for !q.isEmpty() {
r := q.dequeue()
go func() {
for {
elapsed := time.Since(startTime)
fmt.Printf("[%s] wg count: %d\n", elapsed.Round(time.Second), wg.GetCount())
time.Sleep(time.Second)
}
}()
hasOverlap := func() bool {
for _, data := range dataMap[identifiers[i]] {
start := max(r.lower, data.source)
end := min(r.upper, data.source+data.offset)
for _, bound := range bounds {
wg.Add(1)
go func(start int, end int) {
defer wg.Done()
for i := start; i < end; i++ {
location := next(0, i)
if location < lowest {
lowest = location
if isOverlapping(start, end) {
// add new seed range
in = append(in, newBound(data.transform(start, end)))
// append unmatched portions of seed range back into queue
if start > r.lower {
q.enqueue(newBound(r.lower, start))
}
if r.upper > end {
q.enqueue(newBound(end, r.upper))
}
return true
}
}
}(bound.start, bound.end)
log.Info(bound, " completed")
return false
}()
// there was no overlap, add the seed range as is
if !hasOverlap {
in = append(in, r)
}
}
return nextTransform(i+1, in)
}
// two returns the lowest location for any seed in seedRanges
func two(lines []string) int {
var seedRanges = []bound{}
for i := 0; i < len(seeds); i += 2 {
wg.Add(1)
go func(i int) {
defer wg.Done()
mu.Lock()
seedRanges = append(seedRanges, nextTransform(0, []bound{newBound(seeds[i], seeds[i]+seeds[i+1])})...)
mu.Unlock()
}(i)
}
wg.Wait()
return lowest - 1, nil // returning a value one too high? not sure why.
lowest := math.MaxInt
for _, r := range seedRanges {
if r.lower < lowest {
lowest = r.lower
}
}
return lowest
}

View File

@@ -2,12 +2,13 @@ package main
import (
"bufio"
"log"
"os"
"regexp"
"strconv"
"strings"
"unicode"
log "github.com/sirupsen/logrus"
)
// readlines reads lines from stdin.
@@ -27,28 +28,27 @@ func readlines() []string {
return lines
}
var r = regexp.MustCompile(`([\w-]+) map[:]`)
// parseLines parses input to a data map
func parseLines(lines []string) {
var _regex_identifier = regexp.MustCompile(`(?P<identifier>[\w-]+) map[:]`)
f := func(c rune) bool {
return !unicode.IsDigit(c)
}
for i := 0; i < len(lines); i++ {
if i == 0 {
seeds = convertToInts(strings.FieldsFunc(lines[i], f))
continue
}
seeds = convertToInts(strings.FieldsFunc(lines[0], f))
m := _regex_identifier.FindStringSubmatch(lines[i])
for i := 2; i < len(lines); i++ {
m := r.FindStringSubmatch(lines[i])
if len(m) == 2 {
identifiers = append(identifiers, m[1])
for i = i + 1; i < len(lines) && len(lines[i]) != 0; i++ {
nums := convertToInts(strings.FieldsFunc(lines[i], f))
dataMap[m[1]] = append(dataMap[m[1]], Data{dest: nums[0], source: nums[1], offset: nums[2]})
d := newData(convertToInts(strings.FieldsFunc(lines[i], f))...)
dataMap[m[1]] = append(dataMap[m[1]], d)
}
}
}
log.Debug(identifiers)
}
// convertToInts converts a string representing ints to an array of ints
@@ -61,13 +61,8 @@ func convertToInts(data []string) []int {
return nums
}
/*
func isChecked(i int) bool {
for _, bound := range checked {
if i > bound.start && i < bound.end {
return true
}
}
return false
// isOverlapping returns true if two ranges overlap. see:
// https://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap/325964#325964
func isOverlapping(maxStart, minEnd int) bool {
return maxStart < minEnd
}
*/

View File

@@ -1,3 +1,3 @@
module github.com/onyx-and-iris/aoc2023/day-6
go 1.20
go 1.21.5

10
day-6/makefile Normal file
View File

@@ -0,0 +1,10 @@
TEST="test.txt"
INPUT="input.txt"
test:
go run . < $(TEST)
run:
go run . < $(INPUT)
all: test

View File

@@ -1,5 +0,0 @@
#!/usr/bin/env bash
INPUT="input.txt"
cat $INPUT | go run .

View File

@@ -1,6 +1,6 @@
module github.com/onyx-and-iris/aoc2023/day-7
go 1.20
go 1.21.5
require (
github.com/samber/lo v1.39.0 // indirect

10
day-7/makefile Normal file
View File

@@ -0,0 +1,10 @@
TEST="test.txt"
INPUT="input.txt"
test:
go run . < $(TEST)
run:
go run . < $(INPUT)
all: test

View File

@@ -1,5 +0,0 @@
#!/usr/bin/env bash
INPUT="input.txt"
cat $INPUT | go run .

View File

@@ -1,3 +1,3 @@
module github.com/onyx-and-iris/aoc2023/day-8
go 1.20
go 1.21.5

10
day-8/makefile Normal file
View File

@@ -0,0 +1,10 @@
TEST="test.txt"
INPUT="input.txt"
test:
go run . < $(TEST)
run:
go run . < $(INPUT)
all: test

View File

@@ -1,5 +0,0 @@
#!/usr/bin/env bash
INPUT="input.txt"
cat $INPUT | go run .

View File

@@ -1,3 +1,3 @@
module github.com/onyx-and-iris/aoc2023/day-9
go 1.20
go 1.21.5

10
day-9/makefile Normal file
View File

@@ -0,0 +1,10 @@
TEST="test.txt"
INPUT="input.txt"
test:
go run . < $(TEST)
run:
go run . < $(INPUT)
all: test

View File

@@ -1,5 +0,0 @@
#!/usr/bin/env bash
INPUT="input.txt"
cat $INPUT | go run .