Compare commits
25 Commits
e611d48ab7
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| b495e01ab4 | |||
| aa84e3974b | |||
|
aa0a2b7b7d
|
|||
|
77a0669f45
|
|||
|
c077b91c41
|
|||
| b3c36c1af1 | |||
|
6a17669107
|
|||
|
127257c494
|
|||
|
7b9a71f84c
|
|||
|
669e2cfb2c
|
|||
|
2274eca981
|
|||
|
b1e4c4d634
|
|||
|
4b3fc7eb73
|
|||
|
0d652660f1
|
|||
|
e046a6c06d
|
|||
|
04bbac8559
|
|||
|
3c9af95ec4
|
|||
|
50af2114d4
|
|||
|
f8fa61672f
|
|||
|
951c2c298a
|
|||
|
328a0f3eb3
|
|||
|
7d7402f054
|
|||
|
7585526634
|
|||
|
924c8d74f3
|
|||
|
22184ed9c7
|
6
README.md.sig
Normal file
6
README.md.sig
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
-----BEGIN SSH SIGNATURE-----
|
||||||
|
U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgZ+OuJYdd3UiUbyBuO1RlsQR20a
|
||||||
|
Qm5mKneuMxRjGo3zkAAAAEZmlsZQAAAAAAAAAGc2hhNTEyAAAAUwAAAAtzc2gtZWQyNTUx
|
||||||
|
OQAAAED8T4C6WILXYZ1KxqDIlVhlrAEjr1Vc+tn8ypcVM3bN7iOexVvuUuvm90nr8eEwKU
|
||||||
|
acrdDxmq2S+oysQbK+pMUE
|
||||||
|
-----END SSH SIGNATURE-----
|
||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
aoc "go.sour.is/advent-of-code"
|
aoc "go.sour.is/advent-of-code"
|
||||||
@@ -62,7 +63,7 @@ func run(scan *bufio.Scanner) (*result, error) {
|
|||||||
func(i int, v int, counts [3]int) [3]int {
|
func(i int, v int, counts [3]int) [3]int {
|
||||||
counts[v]++
|
counts[v]++
|
||||||
return counts
|
return counts
|
||||||
}, [3]int{}, maps.Values(memo)...)
|
}, [3]int{}, slices.Values(maps.Values(memo)))
|
||||||
|
|
||||||
// fmt.Println(i, counts)
|
// fmt.Println(i, counts)
|
||||||
i = 1_000_000_000 - (1_000_000_000-counts[0]-counts[1])%counts[2]
|
i = 1_000_000_000 - (1_000_000_000-counts[0]-counts[1])%counts[2]
|
||||||
@@ -32,14 +32,14 @@ func run(scan *bufio.Scanner) (*result, error) {
|
|||||||
r.valuePT1 = aoc.Reduce(func(i int, t string, sum int) int {
|
r.valuePT1 = aoc.Reduce(func(i int, t string, sum int) int {
|
||||||
sum += hash(t)
|
sum += hash(t)
|
||||||
return sum
|
return sum
|
||||||
}, 0, ops...)
|
}, 0, slices.Values(ops))
|
||||||
}
|
}
|
||||||
|
|
||||||
var boxen boxes
|
var boxen boxes
|
||||||
|
|
||||||
boxen = aoc.Reduce(func(i int, op string, b boxes) boxes {
|
boxen = aoc.Reduce(func(i int, op string, b boxes) boxes {
|
||||||
return b.Op(op)
|
return b.Op(op)
|
||||||
}, boxen, ops...)
|
}, boxen, slices.Values(ops))
|
||||||
|
|
||||||
r.valuePT2 = boxen.Sum()
|
r.valuePT2 = boxen.Sum()
|
||||||
|
|
||||||
@@ -29,10 +29,10 @@ func run(scan *bufio.Scanner) (*result, error) {
|
|||||||
log("start day 17")
|
log("start day 17")
|
||||||
|
|
||||||
result := result{}
|
result := result{}
|
||||||
result.valuePT1 = search(m, 1, 3)
|
result.valuePT1 = search(m, 1, 3, seenFn)
|
||||||
log("result from part 1 = ", result.valuePT1)
|
log("result from part 1 = ", result.valuePT1)
|
||||||
|
|
||||||
result.valuePT2 = search(m, 4, 10)
|
result.valuePT2 = search(m, 4, 10, nil)
|
||||||
log("result from part 2 = ", result.valuePT2)
|
log("result from part 2 = ", result.valuePT2)
|
||||||
|
|
||||||
return &result, nil
|
return &result, nil
|
||||||
@@ -90,6 +90,7 @@ type graph struct {
|
|||||||
m Map
|
m Map
|
||||||
target Point
|
target Point
|
||||||
reads int
|
reads int
|
||||||
|
seenFn func(a position) position
|
||||||
}
|
}
|
||||||
|
|
||||||
// Neighbors returns valid steps from given position. if at target returns none.
|
// Neighbors returns valid steps from given position. if at target returns none.
|
||||||
@@ -118,6 +119,7 @@ func (g *graph) Neighbors(current position) []position {
|
|||||||
if forward := current.step(); current.steps < g.max && g.m.Valid(forward.loc) {
|
if forward := current.step(); current.steps < g.max && g.m.Valid(forward.loc) {
|
||||||
nbs = append(nbs, forward)
|
nbs = append(nbs, forward)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nbs
|
return nbs
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,12 +131,13 @@ func (g *graph) Cost(a, b position) int16 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Potential calculates distance to target
|
// Potential calculates distance to target
|
||||||
func (g *graph) Potential(a, b position) int16 {
|
// func (g *graph) Potential(a position) int16 {
|
||||||
return aoc.ManhattanDistance(a.loc, b.loc)
|
// return aoc.ManhattanDistance(a.loc, g.target)
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (g *graph) Target(a position) bool {
|
// Target returns true when target reached. receives node and cost.
|
||||||
if a.loc == g.target && a.steps >= g.min {
|
func (g *graph) Target(a position, c int16) bool {
|
||||||
|
if a.loc == g.target && a.steps >= g.min && a.steps <= g.max {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
@@ -142,47 +145,79 @@ func (g *graph) Target(a position) bool {
|
|||||||
|
|
||||||
// Seen attempt at simplifying the seen to use horizontal/vertical and no steps.
|
// Seen attempt at simplifying the seen to use horizontal/vertical and no steps.
|
||||||
// It returns correct for part1 but not part 2..
|
// It returns correct for part1 but not part 2..
|
||||||
// func (g *graph) Seen(a position) position {
|
func (g *graph) Seen(a position) position {
|
||||||
// if a.direction == U {
|
if g.seenFn != nil {
|
||||||
// a.direction = D
|
return g.seenFn(a)
|
||||||
// }
|
}
|
||||||
// if a.direction == L {
|
return a
|
||||||
// a.direction = R
|
}
|
||||||
// }
|
|
||||||
// a.steps = 0
|
|
||||||
// return a
|
|
||||||
// }
|
|
||||||
|
|
||||||
func search(m Map, minSteps, maxSteps int8) int {
|
func seenFn(a position) position {
|
||||||
|
if a.direction == U {
|
||||||
|
a.direction = D
|
||||||
|
}
|
||||||
|
if a.direction == L {
|
||||||
|
a.direction = R
|
||||||
|
}
|
||||||
|
// a.steps = 0
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
func search(m Map, minSteps, maxSteps int8, seenFn func(position) position) int {
|
||||||
rows, cols := m.Size()
|
rows, cols := m.Size()
|
||||||
start := Point{}
|
start := Point{}
|
||||||
target := Point{rows - 1, cols - 1}
|
target := Point{rows - 1, cols - 1}
|
||||||
|
|
||||||
g := graph{min: minSteps, max: maxSteps, m: m, target: target}
|
g := graph{min: minSteps, max: maxSteps, m: m, target: target, seenFn: seenFn}
|
||||||
cost, path := aoc.FindPath[int16, position](&g, position{loc: start}, position{loc: target})
|
|
||||||
|
|
||||||
log("total map reads = ", g.reads)
|
cost, path, closed := aoc.FindPath[int16, position](&g, position{loc: start}, position{loc: target})
|
||||||
printGraph(m, path)
|
|
||||||
|
log("total map reads = ", g.reads, "cost = ", cost)
|
||||||
|
printGraph(m, path, closed, g.seenFn)
|
||||||
|
|
||||||
return int(cost)
|
return int(cost)
|
||||||
}
|
}
|
||||||
|
|
||||||
// printGraph with the path overlay
|
// printGraph with the path/cost overlay
|
||||||
func printGraph(m Map, path []position) {
|
func printGraph(m Map, path []position, closed map[position]int16, seenFn func(a position) position) {
|
||||||
pts := make(map[Point]position, len(path))
|
pts := make(map[Point]position, len(path))
|
||||||
for _, pt := range path {
|
for _, pt := range path {
|
||||||
pts[pt.loc] = pt
|
pts[pt.loc] = pt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clpt := make(map[position]position, len(closed))
|
||||||
|
for pt := range closed {
|
||||||
|
clpt[position{loc: pt.loc, steps: pt.steps}] = pt
|
||||||
|
}
|
||||||
|
|
||||||
for r, row := range m {
|
for r, row := range m {
|
||||||
|
// if r == 0 {
|
||||||
|
// for c := range row {
|
||||||
|
// if c == 0 {
|
||||||
|
// fmt.Print(" ")
|
||||||
|
// }
|
||||||
|
// fmt.Printf("% 5d", c)
|
||||||
|
// }
|
||||||
|
// fmt.Println("")
|
||||||
|
// }
|
||||||
for c := range row {
|
for c := range row {
|
||||||
if _, ok := pts[Point{int16(r), int16(c)}]; ok {
|
// if c == 0 {
|
||||||
|
// fmt.Printf("% 5d", r)
|
||||||
|
// }
|
||||||
|
|
||||||
|
if pt, ok := pts[Point{int16(r), int16(c)}]; ok {
|
||||||
|
if seenFn != nil {
|
||||||
|
pt = seenFn(pt)
|
||||||
|
}
|
||||||
|
_ = pt
|
||||||
|
// fmt.Printf("% 5d", closed[pt])
|
||||||
fmt.Print("*")
|
fmt.Print("*")
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Print(".")
|
// fmt.Print(" ....")
|
||||||
|
fmt.Print(" ")
|
||||||
}
|
}
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
}
|
}
|
||||||
@@ -28,14 +28,14 @@ func TestExample(t *testing.T) {
|
|||||||
is.Equal(result.valuePT2, 94)
|
is.Equal(result.valuePT2, 94)
|
||||||
}
|
}
|
||||||
|
|
||||||
// func TestSolution(t *testing.T) {
|
func TestSolution(t *testing.T) {
|
||||||
// is := is.New(t)
|
is := is.New(t)
|
||||||
// scan := bufio.NewScanner(bytes.NewReader(input))
|
scan := bufio.NewScanner(bytes.NewReader(input))
|
||||||
|
|
||||||
// result, err := run(scan)
|
result, err := run(scan)
|
||||||
// is.NoErr(err)
|
is.NoErr(err)
|
||||||
|
|
||||||
// t.Log(result)
|
t.Log(result)
|
||||||
// is.Equal(result.valuePT1, 843)
|
is.Equal(result.valuePT1, 843)
|
||||||
// is.Equal(result.valuePT2, 1017)
|
is.Equal(result.valuePT2, 1017)
|
||||||
// }
|
}
|
||||||
@@ -186,8 +186,8 @@ func solveWorkflow(parts []part, workflows map[string][]rule) int {
|
|||||||
|
|
||||||
func solveRanges(workflows map[string][]rule) uint {
|
func solveRanges(workflows map[string][]rule) uint {
|
||||||
|
|
||||||
pq := aoc.PriorityQueue(func(a, b queue) bool { return false })
|
pq := aoc.PriorityQueue(func(a, b *queue) bool { return false })
|
||||||
pq.Enqueue(queue{
|
pq.Insert(&queue{
|
||||||
"in",
|
"in",
|
||||||
block{
|
block{
|
||||||
ranger{1, 4000},
|
ranger{1, 4000},
|
||||||
@@ -200,9 +200,9 @@ func solveRanges(workflows map[string][]rule) uint {
|
|||||||
// var rejected []block
|
// var rejected []block
|
||||||
|
|
||||||
for !pq.IsEmpty() {
|
for !pq.IsEmpty() {
|
||||||
current, _ := pq.Dequeue()
|
current := pq.ExtractMin()
|
||||||
for _, rule := range workflows[current.name] {
|
for _, rule := range workflows[current.name] {
|
||||||
next := queue{name: rule.queue, block: current.block}
|
next := &queue{name: rule.queue, block: current.block}
|
||||||
|
|
||||||
switch rule.match {
|
switch rule.match {
|
||||||
case "x":
|
case "x":
|
||||||
@@ -223,14 +223,14 @@ func solveRanges(workflows map[string][]rule) uint {
|
|||||||
accepted = append(accepted, next.block)
|
accepted = append(accepted, next.block)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
pq.Enqueue(next)
|
pq.Insert(next)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var sum uint
|
var sum uint
|
||||||
for _, a := range accepted {
|
for _, a := range accepted {
|
||||||
sum += uint((a.x[1]-a.x[0]+1) * (a.m[1]-a.m[0]+1) * (a.a[1]-a.a[0]+1) * (a.s[1]-a.s[0]+1))
|
sum += uint((a.x[1] - a.x[0] + 1) * (a.m[1] - a.m[0] + 1) * (a.a[1] - a.a[0] + 1) * (a.s[1] - a.s[0] + 1))
|
||||||
}
|
}
|
||||||
|
|
||||||
return sum
|
return sum
|
||||||
11
aoc2023/day21/example.txt
Normal file
11
aoc2023/day21/example.txt
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
...........
|
||||||
|
.....###.#.
|
||||||
|
.###.##..#.
|
||||||
|
..#.#...#..
|
||||||
|
....#.#....
|
||||||
|
.##..S####.
|
||||||
|
.##..#...#.
|
||||||
|
.......##..
|
||||||
|
.##.#.####.
|
||||||
|
.##..##.##.
|
||||||
|
...........
|
||||||
131
aoc2023/day21/input.txt
Normal file
131
aoc2023/day21/input.txt
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
...................................................................................................................................
|
||||||
|
.#.......#..#...#.......#......#....#..#...#...#....#.#..........................................#...............#..........#......
|
||||||
|
.............##..................#......#.#....#.......................##..#........................#......#..........#............
|
||||||
|
......#..........................#....#.....#.............#................##....#.#.............#.........#.##...........#........
|
||||||
|
.#................#............................#...........................#..##.....#..................#..##..#.#.........##......
|
||||||
|
.....#.............#.....##.............##..........#...........#.............#.....#...........#.#.......#....#......#..........#.
|
||||||
|
........#....................#....#.#.#......##........#...........#...........#..........#...#..........#......................#..
|
||||||
|
.........#.#...#.......#........#........##...#...#..##........................................##...#.#........................#...
|
||||||
|
......#.....#..#..#.#..#..............#...#..#.....#...........#.....#........#.#...........#...........#.#...............#.....#..
|
||||||
|
...........#.......####..#.#..............##................#.#.#.....................##.....................#...#..............#..
|
||||||
|
......###...#......#................##...#...#....#...........#...#..............#.........#..#..................................#.
|
||||||
|
...............#..##..........#...........................##......#.##.........................#..#.#...........#.#...........#....
|
||||||
|
...##........#...#...#...##...##...........#..#...................#...................#.#..#.........#.......#..#...........##...#.
|
||||||
|
................#.....#..##......#.....#.....##............#..#..........##...........................#.#.......##.....#.....#.....
|
||||||
|
.#..#.......#.#...#..##.....#............................#......#...#....#................#.#..#.....#.......##................#...
|
||||||
|
...............#.....#........................#..........#.....#..............................................#..................#.
|
||||||
|
.....#......#..........................................#....................................#..#.......#.#..#......#.#....#..##....
|
||||||
|
............##............#.......###.......................#..#....#......................................#..#.........#..........
|
||||||
|
..#...............#.....#...#...#......................#...#.#..............................#.#..#.#.....#.#.#..#....#.....#...##..
|
||||||
|
.#.............................##.................#.......#.......##....#....................#.....#.#........#.....#........#.....
|
||||||
|
.#....##...................##.....#.#..............#.#....#....#..#....#......#..........#.....##............#....#.............##.
|
||||||
|
.........#....#.....#..#.........#....#.#..........#...#...#.......#....#..##.......................#.#..#........#..#....#..#.#...
|
||||||
|
..#..................#.......#..#.#..............##.......#........#..............#.........#..#....#......#..#..........#.........
|
||||||
|
........#.....#......#.....#......#...#.......#....#.......#.#..#................#....................##.............#.......#.....
|
||||||
|
..#.................#..............#...........................................#.....#..................#...#.#..................#.
|
||||||
|
.......#......#.....#...........................#.....#..................#.....................#.....#.#......#........#.#.......#.
|
||||||
|
..#.#.#...................#...#..#................#....#.......#......................#...............#.........##...#.#.....###...
|
||||||
|
.....#...#....##....#........####...........#.................#...#...........................................#..............##....
|
||||||
|
..##.#..#.#..........#.#.#..#................................#..#.............#..#......#...........................#.##...........
|
||||||
|
..............#.............................##........#..##........................#.............................#........#........
|
||||||
|
........#.........#........#..#........#..#........#...#.#........#...........#.........................#.#..........#...#......#..
|
||||||
|
.........##.....#........#.....................#..#.............#.....#........#....................#....#.#.#......#.....#........
|
||||||
|
......................#...................####..#.........#....#.........#...........#..................#..#.................#.....
|
||||||
|
..#.........#...#.....##................##.##....#.....#....#..............................#..#.............#..#...................
|
||||||
|
..........##......#..#..............#.#....#......#................#.....#....#..............#..................#..................
|
||||||
|
.#.#..####...#....................#.#.#.....#...#...................#....#.#.....#.#.......#.#...........#......#.#................
|
||||||
|
.......#.........#..#.#..............###.......#....#.........................#.......#.###...#............#....#.#..#....###.#....
|
||||||
|
........#....#...#......#.................#..........................#.........................#...........##.............#.#......
|
||||||
|
.#..............#.....................#......#.............##............#..#..........#...................#...#....#..............
|
||||||
|
......#..#..#.#...............#........###.......#.#...........#...........#.#.........#..#.....................#......#..#.#......
|
||||||
|
.....#...............#.........#....#..........#.......#.......#......#...#..##.....#.....#........#.#.......#.......#..#...#......
|
||||||
|
..#............#.....................#...#.......#....###...........#.....#.#.................#................#.........#..##.#...
|
||||||
|
..............................#.....#..................................#.....##...............#.......#.........#.....#....##......
|
||||||
|
..#...##...#...#..............#.................##..#......#.................#....#......#...#...#......#..........................
|
||||||
|
.........#....#..............#........#..#...............#....#...#.....#..#......#.........#.......#.#..#........#...........#....
|
||||||
|
........#..#....................#.........#...##.#.#..............#......#.#..........##...##....#......................#..........
|
||||||
|
.##..#....#....#.......##......#............#..#......#.....##..................#####..#...#......#.#...#..........#.#.....#..#....
|
||||||
|
..#...........#....................#.......#......#.........#...............#......#........##........#.................#.....#.#..
|
||||||
|
..#....#.#...............#.#.........................#.............####..............................##................###.........
|
||||||
|
.....#.......................................#....#...##...#.......#.......#.....#.##..##.................#.....................##.
|
||||||
|
.........#...........#..#..##.#..........#..#.........#..#.....#...................................##....#...............#..#.#....
|
||||||
|
.....#..#...........##............#........#..................#............#..........#..........#..#...........#..................
|
||||||
|
............................#.........#...............#..................#.#....#.#..#........#.....#...........#...........#......
|
||||||
|
................#...##.#....#..##..........#.#........##.#..#..#.....................#....#.................#.#...............#....
|
||||||
|
.#.....#..............#.#..........#..#........#...............#..#.#..##..#.#.#.........................#.................#.......
|
||||||
|
....#..............#................#.....#..#.......#..#.#.........#..........#..........#.....#..........##.....#............#...
|
||||||
|
..............##...........#.......#......#..................#.......#.....#.#...#...............#.#........#......#............#..
|
||||||
|
................#.....#....#........#....#.......#.#.##.....#...#..#.##...#................#.#.....#....#.............#............
|
||||||
|
...........#.#.##..####.....#....#.#...#.#...........#...#.......................#.#....#.....#..#................###..............
|
||||||
|
...................#....#..........#.#..##.....#..#..#......###............#..#...........#....#......#............................
|
||||||
|
...........................................#.......#..............#.##.......##.......#...#.....#.#..#.....#..###..................
|
||||||
|
............###........#.......#.#....#.....#....#..#........#.........##....#.....#..........#......#............#..#.#...........
|
||||||
|
............#..........#................##.#..............#.....#................#....................................#..#.........
|
||||||
|
...........#.....#.....................#...#.....#.......#..........#....#.....###.....##.#.##.#.....#......#......#.......#.......
|
||||||
|
.....................#..#.....#...##.....#..#...........#....#....##............##.................##............##................
|
||||||
|
.................................................................S.................................................................
|
||||||
|
........#.#........#..........#.....#......#........#.#............#.#.........##...#............#...#.....#...........#.....#.....
|
||||||
|
..................##.......#......#..#..................#..##......#.........#...#.........#............#....#...........##........
|
||||||
|
..................#...........#.##.......#..............#....................#.#....#...##...#.....#.......#..#.....##.............
|
||||||
|
...........#....#....##...#................................#.........#.................#....#.......................#...#..........
|
||||||
|
.............#.#.#.....#.#...#.......#.#...........#....#.#.........#........##......#.......#.......##...#..........#.##..........
|
||||||
|
...........##.#....#....#..#.....#...............#......................#...........#.....#...........#.#.......#..................
|
||||||
|
.#.......................#.#...##.#....#.#...........#.#....##.##......#........................##....#.#....#.....................
|
||||||
|
...#..................#.#...........##....###.................#....................##......#.....#............#.##....#.......##...
|
||||||
|
.#..................####...#.#..#.##.#...####...#......#....................#..........................#....#......................
|
||||||
|
.....#..........#.....#.#...............#.........##...#......#.#.....#...........#.....#............##......#.....#...............
|
||||||
|
......#........#....#..#............................###.#...##........#........#.................#..#........#...............##....
|
||||||
|
......................#.........................##....#...#..............#..#............#.#....#......##.#......#.........##......
|
||||||
|
..................#....#...#.#.......#..............#..#..#...#.......#...#.....#.....#...............#.#.#...............#........
|
||||||
|
...#...##......................##..##...........#......#.............#............#...........##.......#....#......................
|
||||||
|
.............................##...........##...#...#.....#....#.......#.#..#.....##...............#..#.#......#..............##....
|
||||||
|
...#..#.#...#.......#..................#............#....#.#.##.#.........#......#........#..#..........#....##...........#....#...
|
||||||
|
.....#.............................#......#.#.....#.##.#.................#.........#..#..#.#.#........................#.#.......#..
|
||||||
|
....#........#........#....#.#.#.....#.##............###.#..........#...##......#........##...#.........#.............#........#...
|
||||||
|
..#..............................#.............................#......#...................#...#......#.............#...............
|
||||||
|
.......#...#...............#.................##....................#....##.......#....##....##...#..................#..............
|
||||||
|
.........................#...#..#.....##.............#...##.......#.....#...#..............#.....#.#.#..#........................#.
|
||||||
|
......#.......##.#.............#....................#...#...........#...#..##....#.....#............#...........#........#.........
|
||||||
|
...#....#...#......#........................#.....#.........#......#........#.....#.#...........#.#..............#......#....#.....
|
||||||
|
........#..........#...................#...........##.#..#...#................#....##.............#................#....#..........
|
||||||
|
........#.......#....#.............#..........#...................#.#....#.#........#..#..............................#..........#.
|
||||||
|
............#........#....................#.#.........#......#.............#................#...#.............#....#...............
|
||||||
|
..#..............................#..#....##.....#......................##...#..#.........#......#......................#...#...#.#.
|
||||||
|
.##....#.............#...........##..................#...#..............#.......#................................#.........#.......
|
||||||
|
.....#......#.#.....##...........#.....#.......#..........##..#.............#.#.........#.#.#.............##........#......#..#....
|
||||||
|
.......#.......#.#.#..#......................#....#............##....#...........#.........................#......#................
|
||||||
|
.........##.........#..............#.#.#....#...#.......#.................................#..##..................##.###.##...#.....
|
||||||
|
......................#...#.#.......#..#.#..........#........#.......#.................#..................................#........
|
||||||
|
....##........#.........#.........................#................#......#...#.....#..................................#..#.#......
|
||||||
|
...##..............#..................#...#......#........###..#.......#..........#..............................#......#..........
|
||||||
|
.#.#.#....#..........#......#.#.........#.....#...............................#.....#.#............#....#........#.............#...
|
||||||
|
........#.........#.........#......................#..#......##.....#..#..........................#......##....#......##.##.....#..
|
||||||
|
.##...#.......#.#....#..#.#...................#.........#....#...........#.....#.........#............##......#............#.......
|
||||||
|
.##..#.........#..........#.#.................................#.#.#...............................#............#......##...#.#.....
|
||||||
|
......#........#..#...#............#..............#............#......##.....#.................................#......#.#.....#....
|
||||||
|
..........#........#...........#....................................#.....#...............................#............##....#.##..
|
||||||
|
........#.............#..#......##..................#.............#..#...#.#.....#...#...............#........................#....
|
||||||
|
....#...............#..........#....#...........................#....##.##...................#.#.......#...#.##....#........##..#..
|
||||||
|
......#.....#..#..........###...#.................####.###..........#...........#..........#......#............................#...
|
||||||
|
.#...#......#........#..#............##...............#.##......#...#.....#....................#.........#............#...#........
|
||||||
|
................##...................#.##..............#...#..#.......#.......###........#..#..#........#...##.....................
|
||||||
|
..............#....#..#..#.............##..............#................................#.....#..#....###.##..............#........
|
||||||
|
..#..............#........................................#..........#.....#..................#............#..#....................
|
||||||
|
.........#...#.....#...#...##...........#.###............##...#........#......................##......#....#.......#...............
|
||||||
|
....#....#.....#.......#......................................#.#.#....#..#..#..........#...................#........#...#.........
|
||||||
|
..#..#..................#..............#...#..#.........#......#....#.#................##..........#..#................#...........
|
||||||
|
...#...................#...........#....#...#.............##...#.......#.#..........#..#...............#....#....#.............#...
|
||||||
|
.......................##.#.#..##........#....#.........#.#.#.....##..............#.......#.......##.#..........#.#................
|
||||||
|
.....#..#..................#..##....#.......#.............#...#.....#.............#.............#.#...............#.....##......##.
|
||||||
|
...#.#.....#..........#.#............#..#..........................##.............#....#.......#.#...#...#....................#.#..
|
||||||
|
............##.#............#....#....#....#..##...#...............................#...#...#.........#.......................##.##.
|
||||||
|
....#.#...............#...#.......#............#..#...........................#..............#..........#....#...#...#..#...#....#.
|
||||||
|
.......#..#..#.#..#......#..........................#...............#.........#...........#................#..#...#................
|
||||||
|
...#..#...#.....#.#.......#....................................................#........#..........#.#...#......#..........#.......
|
||||||
|
..#...#.......#..#..............#.#...#.....#...#....#............#..........##...#.........##.............#.......................
|
||||||
|
...#.............#...#....#...#.....#.#.#..............#..........#........#........##...........#..#.#......................#.....
|
||||||
|
.#.......#.................#...............#..........#.....................#.....#............#......##....#.....#...#....###.....
|
||||||
|
...##...........#..........#.#........#............#..#.#........................#.....##............#...##.#.......#.##....#......
|
||||||
|
...........#........#...........#.......#......#...#.....#............................#........#..#................#...#....##.....
|
||||||
|
...##.##.........#.####..#............#..#.....#.......................#......#..........#.#..#.#....##........#....#.....#..#..##.
|
||||||
|
...................................................................................................................................
|
||||||
99
aoc2023/day21/main.go
Normal file
99
aoc2023/day21/main.go
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
_ "embed"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
aoc "go.sour.is/advent-of-code"
|
||||||
|
)
|
||||||
|
|
||||||
|
// var log = aoc.Log
|
||||||
|
|
||||||
|
func main() { aoc.MustResult(aoc.Runner(runner(64))) }
|
||||||
|
|
||||||
|
type result struct {
|
||||||
|
valuePT1 int
|
||||||
|
valuePT2 int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r result) String() string { return fmt.Sprintf("%#v", r) }
|
||||||
|
|
||||||
|
func runner(rounds int) func(scan *bufio.Scanner) (*result, error) {
|
||||||
|
return func(scan *bufio.Scanner) (*result, error) {
|
||||||
|
var garden garden
|
||||||
|
|
||||||
|
for scan.Scan() {
|
||||||
|
txt := scan.Text()
|
||||||
|
garden.m = append(garden.m, []rune(txt))
|
||||||
|
|
||||||
|
for i, c := range txt {
|
||||||
|
if c == 'S' {
|
||||||
|
garden.start[0] = len(garden.m) - 1
|
||||||
|
garden.start[1] = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
garden.Step(rounds)
|
||||||
|
return &result{
|
||||||
|
valuePT1: len(garden.steps[len(garden.steps)-1]),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type garden struct {
|
||||||
|
start aoc.Point[int]
|
||||||
|
m [][]rune
|
||||||
|
steps []aoc.Set[aoc.Point[int]]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *garden) Neighbors(p aoc.Point[int]) []aoc.Point[int] {
|
||||||
|
var neighbors []aoc.Point[int]
|
||||||
|
for _, n := range []aoc.Point[int]{
|
||||||
|
{p[0] - 1, p[1]},
|
||||||
|
{p[0] + 1, p[1]},
|
||||||
|
{p[0], p[1] - 1},
|
||||||
|
{p[0], p[1] + 1},
|
||||||
|
} {
|
||||||
|
if n[0] >= 0 && n[0] < len(g.m) && n[1] >= 0 && n[1] < len(g.m[0]) && g.m[n[0]][n[1]] != '#' {
|
||||||
|
neighbors = append(neighbors, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return neighbors
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *garden) Step(n int) {
|
||||||
|
if len(g.steps) == 0 {
|
||||||
|
g.steps = append(g.steps, aoc.NewSet(g.start))
|
||||||
|
}
|
||||||
|
|
||||||
|
for step := range(n) {
|
||||||
|
g.steps = append(g.steps, aoc.NewSet[aoc.Point[int]]())
|
||||||
|
for p := range g.steps[step] {
|
||||||
|
for _, n := range g.Neighbors(p) {
|
||||||
|
g.steps[step+1].Add(n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g garden) String() string {
|
||||||
|
var b []rune
|
||||||
|
for i, line := range g.m {
|
||||||
|
if i == g.start[0] {
|
||||||
|
line[g.start[1]] = 'X'
|
||||||
|
}
|
||||||
|
|
||||||
|
if steps := len(g.steps) - 1; steps > 0 {
|
||||||
|
for p := range g.steps[len(g.steps)-1] {
|
||||||
|
if p[0] == i {
|
||||||
|
line[p[1]] = 'O'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
b = append(b, line...)
|
||||||
|
b = append(b, '\n')
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
41
aoc2023/day21/main_test.go
Normal file
41
aoc2023/day21/main_test.go
Normal 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 := runner(6)(scan)
|
||||||
|
is.NoErr(err)
|
||||||
|
|
||||||
|
t.Log(result)
|
||||||
|
is.Equal(result.valuePT1, 16)
|
||||||
|
is.Equal(result.valuePT2, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSolution(t *testing.T) {
|
||||||
|
is := is.New(t)
|
||||||
|
scan := bufio.NewScanner(bytes.NewReader(input))
|
||||||
|
|
||||||
|
result, err := runner(64)(scan)
|
||||||
|
is.NoErr(err)
|
||||||
|
|
||||||
|
t.Log(result)
|
||||||
|
is.Equal(result.valuePT1, 3709)
|
||||||
|
is.Equal(result.valuePT2, 0)
|
||||||
|
}
|
||||||
13
aoc2023/day25/example.txt
Normal file
13
aoc2023/day25/example.txt
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
jqt: rhn xhk nvd
|
||||||
|
rsh: frs pzl lsr
|
||||||
|
xhk: hfx
|
||||||
|
cmg: qnr nvd lhk bvb
|
||||||
|
rhn: xhk bvb hfx
|
||||||
|
bvb: xhk hfx
|
||||||
|
pzl: lsr hfx nvd
|
||||||
|
qnr: nvd
|
||||||
|
ntq: jqt hfx bvb xhk
|
||||||
|
nvd: lhk
|
||||||
|
lsr: lhk
|
||||||
|
rzs: qnr cmg lsr rsh
|
||||||
|
frs: qnr lhk lsr
|
||||||
1261
aoc2023/day25/input.txt
Normal file
1261
aoc2023/day25/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
63
aoc2023/day25/main.go
Normal file
63
aoc2023/day25/main.go
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
_ "embed"
|
||||||
|
"fmt"
|
||||||
|
"iter"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
aoc "go.sour.is/advent-of-code"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
g := aoc.Graph[string, int]()
|
||||||
|
var root string
|
||||||
|
|
||||||
|
for scan.Scan() {
|
||||||
|
line := scan.Text()
|
||||||
|
v, lis, ok := strings.Cut(line, ": ")
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if root == "" {
|
||||||
|
root = v
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, l := range strings.Split(lis, " ") {
|
||||||
|
g.AddEdge(v, l, 1)
|
||||||
|
g.AddEdge(l, v, 1)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, v := range enumerate(g.BFS(root)) {
|
||||||
|
fmt.Println(i, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &result{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func enumerate[T any](vs iter.Seq[T]) iter.Seq2[int, T] {
|
||||||
|
i := 0
|
||||||
|
return func(yield func(int, T) bool) {
|
||||||
|
for v := range vs {
|
||||||
|
if !yield(i, v) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
41
aoc2023/day25/main_test.go
Normal file
41
aoc2023/day25/main_test.go
Normal 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, 50)
|
||||||
|
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)
|
||||||
|
}
|
||||||
6
aoc2024/day01/example.txt
Normal file
6
aoc2024/day01/example.txt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
3 4
|
||||||
|
4 3
|
||||||
|
2 5
|
||||||
|
1 3
|
||||||
|
3 9
|
||||||
|
3 3
|
||||||
1000
aoc2024/day01/input.txt
Normal file
1000
aoc2024/day01/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
86
aoc2024/day01/main.go
Normal file
86
aoc2024/day01/main.go
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
_ "embed"
|
||||||
|
"fmt"
|
||||||
|
"iter"
|
||||||
|
"slices"
|
||||||
|
"sort"
|
||||||
|
|
||||||
|
aoc "go.sour.is/advent-of-code"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
var (
|
||||||
|
left []int
|
||||||
|
right []int
|
||||||
|
)
|
||||||
|
|
||||||
|
for scan.Scan() {
|
||||||
|
txt := scan.Text()
|
||||||
|
|
||||||
|
var l, r int
|
||||||
|
_, err := fmt.Sscanf(txt, "%d %d", &l, &r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
left = append(left, l)
|
||||||
|
right = append(right, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Ints(left)
|
||||||
|
sort.Ints(right)
|
||||||
|
|
||||||
|
result := &result{}
|
||||||
|
|
||||||
|
result.valuePT1 = aoc.Reduce(func(i int, z pair[int, int], sum int) int {
|
||||||
|
return sum + aoc.ABS(z.L-z.R)
|
||||||
|
}, 0, zip(slices.Values(left), slices.Values(right)))
|
||||||
|
|
||||||
|
rmap := aoc.Reduce(func(i int, z int, m map[int]int) map[int]int {
|
||||||
|
m[z]++
|
||||||
|
return m
|
||||||
|
}, make(map[int]int), slices.Values(right))
|
||||||
|
|
||||||
|
for _, v := range left {
|
||||||
|
if r, ok := rmap[v]; ok {
|
||||||
|
result.valuePT2 += v*r
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type pair[L, R any] struct {
|
||||||
|
L L
|
||||||
|
R R
|
||||||
|
}
|
||||||
|
|
||||||
|
func zip[L, R any](l iter.Seq[L], r iter.Seq[R]) iter.Seq[pair[L,R]] {
|
||||||
|
return func(yield func(pair[L, R]) bool) {
|
||||||
|
pullR, stop := iter.Pull(r)
|
||||||
|
defer stop()
|
||||||
|
|
||||||
|
for l := range l {
|
||||||
|
r, _ := pullR()
|
||||||
|
|
||||||
|
if !yield(pair[L, R]{L: l, R: r}) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
41
aoc2024/day01/main_test.go
Normal file
41
aoc2024/day01/main_test.go
Normal 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, 11)
|
||||||
|
is.Equal(result.valuePT2, 31)
|
||||||
|
}
|
||||||
|
|
||||||
|
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, 2756096)
|
||||||
|
is.Equal(result.valuePT2, 23117829)
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user