mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2026-04-19 11:33:31 +00:00
Compare commits
72 Commits
c9ea61c7f4
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| cacd140961 | |||
| 0f357df19d | |||
| 5b225a9166 | |||
| 9ba5ad695e | |||
| cc452c76a4 | |||
| 73298dae8d | |||
| 35776a5470 | |||
| 2814b12750 | |||
| e541631a35 | |||
| 038dd531d9 | |||
| 497da642b2 | |||
| 58f36581f0 | |||
| cb3506cfbc | |||
| 171e50ebe4 | |||
| 3a3af64df9 | |||
| 801ed17bf1 | |||
| 6dae7502db | |||
| 6dc1631eb3 | |||
| e5a75872fe | |||
| 7faa19e159 | |||
| e7635b5e4c | |||
| 20713a9967 | |||
| 927e78aa3a | |||
| 8655265908 | |||
| e29029c688 | |||
| b2819a20dd | |||
| 6a65b04ee8 | |||
| 92ff666ae2 | |||
| 219bb3884b | |||
| 621a0dbd28 | |||
| eab63b0b36 | |||
| 94ffd625c9 | |||
| b55019149b | |||
| e8f7c2a63d | |||
| b98379ca2b | |||
| 0c26ef875a | |||
| b878eb9df6 | |||
| 3d42da7862 | |||
| 0fc0c49387 | |||
| 8b61fa2011 | |||
| 0f254a243e | |||
| e1829e595a | |||
| a67a87ad96 | |||
| 35e74a90b3 | |||
| 8e70c5565f | |||
| 23be028cd3 | |||
| af3e483382 | |||
| f5ad468db5 | |||
| cc6a0942fb | |||
| 7cf383d9c9 | |||
| f24a9085b3 | |||
| 4a7c9082ba | |||
| af4701dab7 | |||
| 457358e77b | |||
| c6dd5475fa | |||
| 2ade2bf1fc | |||
| 14c9e2c504 | |||
| 4aafad4e8d | |||
| 1c686b53de | |||
| 9fd4f8131a | |||
| 60ef7a0557 | |||
| fd65c94298 | |||
| 8af5740946 | |||
| c47f06b8e7 | |||
| 15af19f442 | |||
| 981152aba2 | |||
| 78f122c850 | |||
| f3985498ce | |||
| 8aa073b5ad | |||
| 714558f3c1 | |||
| 153db25343 | |||
| df7fa4b973 |
7
.gitignore
vendored
7
.gitignore
vendored
@@ -19,6 +19,11 @@
|
||||
|
||||
# Go workspace file
|
||||
go.work
|
||||
go.work.sum
|
||||
|
||||
# input files
|
||||
input.txt
|
||||
test*.txt
|
||||
input.txt
|
||||
|
||||
# scaffold
|
||||
scaffold.sh
|
||||
@@ -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
10
day-1/makefile
Normal file
@@ -0,0 +1,10 @@
|
||||
TEST="test.txt"
|
||||
INPUT="input.txt"
|
||||
|
||||
test:
|
||||
go run . < $(TEST)
|
||||
|
||||
run:
|
||||
go run . < $(INPUT)
|
||||
|
||||
all: test
|
||||
@@ -1,5 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
INPUT="input.txt"
|
||||
|
||||
cat $INPUT | go run .
|
||||
53
day-10/check.go
Normal file
53
day-10/check.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
functions that check connections between pipes in each direction
|
||||
*/
|
||||
|
||||
func checkSouth(point point) point {
|
||||
if point.Y == len(pointsArray)-1 {
|
||||
return point
|
||||
}
|
||||
|
||||
target := pointsArray[point.Y+1].points[point.X]
|
||||
if point.S && target.N {
|
||||
return target
|
||||
}
|
||||
return point
|
||||
}
|
||||
|
||||
func checkEast(point point) point {
|
||||
if point.X == len(pointsArray[point.Y].points)-1 {
|
||||
return point
|
||||
}
|
||||
|
||||
target := pointsArray[point.Y].points[point.X+1]
|
||||
if point.E && target.W {
|
||||
return target
|
||||
}
|
||||
return point
|
||||
}
|
||||
|
||||
func checkNorth(point point) point {
|
||||
if point.Y == 0 {
|
||||
return point
|
||||
}
|
||||
|
||||
target := pointsArray[point.Y-1].points[point.X]
|
||||
if point.N && target.S {
|
||||
return target
|
||||
}
|
||||
return point
|
||||
}
|
||||
|
||||
func checkWest(point point) point {
|
||||
if point.X == 0 {
|
||||
return point
|
||||
}
|
||||
|
||||
target := pointsArray[point.Y].points[point.X-1]
|
||||
if point.W && target.E {
|
||||
return target
|
||||
}
|
||||
return point
|
||||
}
|
||||
3
day-10/go.mod
Normal file
3
day-10/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module github.com/onyx-and-iris/aoc2023/day-10
|
||||
|
||||
go 1.21.5
|
||||
10
day-10/makefile
Normal file
10
day-10/makefile
Normal file
@@ -0,0 +1,10 @@
|
||||
TEST="test.txt"
|
||||
INPUT="input.txt"
|
||||
|
||||
test:
|
||||
go run . < $(TEST)
|
||||
|
||||
run:
|
||||
go run . < $(INPUT)
|
||||
|
||||
all: test
|
||||
144
day-10/one.go
Normal file
144
day-10/one.go
Normal file
@@ -0,0 +1,144 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type coords struct {
|
||||
X int
|
||||
Y int
|
||||
}
|
||||
|
||||
type points struct {
|
||||
points []point
|
||||
}
|
||||
|
||||
type point struct {
|
||||
N bool
|
||||
S bool
|
||||
E bool
|
||||
W bool
|
||||
coords
|
||||
identifier rune
|
||||
}
|
||||
|
||||
var pointsArray = []points{}
|
||||
|
||||
type tracker struct {
|
||||
init point
|
||||
point point
|
||||
last []coords
|
||||
steps int
|
||||
}
|
||||
|
||||
// newTracker stores the starting point
|
||||
// it creates a queue for storing past 2 moves
|
||||
// returns a tracker struct for counting steps
|
||||
func newTracker() tracker {
|
||||
tracker := tracker{last: make([]coords, 0)}
|
||||
for _, each := range pointsArray {
|
||||
for _, point := range each.points {
|
||||
if point.identifier == 'S' {
|
||||
tracker.init = point
|
||||
tracker.point = tracker.init
|
||||
tracker.last = make([]coords, 1)
|
||||
tracker.last = append(tracker.last, tracker.point.coords)
|
||||
}
|
||||
}
|
||||
}
|
||||
return tracker
|
||||
}
|
||||
|
||||
func (t tracker) X() int {
|
||||
return t.point.X
|
||||
}
|
||||
|
||||
func (t tracker) Y() int {
|
||||
return t.point.Y
|
||||
}
|
||||
|
||||
// mapPoints builds the points array mapping connection types for each coordinate
|
||||
func mapPoints(lines []string) {
|
||||
pointsArray = make([]points, 0)
|
||||
|
||||
for i, line := range lines {
|
||||
var ps = make([]point, 0)
|
||||
for j, r := range line {
|
||||
switch r {
|
||||
case '|':
|
||||
ps = append(ps, point{N: true, S: true, E: false, W: false}) // N,S
|
||||
case '-':
|
||||
ps = append(ps, point{N: false, S: false, E: true, W: true}) // E, W
|
||||
case 'L':
|
||||
ps = append(ps, point{N: true, S: false, E: true, W: false}) // N, E
|
||||
case 'J':
|
||||
ps = append(ps, point{N: true, S: false, E: false, W: true}) // N, W
|
||||
case '7':
|
||||
ps = append(ps, point{N: false, S: true, E: false, W: true}) // S, W
|
||||
case 'F':
|
||||
ps = append(ps, point{N: false, S: true, E: true, W: false}) // S, E
|
||||
case '.':
|
||||
ps = append(ps, point{N: false, S: false, E: false, W: false}) // ground
|
||||
case 'S':
|
||||
ps = append(ps, point{N: true, S: true, E: true, W: true}) // start
|
||||
default:
|
||||
log.Debug("ground point")
|
||||
}
|
||||
ps[j].identifier = r
|
||||
ps[j].coords.Y = i
|
||||
ps[j].coords.X = j
|
||||
}
|
||||
pointsArray = append(pointsArray, points{points: ps})
|
||||
}
|
||||
}
|
||||
|
||||
// walk moves along the pipes storing points that mark loop locations
|
||||
// it also keeps a track of last two moves
|
||||
// returns the number of steps to traverse all pipes
|
||||
func walk(tracker tracker) int {
|
||||
for ; tracker.steps == 0 || !comparePoints(tracker.init, tracker.point); tracker.steps++ {
|
||||
next := checkSouth(tracker.point)
|
||||
if !inLastMoves(tracker.last, next.coords) {
|
||||
log.Debug("moving south from ", string(tracker.point.identifier), " to ", string(next.identifier))
|
||||
tracker.point = next
|
||||
loop = append(loop, pointsArray[tracker.Y()].points[tracker.X()].coords)
|
||||
tracker.last = append(tracker.last[1:], next.coords)
|
||||
continue
|
||||
}
|
||||
next = checkEast(tracker.point)
|
||||
if !inLastMoves(tracker.last, next.coords) {
|
||||
log.Debug("moving east from ", string(tracker.point.identifier), " to ", string(next.identifier))
|
||||
tracker.point = next
|
||||
loop = append(loop, pointsArray[tracker.Y()].points[tracker.X()].coords)
|
||||
tracker.last = append(tracker.last[1:], next.coords)
|
||||
continue
|
||||
}
|
||||
next = checkNorth(tracker.point)
|
||||
if !inLastMoves(tracker.last, next.coords) {
|
||||
log.Debug("moving north from ", string(tracker.point.identifier), " to ", string(next.identifier))
|
||||
tracker.point = next
|
||||
loop = append(loop, pointsArray[tracker.Y()].points[tracker.X()].coords)
|
||||
tracker.last = append(tracker.last[1:], next.coords)
|
||||
continue
|
||||
}
|
||||
next = checkWest(tracker.point)
|
||||
if !inLastMoves(tracker.last, next.coords) {
|
||||
log.Debug("moving west from ", string(tracker.point.identifier), " to ", string(next.identifier))
|
||||
tracker.point = next
|
||||
loop = append(loop, pointsArray[tracker.Y()].points[tracker.X()].coords)
|
||||
tracker.last = append(tracker.last[1:], next.coords)
|
||||
continue
|
||||
}
|
||||
tracker.steps++ // the very last move will be back at start
|
||||
}
|
||||
return tracker.steps
|
||||
}
|
||||
|
||||
// one returns the number of steps to reach the furthest point from the start
|
||||
func one(lines []string) int {
|
||||
mapPoints(lines)
|
||||
|
||||
tracker := newTracker()
|
||||
|
||||
return walk(tracker) / 2
|
||||
}
|
||||
21
day-10/solution.go
Normal file
21
day-10/solution.go
Normal 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)
|
||||
}
|
||||
40
day-10/two.go
Normal file
40
day-10/two.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func debugPrint() {
|
||||
for _, points := range pointsArray {
|
||||
for _, point := range points.points {
|
||||
if contains(loop, point.coords) {
|
||||
fmt.Printf("%c ", point.identifier)
|
||||
} else {
|
||||
fmt.Printf(". ")
|
||||
}
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
}
|
||||
|
||||
var loop []coords
|
||||
|
||||
// two returns the number of coords that sit inside the polygon
|
||||
func two(lines []string) int {
|
||||
if log.GetLevel() == log.DebugLevel {
|
||||
debugPrint()
|
||||
}
|
||||
|
||||
area := 0
|
||||
for i := 0; i < len(loop); i++ {
|
||||
next := loop[(i+1)%len(loop)]
|
||||
area += loop[i].X*next.Y - loop[i].Y*next.X
|
||||
}
|
||||
|
||||
area = int(math.Abs(float64(area))) / 2
|
||||
|
||||
return area - len(loop)/2 + 1
|
||||
}
|
||||
50
day-10/util.go
Normal file
50
day-10/util.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// inLastMoves checks if next coords exists in past moves
|
||||
func inLastMoves(last []coords, b coords) bool {
|
||||
for _, co := range last {
|
||||
if co.X == b.X && co.Y == b.Y {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// comparePoints returns true if coordinates for two points match
|
||||
func comparePoints(a, b point) bool {
|
||||
return a.X == b.X && a.Y == b.Y
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
7
day-11/go.mod
Normal file
7
day-11/go.mod
Normal file
@@ -0,0 +1,7 @@
|
||||
module github.com/onyx-and-iris/aoc2023/day-11
|
||||
|
||||
go 1.21.5
|
||||
|
||||
require github.com/sirupsen/logrus v1.9.3
|
||||
|
||||
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||
15
day-11/go.sum
Normal file
15
day-11/go.sum
Normal file
@@ -0,0 +1,15 @@
|
||||
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
10
day-11/makefile
Normal file
@@ -0,0 +1,10 @@
|
||||
TEST="test.txt"
|
||||
INPUT="input.txt"
|
||||
|
||||
test:
|
||||
go run . < $(TEST)
|
||||
|
||||
run:
|
||||
go run . < $(INPUT)
|
||||
|
||||
all: test
|
||||
73
day-11/one.go
Normal file
73
day-11/one.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var runes = [][]rune{}
|
||||
|
||||
type coords struct {
|
||||
X int
|
||||
Y int
|
||||
}
|
||||
|
||||
type galaxy struct {
|
||||
identifier int
|
||||
coords
|
||||
}
|
||||
|
||||
func newGalaxy(identifier, x, y int) galaxy {
|
||||
return galaxy{identifier: identifier, coords: coords{X: x, Y: y}}
|
||||
}
|
||||
|
||||
var galaxies = make([]galaxy, 0)
|
||||
|
||||
var empty = map[string][]int{
|
||||
"row": {},
|
||||
"col": {},
|
||||
}
|
||||
|
||||
// shortestRoute calculates horizontal and vertical distances between points
|
||||
// if empty row or columns exist then horz and vert are padded
|
||||
// returns the sum of both distances
|
||||
func shortestRoute(a, b coords, factor int) int {
|
||||
horz := int(math.Abs(float64(b.X - a.X)))
|
||||
vert := int(math.Abs(float64(b.Y - a.Y)))
|
||||
|
||||
for _, row := range empty["row"] {
|
||||
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 col >= a.X && col < b.X || col >= b.X && col < a.X {
|
||||
log.Debug("empty col, adding to horz")
|
||||
horz += (factor - 1)
|
||||
}
|
||||
}
|
||||
|
||||
return int(math.Abs(float64(horz + vert)))
|
||||
}
|
||||
|
||||
// one returns the sum of all shortest distances between galaxies
|
||||
func one(lines []string) int {
|
||||
parseInput(lines)
|
||||
|
||||
compared := [][]int{}
|
||||
sum := 0
|
||||
for i := range galaxies {
|
||||
for j := range galaxies {
|
||||
if i == j || inCompared(i, j, compared) {
|
||||
continue
|
||||
}
|
||||
|
||||
compared = append(compared, []int{galaxies[i].identifier, galaxies[j].identifier})
|
||||
sum += shortestRoute(galaxies[i].coords, galaxies[j].coords, 2)
|
||||
}
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
||||
21
day-11/solution.go
Normal file
21
day-11/solution.go
Normal 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)
|
||||
}
|
||||
19
day-11/two.go
Normal file
19
day-11/two.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
// two returns the sum of all shortest distances between galaxies if space expand by a factor of 1000000
|
||||
func two(lines []string) int {
|
||||
compared := [][]int{}
|
||||
sum := 0
|
||||
for i := range galaxies {
|
||||
for j := range galaxies {
|
||||
if i == j || inCompared(i, j, compared) {
|
||||
continue
|
||||
}
|
||||
|
||||
compared = append(compared, []int{galaxies[i].identifier, galaxies[j].identifier})
|
||||
sum += shortestRoute(galaxies[i].coords, galaxies[j].coords, 1000000)
|
||||
}
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
||||
82
day-11/util.go
Normal file
82
day-11/util.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// parseInput stores coordinates of galaxies
|
||||
// stores empty row and column indexes
|
||||
func parseInput(lines []string) {
|
||||
x := 0
|
||||
runes = make([][]rune, len(lines))
|
||||
for i, line := range lines {
|
||||
for j, r := range line {
|
||||
runes[i] = append(runes[i], r)
|
||||
if r == '#' {
|
||||
galaxies = append(galaxies, newGalaxy(x, j, i))
|
||||
x++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for i := range lines {
|
||||
if strings.Count(lines[i], "#") == 0 {
|
||||
empty["row"] = append(empty["row"], i)
|
||||
}
|
||||
}
|
||||
|
||||
f := func(i int) bool {
|
||||
for _, line := range lines {
|
||||
if line[i] == '#' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
for i := range lines[0] {
|
||||
if f(i) {
|
||||
empty["col"] = append(empty["col"], i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// inCompared returns true if both i, j coords are in compared
|
||||
func inCompared(i, j int, compared [][]int) bool {
|
||||
for _, comp := range compared {
|
||||
if contains(comp, i) && contains(comp, j) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
10
day-12/go.mod
Normal file
10
day-12/go.mod
Normal 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
17
day-12/go.sum
Normal 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
10
day-12/makefile
Normal 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
113
day-12/one.go
Normal 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
22
day-12/solution.go
Normal 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
36
day-12/two.go
Normal 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
71
day-12/util.go
Normal 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
3
day-13/go.mod
Normal 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
34
day-13/image.go
Normal 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
10
day-13/makefile
Normal 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
60
day-13/one.go
Normal 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
19
day-13/one_test.go
Normal 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
21
day-13/solution.go
Normal 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
45
day-13/two.go
Normal 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
57
day-13/util.go
Normal 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
3
day-14/go.mod
Normal 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
35
day-14/image.go
Normal 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
10
day-14/makefile
Normal 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
58
day-14/one.go
Normal 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
21
day-14/solution.go
Normal 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
47
day-14/two.go
Normal 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
31
day-14/util.go
Normal 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
16
day-15/go.mod
Normal 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
19
day-15/go.sum
Normal 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
30
day-15/hasher.go
Normal 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
29
day-15/hasher_test.go
Normal 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
10
day-15/makefile
Normal 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
18
day-15/one.go
Normal 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
22
day-15/solution.go
Normal 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
46
day-15/two.go
Normal 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
35
day-15/util.go
Normal 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
30
day-16/debug.go
Normal 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
3
day-16/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module github.com/onyx-and-iris/aoc2023/day-16
|
||||
|
||||
go 1.21.5
|
||||
10
day-16/makefile
Normal file
10
day-16/makefile
Normal 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
64
day-16/mover.go
Normal 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
125
day-16/one.go
Normal 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
21
day-16/solution.go
Normal 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
77
day-16/two.go
Normal 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
45
day-16/util.go
Normal 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
3
day-17/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module github.com/onyx-and-iris/aoc2023/day-17
|
||||
|
||||
go 1.21.5
|
||||
10
day-17/makefile
Normal file
10
day-17/makefile
Normal 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
32
day-17/node.go
Normal 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
114
day-17/one.go
Normal 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
49
day-17/pqueue.go
Normal 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
27
day-17/pqueue_test.go
Normal 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
21
day-17/solution.go
Normal 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
14
day-17/two.go
Normal 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
26
day-17/two_test.go
Normal 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
38
day-17/util.go
Normal 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
3
day-18/go.mod
Normal 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
37
day-18/imager.go
Normal 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
10
day-18/makefile
Normal 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
29
day-18/one.go
Normal 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
22
day-18/solution.go
Normal 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
26
day-18/two.go
Normal 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
73
day-18/util.go
Normal 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
|
||||
}
|
||||
@@ -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
10
day-2/makefile
Normal file
@@ -0,0 +1,10 @@
|
||||
TEST="test.txt"
|
||||
INPUT="input.txt"
|
||||
|
||||
test:
|
||||
go run . < $(TEST)
|
||||
|
||||
run:
|
||||
go run . < $(INPUT)
|
||||
|
||||
all: test
|
||||
@@ -1,5 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
INPUT="input.txt"
|
||||
|
||||
cat $INPUT | go run .
|
||||
@@ -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
10
day-3/makefile
Normal file
@@ -0,0 +1,10 @@
|
||||
TEST="test.txt"
|
||||
INPUT="input.txt"
|
||||
|
||||
test:
|
||||
go run . < $(TEST)
|
||||
|
||||
run:
|
||||
go run . < $(INPUT)
|
||||
|
||||
all: test
|
||||
@@ -34,4 +34,71 @@ func TestSymbolToBool(t *testing.T) {
|
||||
|
||||
func TestCheckDigit(t *testing.T) {
|
||||
//t.Skip("skipping test")
|
||||
symbols := [][]bool{
|
||||
{false, false, false, false, false, false, false, false, false, false},
|
||||
{false, false, false, true, false, false, false, false, false, false},
|
||||
{false, false, false, false, false, false, false, false, false, false},
|
||||
{false, false, false, false, false, false, true, false, false, false},
|
||||
}
|
||||
|
||||
d1 := func() digit {
|
||||
var d digit = digit{row: 0, col: 2, raw: 0, pass: false}
|
||||
i := d.col - 1
|
||||
for ; i < len(symbols[d.row]) && i <= d.col+1; i += 1 {
|
||||
if i < 0 {
|
||||
continue
|
||||
}
|
||||
if d.row != 0 {
|
||||
if symbols[d.row-1][i] {
|
||||
d.pass = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if symbols[d.row][i] {
|
||||
d.pass = true
|
||||
break
|
||||
}
|
||||
if d.row != len(symbols)-1 {
|
||||
if symbols[d.row+1][i] {
|
||||
d.pass = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return d
|
||||
}()
|
||||
d2 := func() digit {
|
||||
var d digit = digit{row: 2, col: 9, raw: 0, pass: false}
|
||||
i := d.col - 1
|
||||
for ; i < len(symbols[d.row]) && i <= d.col+1; i += 1 {
|
||||
if i < 0 {
|
||||
continue
|
||||
}
|
||||
if d.row != 0 {
|
||||
if symbols[d.row-1][i] {
|
||||
d.pass = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if symbols[d.row][i] {
|
||||
d.pass = true
|
||||
break
|
||||
}
|
||||
if d.row != len(symbols)-1 {
|
||||
if symbols[d.row+1][i] {
|
||||
d.pass = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return d
|
||||
}()
|
||||
|
||||
t.Run("Should pass the check", func(t *testing.T) {
|
||||
assert.Equal(t, true, d1.pass)
|
||||
})
|
||||
|
||||
t.Run("Should fail the check", func(t *testing.T) {
|
||||
assert.Equal(t, false, d2.pass)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
INPUT="input.txt"
|
||||
|
||||
cat $INPUT | go run .
|
||||
10
day-3/two.go
10
day-3/two.go
@@ -12,12 +12,10 @@ func getGearRatio(row, col int) (int, error) {
|
||||
continue
|
||||
}
|
||||
for _, enginePart := range engineParts[i] {
|
||||
for _, part := range enginePart.digits {
|
||||
if part.col == col-1 || part.col == col || part.col == col+1 {
|
||||
if anyTrue(enginePart.digits) {
|
||||
partsConsidered = append(partsConsidered, enginePart)
|
||||
break
|
||||
}
|
||||
for _, digit := range enginePart.digits {
|
||||
if digit.pass && digit.col == col-1 || digit.col == col || digit.col == col+1 {
|
||||
partsConsidered = append(partsConsidered, enginePart)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
5
day-4/go.mod
Normal file
5
day-4/go.mod
Normal file
@@ -0,0 +1,5 @@
|
||||
module github.com/onyx-and-iris/aoc2023/day-4
|
||||
|
||||
go 1.21.5
|
||||
|
||||
require github.com/go-playground/assert v1.2.1
|
||||
2
day-4/go.sum
Normal file
2
day-4/go.sum
Normal file
@@ -0,0 +1,2 @@
|
||||
github.com/go-playground/assert v1.2.1 h1:ad06XqC+TOv0nJWnbULSlh3ehp5uLuQEojZY5Tq8RgI=
|
||||
github.com/go-playground/assert v1.2.1/go.mod h1:Lgy+k19nOB/wQG/fVSQ7rra5qYugmytMQqvQ2dgjWn8=
|
||||
10
day-4/makefile
Normal file
10
day-4/makefile
Normal file
@@ -0,0 +1,10 @@
|
||||
TEST="test.txt"
|
||||
INPUT="input.txt"
|
||||
|
||||
test:
|
||||
go run . < $(TEST)
|
||||
|
||||
run:
|
||||
go run . < $(INPUT)
|
||||
|
||||
all: test
|
||||
43
day-4/one.go
Normal file
43
day-4/one.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// Card represents a single card.
|
||||
// it tracks its number of matches and occurrences
|
||||
type Card struct {
|
||||
matches int
|
||||
occurrences int
|
||||
}
|
||||
|
||||
var cards = []Card{}
|
||||
|
||||
// one computes points based on matching numbers
|
||||
func one(lines []string) (int, error) {
|
||||
f := func(c rune) bool {
|
||||
return !unicode.IsDigit(c)
|
||||
}
|
||||
|
||||
sum := 0
|
||||
cards = make([]Card, len(lines))
|
||||
for x, line := range lines {
|
||||
winning, mynums := func() (string, string) {
|
||||
y := strings.Split(line, ":")
|
||||
z := strings.Split(y[1], "|")
|
||||
return z[0], z[1]
|
||||
}()
|
||||
|
||||
m, err := compare(strings.FieldsFunc(winning, f), strings.FieldsFunc(mynums, f))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
cards[x].matches = m
|
||||
if cards[x].matches > 0 {
|
||||
sum += (pow(2, cards[x].matches-1))
|
||||
}
|
||||
}
|
||||
|
||||
return sum, nil
|
||||
}
|
||||
33
day-4/one_test.go
Normal file
33
day-4/one_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"unicode"
|
||||
|
||||
"github.com/go-playground/assert"
|
||||
)
|
||||
|
||||
func TestCompare(t *testing.T) {
|
||||
//t.Skip("skipping test")
|
||||
|
||||
winning1 := "41 48 83 86 17"
|
||||
mynums1 := "83 86 6 31 17 9 48 53"
|
||||
|
||||
winning2 := "13 32 20 16 61"
|
||||
mynums2 := "61 30 68 82 17 32 24 19"
|
||||
|
||||
f := func(c rune) bool {
|
||||
return !unicode.IsDigit(c)
|
||||
}
|
||||
res1, _ := compare(strings.FieldsFunc(winning1, f), strings.FieldsFunc(mynums1, f))
|
||||
res2, _ := compare(strings.FieldsFunc(winning2, f), strings.FieldsFunc(mynums2, f))
|
||||
|
||||
t.Run("Should return 4 matches", func(t *testing.T) {
|
||||
assert.Equal(t, 4, res1)
|
||||
})
|
||||
|
||||
t.Run("Should return 2 matches", func(t *testing.T) {
|
||||
assert.Equal(t, 2, res2)
|
||||
})
|
||||
}
|
||||
22
day-4/solution.go
Normal file
22
day-4/solution.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
lines := readlines()
|
||||
|
||||
ans, err := one(lines)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("solution one: %d\n", ans)
|
||||
|
||||
ans, err = two(lines)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("solution two: %d\n", ans)
|
||||
}
|
||||
18
day-4/two.go
Normal file
18
day-4/two.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
// two returns the total number of occurrences for all cards
|
||||
func two(lines []string) (int, error) {
|
||||
var sum = 0
|
||||
|
||||
for i := range cards {
|
||||
cards[i].occurrences++
|
||||
|
||||
for j := i + 1; j <= i+cards[i].matches; j++ {
|
||||
cards[j].occurrences += cards[i].occurrences
|
||||
}
|
||||
|
||||
sum += cards[i].occurrences
|
||||
}
|
||||
|
||||
return sum, nil
|
||||
}
|
||||
51
day-4/util.go
Normal file
51
day-4/util.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
)
|
||||
|
||||
// readlines reads lines from stdin.
|
||||
// Then 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
|
||||
}
|
||||
|
||||
// compare returns the number of matching elements
|
||||
func compare(a []string, b []string) (int, error) {
|
||||
n := 0
|
||||
for _, elem := range a {
|
||||
if contains(b, elem) {
|
||||
n += 1
|
||||
}
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// pow returns the value of x to the n
|
||||
func pow(x, n int) int {
|
||||
return int(math.Pow(float64(x), float64(n)))
|
||||
}
|
||||
22
day-5/data.go
Normal file
22
day-5/data.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
type data struct {
|
||||
dest int
|
||||
source int
|
||||
offset int
|
||||
}
|
||||
|
||||
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{}
|
||||
7
day-5/go.mod
Normal file
7
day-5/go.mod
Normal file
@@ -0,0 +1,7 @@
|
||||
module github.com/onyx-and-iris/aoc2023/day-5
|
||||
|
||||
go 1.21.5
|
||||
|
||||
require github.com/sirupsen/logrus v1.9.3
|
||||
|
||||
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||
15
day-5/go.sum
Normal file
15
day-5/go.sum
Normal file
@@ -0,0 +1,15 @@
|
||||
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-5/makefile
Normal file
10
day-5/makefile
Normal file
@@ -0,0 +1,10 @@
|
||||
TEST="test.txt"
|
||||
INPUT="input.txt"
|
||||
|
||||
test:
|
||||
go run . < $(TEST)
|
||||
|
||||
run:
|
||||
go run . < $(INPUT)
|
||||
|
||||
all: test
|
||||
41
day-5/one.go
Normal file
41
day-5/one.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
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 := 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 next(i+1, dest)
|
||||
}
|
||||
|
||||
// one returns the lowest location for any seed in seeds
|
||||
func one(lines []string) (int, error) {
|
||||
parseLines(lines)
|
||||
|
||||
lowest := math.MaxInt
|
||||
for _, seed := range seeds {
|
||||
location := next(0, seed)
|
||||
if location < lowest {
|
||||
lowest = location
|
||||
}
|
||||
}
|
||||
|
||||
return lowest, nil
|
||||
}
|
||||
51
day-5/queue.go
Normal file
51
day-5/queue.go
Normal 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
|
||||
}
|
||||
24
day-5/solution.go
Normal file
24
day-5/solution.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func init() {
|
||||
log.SetLevel(log.InfoLevel)
|
||||
}
|
||||
|
||||
func main() {
|
||||
lines := readlines()
|
||||
|
||||
ans, err := one(lines)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("solution one: %d\n", ans)
|
||||
|
||||
ans = two(lines)
|
||||
fmt.Printf("solution two: %d\n", ans)
|
||||
}
|
||||
89
day-5/two.go
Normal file
89
day-5/two.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
var mu sync.Mutex
|
||||
|
||||
const UNLIMITED = -1
|
||||
|
||||
// bound represents the lower and upper limits of a single range
|
||||
type bound struct {
|
||||
lower, upper int
|
||||
}
|
||||
|
||||
// newBound returns a bound type
|
||||
// it defines an open ended interval
|
||||
func newBound(lower, upper int) bound {
|
||||
return bound{lower: lower, upper: upper - 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
|
||||
}
|
||||
|
||||
q := newQueue(UNLIMITED, in)
|
||||
in = make([]bound, 0)
|
||||
for !q.isEmpty() {
|
||||
r := q.dequeue()
|
||||
|
||||
hasOverlap := func() bool {
|
||||
for _, data := range dataMap[identifiers[i]] {
|
||||
start := max(r.lower, data.source)
|
||||
end := min(r.upper, data.source+data.offset)
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
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()
|
||||
|
||||
lowest := math.MaxInt
|
||||
for _, r := range seedRanges {
|
||||
if r.lower < lowest {
|
||||
lowest = r.lower
|
||||
}
|
||||
}
|
||||
|
||||
return lowest
|
||||
}
|
||||
68
day-5/util.go
Normal file
68
day-5/util.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// readlines reads lines from stdin.
|
||||
// Then 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
|
||||
}
|
||||
|
||||
var r = regexp.MustCompile(`([\w-]+) map[:]`)
|
||||
|
||||
// parseLines parses input to a data map
|
||||
func parseLines(lines []string) {
|
||||
f := func(c rune) bool {
|
||||
return !unicode.IsDigit(c)
|
||||
}
|
||||
|
||||
seeds = convertToInts(strings.FieldsFunc(lines[0], f))
|
||||
|
||||
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++ {
|
||||
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
|
||||
func convertToInts(data []string) []int {
|
||||
nums := []int{}
|
||||
for _, elem := range data {
|
||||
n, _ := strconv.Atoi(elem)
|
||||
nums = append(nums, n)
|
||||
}
|
||||
return nums
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
3
day-6/go.mod
Normal file
3
day-6/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module github.com/onyx-and-iris/aoc2023/day-6
|
||||
|
||||
go 1.21.5
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user