Compare commits
5 Commits
graphs
...
04bbac8559
| Author | SHA1 | Date | |
|---|---|---|---|
|
04bbac8559
|
|||
|
3c9af95ec4
|
|||
|
50af2114d4
|
|||
|
f8fa61672f
|
|||
|
951c2c298a
|
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-----
|
||||
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.................................................................
|
||||
........#.#........#..........#.....#......#........#.#............#.#.........##...#............#...#.....#...........#.....#.....
|
||||
..................##.......#......#..#..................#..##......#.........#...#.........#............#....#...........##........
|
||||
..................#...........#.##.......#..............#....................#.#....#...##...#.....#.......#..#.....##.............
|
||||
...........#....#....##...#................................#.........#.................#....#.......................#...#..........
|
||||
.............#.#.#.....#.#...#.......#.#...........#....#.#.........#........##......#.......#.......##...#..........#.##..........
|
||||
...........##.#....#....#..#.....#...............#......................#...........#.....#...........#.#.......#..................
|
||||
.#.......................#.#...##.#....#.#...........#.#....##.##......#........................##....#.#....#.....................
|
||||
...#..................#.#...........##....###.................#....................##......#.....#............#.##....#.......##...
|
||||
.#..................####...#.#..#.##.#...####...#......#....................#..........................#....#......................
|
||||
.....#..........#.....#.#...............#.........##...#......#.#.....#...........#.....#............##......#.....#...............
|
||||
......#........#....#..#............................###.#...##........#........#.................#..#........#...............##....
|
||||
......................#.........................##....#...#..............#..#............#.#....#......##.#......#.........##......
|
||||
..................#....#...#.#.......#..............#..#..#...#.......#...#.....#.....#...............#.#.#...............#........
|
||||
...#...##......................##..##...........#......#.............#............#...........##.......#....#......................
|
||||
.............................##...........##...#...#.....#....#.......#.#..#.....##...............#..#.#......#..............##....
|
||||
...#..#.#...#.......#..................#............#....#.#.##.#.........#......#........#..#..........#....##...........#....#...
|
||||
.....#.............................#......#.#.....#.##.#.................#.........#..#..#.#.#........................#.#.......#..
|
||||
....#........#........#....#.#.#.....#.##............###.#..........#...##......#........##...#.........#.............#........#...
|
||||
..#..............................#.............................#......#...................#...#......#.............#...............
|
||||
.......#...#...............#.................##....................#....##.......#....##....##...#..................#..............
|
||||
.........................#...#..#.....##.............#...##.......#.....#...#..............#.....#.#.#..#........................#.
|
||||
......#.......##.#.............#....................#...#...........#...#..##....#.....#............#...........#........#.........
|
||||
...#....#...#......#........................#.....#.........#......#........#.....#.#...........#.#..............#......#....#.....
|
||||
........#..........#...................#...........##.#..#...#................#....##.............#................#....#..........
|
||||
........#.......#....#.............#..........#...................#.#....#.#........#..#..............................#..........#.
|
||||
............#........#....................#.#.........#......#.............#................#...#.............#....#...............
|
||||
..#..............................#..#....##.....#......................##...#..#.........#......#......................#...#...#.#.
|
||||
.##....#.............#...........##..................#...#..............#.......#................................#.........#.......
|
||||
.....#......#.#.....##...........#.....#.......#..........##..#.............#.#.........#.#.#.............##........#......#..#....
|
||||
.......#.......#.#.#..#......................#....#............##....#...........#.........................#......#................
|
||||
.........##.........#..............#.#.#....#...#.......#.................................#..##..................##.###.##...#.....
|
||||
......................#...#.#.......#..#.#..........#........#.......#.................#..................................#........
|
||||
....##........#.........#.........................#................#......#...#.....#..................................#..#.#......
|
||||
...##..............#..................#...#......#........###..#.......#..........#..............................#......#..........
|
||||
.#.#.#....#..........#......#.#.........#.....#...............................#.....#.#............#....#........#.............#...
|
||||
........#.........#.........#......................#..#......##.....#..#..........................#......##....#......##.##.....#..
|
||||
.##...#.......#.#....#..#.#...................#.........#....#...........#.....#.........#............##......#............#.......
|
||||
.##..#.........#..........#.#.................................#.#.#...............................#............#......##...#.#.....
|
||||
......#........#..#...#............#..............#............#......##.....#.................................#......#.#.....#....
|
||||
..........#........#...........#....................................#.....#...............................#............##....#.##..
|
||||
........#.............#..#......##..................#.............#..#...#.#.....#...#...............#........................#....
|
||||
....#...............#..........#....#...........................#....##.##...................#.#.......#...#.##....#........##..#..
|
||||
......#.....#..#..........###...#.................####.###..........#...........#..........#......#............................#...
|
||||
.#...#......#........#..#............##...............#.##......#...#.....#....................#.........#............#...#........
|
||||
................##...................#.##..............#...#..#.......#.......###........#..#..#........#...##.....................
|
||||
..............#....#..#..#.............##..............#................................#.....#..#....###.##..............#........
|
||||
..#..............#........................................#..........#.....#..................#............#..#....................
|
||||
.........#...#.....#...#...##...........#.###............##...#........#......................##......#....#.......#...............
|
||||
....#....#.....#.......#......................................#.#.#....#..#..#..........#...................#........#...#.........
|
||||
..#..#..................#..............#...#..#.........#......#....#.#................##..........#..#................#...........
|
||||
...#...................#...........#....#...#.............##...#.......#.#..........#..#...............#....#....#.............#...
|
||||
.......................##.#.#..##........#....#.........#.#.#.....##..............#.......#.......##.#..........#.#................
|
||||
.....#..#..................#..##....#.......#.............#...#.....#.............#.............#.#...............#.....##......##.
|
||||
...#.#.....#..........#.#............#..#..........................##.............#....#.......#.#...#...#....................#.#..
|
||||
............##.#............#....#....#....#..##...#...............................#...#...#.........#.......................##.##.
|
||||
....#.#...............#...#.......#............#..#...........................#..............#..........#....#...#...#..#...#....#.
|
||||
.......#..#..#.#..#......#..........................#...............#.........#...........#................#..#...#................
|
||||
...#..#...#.....#.#.......#....................................................#........#..........#.#...#......#..........#.......
|
||||
..#...#.......#..#..............#.#...#.....#...#....#............#..........##...#.........##.............#.......................
|
||||
...#.............#...#....#...#.....#.#.#..............#..........#........#........##...........#..#.#......................#.....
|
||||
.#.......#.................#...............#..........#.....................#.....#............#......##....#.....#...#....###.....
|
||||
...##...........#..........#.#........#............#..#.#........................#.....##............#...##.#.......#.##....#......
|
||||
...........#........#...........#.......#......#...#.....#............................#........#..#................#...#....##.....
|
||||
...##.##.........#.####..#............#..#.....#.......................#......#..........#.#..#.#....##........#....#.....#..#..##.
|
||||
...................................................................................................................................
|
||||
30
aoc2023/day21/main.go
Normal file
30
aoc2023/day21/main.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
|
||||
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) {
|
||||
|
||||
for scan.Scan() {
|
||||
_ = scan.Text()
|
||||
|
||||
}
|
||||
|
||||
return &result{}, nil
|
||||
}
|
||||
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 := 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)
|
||||
}
|
||||
0
aoc2024/day01/example.txt
Normal file
0
aoc2024/day01/example.txt
Normal file
0
aoc2024/day01/input.txt
Normal file
0
aoc2024/day01/input.txt
Normal file
30
aoc2024/day01/main.go
Normal file
30
aoc2024/day01/main.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
|
||||
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) {
|
||||
|
||||
for scan.Scan() {
|
||||
_ = scan.Text()
|
||||
|
||||
}
|
||||
|
||||
return &result{}, nil
|
||||
}
|
||||
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, 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)
|
||||
}
|
||||
97
aoc_test.go
97
aoc_test.go
@@ -9,66 +9,6 @@ import (
|
||||
aoc "go.sour.is/advent-of-code"
|
||||
)
|
||||
|
||||
func TestReverse(t *testing.T) {
|
||||
is := is.New(t)
|
||||
|
||||
is.Equal(aoc.Reverse([]int{1, 2, 3, 4}), []int{4, 3, 2, 1})
|
||||
}
|
||||
|
||||
func TestLCM(t *testing.T) {
|
||||
is := is.New(t)
|
||||
|
||||
is.Equal(aoc.LCM([]int{}...), 0)
|
||||
is.Equal(aoc.LCM(5), 5)
|
||||
is.Equal(aoc.LCM(5, 3), 15)
|
||||
is.Equal(aoc.LCM(5, 3, 2), 30)
|
||||
}
|
||||
|
||||
func TestReadStringToInts(t *testing.T) {
|
||||
is := is.New(t)
|
||||
|
||||
is.Equal(aoc.ReadStringToInts([]string{"1", "2", "3"}), []int{1, 2, 3})
|
||||
}
|
||||
|
||||
func TestRepeat(t *testing.T) {
|
||||
is := is.New(t)
|
||||
|
||||
is.Equal(aoc.Repeat(5, 3), []int{5, 5, 5})
|
||||
}
|
||||
|
||||
func TestPower2(t *testing.T) {
|
||||
is := is.New(t)
|
||||
|
||||
is.Equal(aoc.Power2(0), 1)
|
||||
is.Equal(aoc.Power2(1), 2)
|
||||
is.Equal(aoc.Power2(2), 4)
|
||||
}
|
||||
|
||||
func TestABS(t *testing.T) {
|
||||
is := is.New(t)
|
||||
|
||||
is.Equal(aoc.ABS(1), 1)
|
||||
is.Equal(aoc.ABS(0), 0)
|
||||
is.Equal(aoc.ABS(-1), 1)
|
||||
}
|
||||
|
||||
func TestTranspose(t *testing.T) {
|
||||
is := is.New(t)
|
||||
|
||||
is.Equal(
|
||||
aoc.Transpose(
|
||||
[][]int{
|
||||
{1, 1},
|
||||
{0, 0},
|
||||
{1, 1},
|
||||
},
|
||||
),
|
||||
[][]int{
|
||||
{1, 0, 1},
|
||||
{1, 0, 1},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func TestList(t *testing.T) {
|
||||
is := is.New(t)
|
||||
@@ -116,23 +56,6 @@ func TestPriorityQueue(t *testing.T) {
|
||||
is.True(v == nil)
|
||||
}
|
||||
|
||||
func TestSet(t *testing.T) {
|
||||
is := is.New(t)
|
||||
|
||||
s := aoc.Set(1, 2, 3)
|
||||
is.True(!s.Has(0))
|
||||
is.True(s.Has(1))
|
||||
is.True(s.Has(2))
|
||||
is.True(s.Has(3))
|
||||
is.True(!s.Has(4))
|
||||
|
||||
s.Add(4)
|
||||
is.True(s.Has(4))
|
||||
|
||||
items := s.Items()
|
||||
sort.Ints(items)
|
||||
is.Equal(items, []int{1, 2, 3, 4})
|
||||
}
|
||||
|
||||
func ExamplePriorityQueue() {
|
||||
type memo struct {
|
||||
@@ -194,26 +117,6 @@ func ExamplePriorityQueue() {
|
||||
// point 5 is 22 steps away.
|
||||
// point 6 is 19 steps away.
|
||||
}
|
||||
|
||||
func TestStack(t *testing.T) {
|
||||
is := is.New(t)
|
||||
|
||||
s := aoc.Stack(1, 2, 3, 4)
|
||||
is.True(!s.IsEmpty())
|
||||
is.Equal(s.Pop(), 4)
|
||||
is.Equal(s.Pop(), 3)
|
||||
is.Equal(s.Pop(), 2)
|
||||
is.Equal(s.Pop(), 1)
|
||||
is.True(s.IsEmpty())
|
||||
s.Push(4, 3, 2, 1)
|
||||
is.True(!s.IsEmpty())
|
||||
is.Equal(s.Pop(), 1)
|
||||
is.Equal(s.Pop(), 2)
|
||||
is.Equal(s.Pop(), 3)
|
||||
is.Equal(s.Pop(), 4)
|
||||
is.True(s.IsEmpty())
|
||||
}
|
||||
|
||||
func TestGraph(t *testing.T) {
|
||||
is := is.New(t)
|
||||
|
||||
|
||||
4
go.mod
4
go.mod
@@ -1,8 +1,6 @@
|
||||
module go.sour.is/advent-of-code
|
||||
|
||||
go 1.21.4
|
||||
|
||||
toolchain go1.21.5
|
||||
go 1.23.2
|
||||
|
||||
require (
|
||||
github.com/matryer/is v1.4.1
|
||||
|
||||
80
grids.go
80
grids.go
@@ -2,7 +2,11 @@ package aoc
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/exp/maps"
|
||||
)
|
||||
|
||||
type Vector struct {
|
||||
@@ -82,6 +86,76 @@ func (m *Map[I, T]) Valid(p Point[I]) bool {
|
||||
return p[0] >= 0 && p[0] < rows && p[1] >= 0 && p[1] < cols
|
||||
}
|
||||
|
||||
type cmap[C number, N comparable] struct {
|
||||
base pather[C, N]
|
||||
neighbors map[N]map[N]C
|
||||
}
|
||||
|
||||
func (m *cmap[C, N]) Cost(a, b N) C {
|
||||
if v, ok := m.neighbors[a]; ok {
|
||||
return v[b]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
func (m *cmap[C, N]) Neighbors(n N) []N {
|
||||
if v, ok := m.neighbors[n]; ok {
|
||||
return maps.Keys(v)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (m *cmap[C, N]) Target(n N, c C) bool {
|
||||
return m.base.Target(n, c)
|
||||
}
|
||||
func (m *cmap[C, N]) String() string {
|
||||
var b = &strings.Builder{}
|
||||
|
||||
for k, nbs := range m.neighbors {
|
||||
fmt.Fprintln(b, k)
|
||||
for to, c := range nbs {
|
||||
fmt.Fprintln(b, " ", to, c)
|
||||
}
|
||||
}
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func CompressMap[C number, N comparable](p pather[C, N], start N) pather[C, N] {
|
||||
var next = []N{start}
|
||||
var visited = make(map[N]map[N]C)
|
||||
|
||||
var n N
|
||||
for len(next) > 0 {
|
||||
n, next = next[len(next)-1], next[:len(next)-1]
|
||||
|
||||
if _, ok := visited[n]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
nbs := p.Neighbors(n)
|
||||
if len(nbs) == 2 {
|
||||
a, b := nbs[0], nbs[1]
|
||||
if to, ok := visited[a]; ok {
|
||||
to[b] = to[n] + p.Cost(n, b)
|
||||
delete(to, n)
|
||||
visited[a] = to
|
||||
} else if to, ok := visited[b]; ok {
|
||||
to[a] = to[n] + p.Cost(n, a)
|
||||
delete(to, n)
|
||||
visited[b] = to
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
visited[n] = make(map[N]C)
|
||||
next = append(next, nbs...)
|
||||
for _, to := range nbs {
|
||||
visited[n][to] = p.Cost(n, to)
|
||||
}
|
||||
}
|
||||
|
||||
return &cmap[C, N]{base: p, neighbors: visited}
|
||||
}
|
||||
|
||||
type adjacencyList[V any, C comparable] map[C][]V
|
||||
type graph[V any, W cmp.Ordered, C comparable] map[C]*vertex[V, W]
|
||||
type graphOption[V any, W cmp.Ordered, C comparable] func(g *graph[V, W, C])
|
||||
@@ -117,7 +191,7 @@ func Graph[V any, W cmp.Ordered, C comparable](opts ...graphOption[V, W, C]) *gr
|
||||
return &g
|
||||
}
|
||||
func (g *graph[V, W, C]) AddVertex(id C, value V) {
|
||||
(*g)[id] = &vertex[V,W]{Value: value}
|
||||
(*g)[id] = &vertex[V, W]{Value: value}
|
||||
}
|
||||
func (g *graph[V, W, C]) AddEdge(from, to C, w W) {
|
||||
if g == nil {
|
||||
@@ -130,7 +204,7 @@ func (g *graph[V, W, C]) AddEdge(from, to C, w W) {
|
||||
return
|
||||
}
|
||||
|
||||
(*g)[from].Edges = append((*g)[from].Edges, edge[V,W]{(*g)[to], w})
|
||||
(*g)[from].Edges = append((*g)[from].Edges, edge[V, W]{(*g)[to], w})
|
||||
}
|
||||
func (g *graph[V, W, C]) Neighbors(v C) []V {
|
||||
if g == nil {
|
||||
@@ -170,5 +244,3 @@ func WithAdjacencyList[W cmp.Ordered, C comparable](list adjacencyList[C, C]) gr
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// func GraphFromMap()
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
package main
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"bufio"
|
||||
"strings"
|
||||
"strconv"
|
||||
)
|
||||
func main() {
|
||||
//Enter your code here. Read input from STDIN. Print output to STDOUT
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
|
||||
var nodes int
|
||||
var tree *node
|
||||
var target [2]int
|
||||
|
||||
for scanner.Scan() {
|
||||
text := scanner.Text()
|
||||
if nodes == 0 {
|
||||
nodes, _ = strconv.Atoi(text)
|
||||
continue
|
||||
}
|
||||
|
||||
if tree == nil {
|
||||
for _, s := range strings.Fields(text) {
|
||||
v, _ := strconv.Atoi(s)
|
||||
tree = insertNode(tree, &node{value:v})
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
for i, s := range strings.Fields(text) {
|
||||
v, _ := strconv.Atoi(s)
|
||||
target[i] = v
|
||||
}
|
||||
}
|
||||
if target[0]>target[1] {
|
||||
target[0], target[1] = target[1], target[0]
|
||||
}
|
||||
fmt.Fprintln(os.Stderr, tree)
|
||||
if n := lsa(tree, target); n != nil {
|
||||
fmt.Println(n.value)
|
||||
return
|
||||
}
|
||||
fmt.Println("nil")
|
||||
}
|
||||
|
||||
type node struct{
|
||||
value int
|
||||
left *node
|
||||
right *node
|
||||
}
|
||||
func insertNode(root, n *node) *node {
|
||||
if root == nil {
|
||||
return n
|
||||
}
|
||||
if root.value > n.value {
|
||||
root.left = insertNode(root.left, n)
|
||||
return root
|
||||
}
|
||||
root.right = insertNode(root.right, n)
|
||||
return root
|
||||
}
|
||||
|
||||
func (n *node) String() string {
|
||||
if n == nil { return "nil" }
|
||||
return fmt.Sprintf("%v (%v %v)", n.value, n.left, n.right)
|
||||
}
|
||||
|
||||
func lsa(tree *node, target [2]int) *node {
|
||||
if tree == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if target[0]>tree.value {
|
||||
return lsa(tree.right, target)
|
||||
}
|
||||
|
||||
if target[1]<tree.value {
|
||||
return lsa(tree.left, target)
|
||||
}
|
||||
|
||||
return tree
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user