Compare commits

..

3 Commits

Author SHA1 Message Date
xuu
0204b4841b Merge pull request 'chore: add day 11' (#9) from day10 into main
All checks were successful
Go Bump / bump (push) Successful in 7s
Go Test / build (push) Successful in 28s
Reviewed-on: xuu/advent-of-code-2023#9
2023-12-12 13:45:14 -07:00
xuu
4159024e6f Merge branch 'main' into day10
All checks were successful
Go Test / build (pull_request) Successful in 32s
2023-12-12 13:45:09 -07:00
xuu
596f3e7533
chore: add day 11
All checks were successful
Go Test / build (pull_request) Successful in 35s
2023-12-12 13:44:28 -07:00
10 changed files with 592 additions and 21 deletions

View File

@ -113,8 +113,7 @@ func (m *Path) readLine(text string) {
} }
func (m *Path) buildPath() int { func (m *Path) buildPath() int {
m.start() m.start()
for m.next() { for m.next() {}
}
return (len(m.p) + 1) / 2 return (len(m.p) + 1) / 2
} }
func (m *Path) start() { func (m *Path) start() {
@ -178,23 +177,15 @@ func (m *Path) next() bool {
last := n.whence last := n.whence
next := m.head.left.whence next := m.head.left.whence
switch (last<<4)|next { switch last<<4|next {
case 0x11, 0x22: m.n.value = '|' // UP UP, DN DN case UP<<4|UP, DN<<4|DN: m.head.value = '|' // UP UP, DN DN
case LF<<4|LF, RT<<4|RT: m.head.value = '-' // LF LF, RT RT
case 0x13: m.head.value = 'J' // UP LF case UP<<4|RT, LF<<4|DN: m.head.value = 'J' // UP RT, LT DN
case 0x14: m.head.value = 'L' // UP RT case UP<<4|LF, RT<<4|DN: m.head.value = 'L' // UP RT, RT DN
case 0x23: m.head.value = '7' // DN LF
case 0x24: m.head.value = 'F' // DN RT
case 0x33, 0x44: m.head.value = '-' // LF LF, RT RT
case 0x31: m.head.value = '7' // LF UP
case 0x32: m.head.value = 'J' // LF DN
case 0x41: m.head.value = 'F' // RT UP
case 0x42: m.head.value = 'L' // RT DN
case DN<<4|RT, LF<<4|UP: m.head.value = '7' // DN LF, LF UP
case RT<<4|UP, DN<<4|LF: m.head.value = 'F' // DN LF, RT UP
} }
return false return false
@ -210,7 +201,7 @@ const (
ST int8 = iota ST int8 = iota
UP UP
DN DN
LF LF
RT RT
) )
@ -255,7 +246,6 @@ func (m *Path) peek(d int8) *node {
if any(r, 'J', '|', 'L', 'S') { if any(r, 'J', '|', 'L', 'S') {
return &node{value: r, whence: UP, pos: p} return &node{value: r, whence: UP, pos: p}
} }
case LF: case LF:
x, y := toXY(m.n.pos, m.w) x, y := toXY(m.n.pos, m.w)
if x == 0 { if x == 0 {
@ -267,7 +257,6 @@ func (m *Path) peek(d int8) *node {
if any(r, 'F', '-', 'L', 'S') { if any(r, 'F', '-', 'L', 'S') {
return &node{value: r, whence: RT, pos: p} return &node{value: r, whence: RT, pos: p}
} }
case RT: case RT:
x, y := toXY(m.n.pos, m.w) x, y := toXY(m.n.pos, m.w)
if x == m.w { if x == m.w {
@ -278,7 +267,6 @@ func (m *Path) peek(d int8) *node {
if any(r, '7', '-', 'J', 'S') { if any(r, '7', '-', 'J', 'S') {
return &node{value: r, whence: LF, pos: p} return &node{value: r, whence: LF, pos: p}
} }
} }
return nil return nil
} }

10
day11/example.txt Normal file
View File

@ -0,0 +1,10 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....

140
day11/input.txt Normal file
View File

@ -0,0 +1,140 @@
..............................#.........................#.......................#..............................#.........................#..
....#.....#...................................#.............................................................................................
.....................................#..................................#...................................................................
...................................................#....................................................................#...................
...............................................................#..................#...............................#.........................
...............#.......................................#...................................#.....#.......#.........................#........
............................................................................................................................................
............................................................................#...............................................................
.......#.......................#....................#.......................................................................................
........................#..................#....................................................................#...........................
..#.......................................................#.........#....................#....................................#.............
..............#...........................................................#..........................................................#......
......................................#...........................................#..........................#..............................
.....................................................#.........#..........................................................................#.
#.......#....................................#.................................................#............................................
..............................#.......................................#..............#....................#..........#......................
....#...................................#...................................................................................................
....................................................................................................#.......................................
.....................#...........#...............................................#..................................................#.......
...............................................#.........#..................................................#..................#............
..........................................................................#...............#........................#........................
.................#.......................................................................................................#..................
......................................................................................#..................#..............................#...
.............#...................................................................................#..........................................
#...........................#..........#....................................................................................................
.........#........................................................................#...............................#.........................
.....................#.........................#.....................................................#....................#......#..........
.....................................................................#......................................................................
................................#.....................................................................................................#.....
...............#..........................................#.................#................#..............................................
....................................#...........................#...........................................#...............................
.......#...................#................#.......................................#..............................#......................#.
......................#.................................................#................................................#..................
....................................................#......................................#............#...................................
.............#........................#..........................................................#.............#............................
...................#..............................................................#.........................................................
.........................................................#..................#..........#....................................................
........................#.....................#..............................................#.........................................#....
.................................................................#.........................................#...................#............
...................................................#........#.....................................#......................#..................
.........#....................#.......#................................................................#............#.......................
#.............................................................................#.....................................................#.......
..................#.......#.................................................................................................................
.............#........................................................#........................#................#..........#..............#.
.....................................................#......................................................................................
.......#...............#.................#......................#................#..........................................................
...................................#.....................................................#............................#.....................
............................................................................................................................................
...............................#........................................#.........................#.........................................
.....#.........#...............................#...................#.....................................................................#..
............................................................................................................................................
..........#......................................................................#..........................................................
.........................................#.............................................#......#...................#...................#.....
........................#.....#........................................................................#....................................
...................................................#.....#...................#..............................................................
....................................................................#...............#....................................#........#.........
...........................#..............................................................#.................................................
...#........................................#.........#.......................................................#..............#..............
..........#....................................................#................#...........................................................
..................................#..................................................................................#...............#......
.................#..........................................................................................................................
............................................................................................................................................
............................#.............#................................#.........................#.....................#................
.........#.........................................#............#.........................#............................................#....
....#................................................................#.........................#..............#.............................
...................#......................................#.................................................................................
..............................#......#..............................................#..............#................#.......................
.............................................................................#..............................................................
.......#.........................................................................................................................#..........
..............#............#....................#...................#............#.......#.................................#................
................................#......................#................................................#.................................#.
............................................................................................................................................
.#......................#......................................#...............................................#............................
........................................................................#............................#......................................
..................................................#..........................#..........................................#.......#...........
.........................................#..........................................#....................#..............................#...
......#...............#.................................#...................................................................................
....................................#.......................................................................................................
...............................................................#................#........#.........................#..........#.............
..#........#.....#..........#...................#...................................................#.......................................
.......................................#...........................#...........................................#............................
.............................................................................#..............................................................
.......................#..................................#............#...............#.......#.....................................#......
............................................................................................................................................
............#.....#................................#...........#...................................................#............#...........
...........................................#..............................................#.................................................
..................................#..............................................#.....................#....................................
..................................................................#.........................................................................
...#..........................#............................................#................................................................
.......................................................#..........................................#........#........#.......................
.......................................#.................................................................................#......#...........
.................................................#..........#.........#.......................#.............................................
............................................................................................................................................
....#...........#........................................................................................#.......#...........#..............
......................#...........#.......#....................#..............#.....#.......................................................
.........#...............................................................................#..................................................
..................................................#.........................................................................................
...................#......................................#...........................................#...........................#......#..
.............#..........#.....................#....................#........................................................................
...............................................................................#.....#.....#.................#......#.......................
...#.........................#...................................................................#........................#.................
............................................................................................................................................
.......#.........#..........................................#...............................................................................
.................................#..............................................................................................#...........
........................................................................#..............#.......#..................#........................#
.#...................................#......#......#........................................................................................
............................................................................................................................................
.............#................#............................................................#..............#.................................
......#.....................................................#.......................#................#...............................#......
...................#..............................................#.......................................................#.................
....................................#.....................................#.................................................................
........................................................................................................#.......#...........................
....#.....................................#...............#...................#...............#.........................................#...
.....................................................................................#......................................................
....................................................#...........#......................................................#............#.......
........................#...................................................................................#...............................
............................................................................#.....#.........#......................#........................
...#.....#.....#.......................#....................................................................................................
............................................................................................................................................
......................................................................#.....................................................................
......................................................................................#............#..........................#.............
.....#.............#.........................................................#................#.............................................
..............................#..........#............#.....#...........................................................#.............#.....
........................................................................#................#..................................................
..................................................................................#.........................................................
......................#.............#....................#........................................................#.........................
.................#................................#.............#.............#.............................................................
............................................................................................................................................
...#..........................#.............#....................................................#.......#...............#.........#........
........#...........#.................#..................................................................................................#..
..............#.................................#.....#..........................................................#..........................
...........................................................#......#.........................................................................
............................#..................................................#.....................................#......................
...................................................#.......................................#................................................
.#................#................#...............................................#...........................................#.....#......
.............#....................................................................................................#.........................
.........................#...........................................#.....#......................#..........#..............................
........................................#...................#...........................................#...................................
...#......#............................................................................#....................................................
...............................................#.......................................................................#....................

214
day11/main.go Normal file
View File

@ -0,0 +1,214 @@
package main
import (
"bufio"
_ "embed"
"fmt"
"sort"
"strings"
aoc "go.sour.is/advent-of-code-2023"
)
// var log = aoc.Log
func main() { aoc.MustResult(aoc.Runner(run)) }
type result struct {
valuePT1 int
valuePT2 int
}
func (r result) String() string { return fmt.Sprintf("%#v", r) }
func run(scan *bufio.Scanner) (*result, error) {
m := NewMap()
for scan.Scan() {
text := scan.Text()
m.readLine(text)
}
// solution9 := m.expand(9).sumPaths()
// fmt.Println(solution9)
// solution99 := m.expand(99).sumPaths()
// fmt.Println(solution99)
return &result{
valuePT1: m.expand(1).sumPaths(),
valuePT2: m.expand(999_999).sumPaths(),
}, nil
}
type Map struct {
rows int
cols int
emptyRows map[int]bool
emptyCols map[int]bool
*aoc.List[rune]
}
func NewMap() *Map {
return &Map{
emptyRows: make(map[int]bool),
emptyCols: make(map[int]bool),
List: aoc.NewList[rune](nil),
}
}
func (m *Map) String() string {
buf := &strings.Builder{}
fmt.Fprintf(buf, "Map size %d x %d\n", m.rows, m.cols)
fmt.Fprintln(buf, "empty rows:", all(m.emptyRows))
fmt.Fprintln(buf, "empty cols:", all(m.emptyCols))
n := m.Head()
for n != nil {
fmt.Fprintln(buf, toXY(n.Position(), m.cols), n.String())
n = n.Next()
}
for row:=0; row<m.rows; row++ {
for col:=0; col<m.cols; col++ {
if n := m.getRC(row, col); n != nil {
buf.WriteRune('#')
} else {
buf.WriteRune('.')
}
}
buf.WriteRune('\n')
}
return buf.String()
}
func (m *Map) readLine(text string) {
if m.cols == 0 {
m.cols = len(text)
}
emptyRow, ok := m.emptyRows[m.rows]
if !ok {
emptyRow = true
}
row := []rune(text)
for col, r := range row {
emptyCol, ok := m.emptyCols[col]
if !ok {
emptyCol = true
}
if r == '#' {
m.Add(r, fromXY(col, m.rows, m.cols))
emptyCol = false
emptyRow = false
}
m.emptyRows[m.rows] = emptyRow
m.emptyCols[col] = emptyCol
}
m.rows++
}
func (m *Map) getRC(row, col int) *aoc.Node[rune] {
return m.List.Get(fromXY(col, row, m.cols))
}
func (m *Map) expand(rate int) *Map {
newM := NewMap()
newM.rows = m.rows + rate*len(all(m.emptyRows))
newM.cols = m.cols + rate*len(all(m.emptyCols))
offsetC := 0
for col := 0; col < m.cols; col++ {
if empty, ok := m.emptyCols[col]; ok && empty {
for r := 0; r <= rate; r++ {
newM.emptyCols[offsetC+col+r] = true
}
offsetC += rate
continue
}
}
offsetR := 0
for row := 0; row < m.rows; row++ {
if empty, ok := m.emptyRows[row]; ok && empty {
for r := 0; r <= rate; r++ {
newM.emptyRows[offsetR+row+r] = true
}
offsetR += rate
continue
}
offsetC := 0
for col := 0; col < m.cols; col++ {
if empty, ok := m.emptyCols[col]; ok && empty {
offsetC += rate
continue
}
if n := m.getRC(row, col); n!= nil {
newM.Add('#', fromXY(offsetC+col, offsetR+row, newM.cols))
}
}
}
return newM
}
func(m *Map) sumPaths() int {
var positions []int
n := m.Head()
for n != nil {
positions = append(positions, n.Position())
n = n.Next()
}
var paths []int
for i := 0; i < len(positions); i++ {
p:= positions[i]
pXY := toXY(p, m.cols)
for j := i; j < len(positions); j++ {
c := positions[j]
if c == p { continue }
cXY := toXY(c, m.cols)
path := abs(cXY[0]-pXY[0])+abs(cXY[1]-pXY[1])
paths = append(paths, path)
}
}
return sum(paths...)
}
func all(m map[int]bool) []int {
lis := make([]int, 0, len(m))
for k, v := range m {
if v {
lis = append(lis, k)
}
}
sort.Ints(lis)
return lis
}
func fromXY(x, y, w int) int { return y*w + x }
func toXY(i, w int) []int { return []int{i % w, i / w} }
func abs(i int) int {
if i < 0 {
return -i
}
return i
}
func sum(arr ...int) int {
acc :=0
for _, a := range arr {
acc += a
}
return acc
}

41
day11/main_test.go Normal file
View File

@ -0,0 +1,41 @@
package main
import (
"bufio"
"bytes"
"testing"
_ "embed"
"github.com/matryer/is"
)
//go:embed example.txt
var example []byte
//go:embed input.txt
var input []byte
func TestExample(t *testing.T) {
is := is.New(t)
scan := bufio.NewScanner(bytes.NewReader(example))
result, err := run(scan)
is.NoErr(err)
t.Log(result)
is.Equal(result.valuePT1, 374)
is.Equal(result.valuePT2, 82000210)
}
func TestSolution(t *testing.T) {
is := is.New(t)
scan := bufio.NewScanner(bytes.NewReader(input))
result, err := run(scan)
is.NoErr(err)
t.Log(result)
is.Equal(result.valuePT1, 9627977)
is.Equal(result.valuePT2, 644248339497)
}

10
template/example.txt Normal file
View File

@ -0,0 +1,10 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....

0
template/input.txt Normal file
View File

30
template/main.go Normal file
View File

@ -0,0 +1,30 @@
package main
import (
"bufio"
_ "embed"
"fmt"
aoc "go.sour.is/advent-of-code-2023"
)
// var log = aoc.Log
func main() { aoc.MustResult(aoc.Runner(run)) }
type result struct {
valuePT1 int
valuePT2 int
}
func (r result) String() string { return fmt.Sprintf("%#v", r) }
func run(scan *bufio.Scanner) (*result, error) {
for scan.Scan() {
_ = scan.Text()
}
return &result{}, nil
}

41
template/main_test.go Normal file
View File

@ -0,0 +1,41 @@
package main
import (
"bufio"
"bytes"
"testing"
_ "embed"
"github.com/matryer/is"
)
//go:embed example.txt
var example []byte
//go:embed input.txt
var input []byte
func TestExample(t *testing.T) {
is := is.New(t)
scan := bufio.NewScanner(bytes.NewReader(example))
result, err := run(scan)
is.NoErr(err)
t.Log(result)
is.Equal(result.valuePT1, 0)
is.Equal(result.valuePT2, 0)
}
func TestSolution(t *testing.T) {
is := is.New(t)
scan := bufio.NewScanner(bytes.NewReader(input))
result, err := run(scan)
is.NoErr(err)
t.Log(result)
is.Equal(result.valuePT1, 0)
is.Equal(result.valuePT2, 0)
}

View File

@ -96,3 +96,100 @@ func ReadStringToInts(fields []string) []int {
} }
return arr return arr
} }
type Node[T any] struct {
value T
pos int
left *Node[T]
}
func (n *Node[T]) add(a *Node[T]) *Node[T] {
if a == nil {
return n
}
if n == nil {
return a
}
n.left = a
return a
}
func (n *Node[T]) Value() (value T, ok bool) {
if n == nil {
return
}
return n.value, true
}
func (n *Node[T]) Position() int {
if n == nil {
return -1
}
return n.pos
}
func (n *Node[T]) SetPosition(i int) {
if n == nil {
return
}
n.pos = i
}
func (n *Node[T]) Next() *Node[T] {
if n == nil {
return nil
}
return n.left
}
func (n *Node[T]) String() string {
if n == nil {
return "EOL"
}
return fmt.Sprintf("node %v", n.value)
}
type List[T any] struct {
head *Node[T]
n *Node[T]
p map[int]*Node[T]
}
func NewList[T any](a *Node[T]) *List[T] {
lis := &List[T]{
head: a,
n: a,
p: make(map[int]*Node[T]),
}
lis.add(a)
return lis
}
func (l *List[T]) Add(value T, pos int) {
a := &Node[T]{value: value, pos: pos}
l.add(a)
}
func (l *List[T]) add(a *Node[T]) {
if l.head == nil {
l.head = a
}
if a == nil {
return
}
l.n = l.n.add(a)
l.p[a.pos] = a
}
func (l *List[T]) Get(pos int) *Node[T] {
return l.p[pos]
}
func(l *List[T]) GetN(pos ...int) []*Node[T] {
lis := make([]*Node[T], len(pos))
for i, p := range pos {
lis[i] = l.p[p]
}
return lis
}
func (l *List[T]) Head() *Node[T] {
return l.head
}