Skip to content

Commit eecd987

Browse files
committed
WIP
1 parent d44a930 commit eecd987

File tree

10 files changed

+452
-0
lines changed

10 files changed

+452
-0
lines changed

2023/17/common.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"os"
6+
)
7+
8+
type Dir uint8
9+
10+
const (
11+
N Dir = iota
12+
E
13+
S
14+
W
15+
)
16+
17+
func (d Dir) Clock() Dir {
18+
return (d + 1) % 4
19+
}
20+
21+
func (d Dir) CounterClock() Dir {
22+
return (d - 1 + 4) % 4
23+
}
24+
25+
func parseInput() map[P]int {
26+
m := map[P]int{}
27+
28+
scanner := bufio.NewScanner(os.Stdin)
29+
for i := 0; scanner.Scan(); i++ {
30+
line := scanner.Text()
31+
for j, ch := range line {
32+
m[P{i, j}] = int(ch - '0')
33+
}
34+
}
35+
return m
36+
}

2023/17/example

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2413432311323
2+
3215453535623
3+
3255245654254
4+
3446585845452
5+
4546657867536
6+
1438598798454
7+
4457876987766
8+
3637877979653
9+
4654967986887
10+
4564679986453
11+
1224686865563
12+
2546548887735
13+
4322674655533

2023/17/main1.go

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package main
2+
3+
import (
4+
"container/heap"
5+
"fmt"
6+
)
7+
8+
// const Size = 141
9+
const Size = 13
10+
11+
var (
12+
Start = P{0, 0}
13+
End = P{12, 12}
14+
)
15+
16+
func main() {
17+
m := parseInput()
18+
fmt.Println(Dijkstra(m, Start, End))
19+
}
20+
21+
type P struct{ x, y int }
22+
23+
type Node struct {
24+
P
25+
Dir
26+
}
27+
28+
func Dijkstra(m map[P]int, start, goal P) int {
29+
parent := map[P]P{}
30+
startNode := Node{start, E}
31+
heats := map[Node]int{startNode: 0}
32+
33+
pq := &priorityQueue{}
34+
heap.Init(pq)
35+
heap.Push(pq, pqNode{startNode, 0})
36+
37+
for pq.Len() > 0 {
38+
39+
curr := heap.Pop(pq).(pqNode)
40+
41+
// we reached the end
42+
if curr.Node.P == goal {
43+
44+
for x := goal; x != start; x = parent[x] {
45+
46+
}
47+
48+
return curr.Heat
49+
}
50+
51+
for _, n := range neighbours(m, curr) {
52+
53+
if _, ok := heats[n.Node]; !ok || n.Heat <= heats[n.Node] {
54+
parent[n.P] = parent[n.Node], curr.Node
55+
heats[n.Node] = n.Heat
56+
heap.Push(pq, n)
57+
}
58+
}
59+
}
60+
panic("no path found")
61+
}
62+
63+
var Delta = map[Dir]P{
64+
N: {-1, 0},
65+
S: {1, 0},
66+
W: {0, -1},
67+
E: {0, 1},
68+
}
69+
70+
func neighbours(m map[P]int, node pqNode) []pqNode {
71+
var neighbs []pqNode
72+
73+
for _, d := range []Dir{node.Dir.Clock(), node.Dir.CounterClock()} {
74+
delta := Delta[d]
75+
heat := node.Heat
76+
for i := 0; i < 3; i++ {
77+
next := P{node.Node.x + i*delta.x, node.Node.y + i*delta.y}
78+
79+
if next.x < 0 || next.x >= Size {
80+
continue
81+
}
82+
if next.y < 0 || next.y >= Size {
83+
continue
84+
}
85+
86+
heat += m[next]
87+
neighbs = append(neighbs,
88+
pqNode{
89+
Node: Node{
90+
P: next,
91+
Dir: d,
92+
},
93+
Heat: node.Heat + m[next],
94+
})
95+
}
96+
}
97+
return neighbs
98+
}

2023/17/main2.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
)
8+
9+
func main() {
10+
scanner := bufio.NewScanner(os.Stdin)
11+
12+
for scanner.Scan() {
13+
line := scanner.Text()
14+
15+
}
16+
17+
fmt.Println()
18+
}

2023/17/priority_queue.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
type pqNode struct {
4+
Node
5+
Heat int
6+
}
7+
8+
type priorityQueue []pqNode
9+
10+
func (n priorityQueue) Len() int {
11+
return len(n)
12+
}
13+
14+
func (n priorityQueue) Less(i, j int) bool {
15+
return n[i].Heat < n[j].Heat
16+
}
17+
18+
func (n priorityQueue) Swap(i, j int) {
19+
n[i], n[j] = n[j], n[i]
20+
}
21+
22+
func (n *priorityQueue) Push(x any) {
23+
*n = append(*n, x.(pqNode))
24+
}
25+
26+
func (n *priorityQueue) Pop() any {
27+
old := *n
28+
l := len(old)
29+
x := old[l-1]
30+
*n = old[0 : l-1]
31+
return x
32+
}

2023/24/Makefile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
input:
2+
http "https://adventofcode.com/2023/day/24/input" "Cookie:session=${AOC_SESSION};" >input
3+
4+
main1:
5+
go build -o main1 main1.go common.go
6+
7+
main2:
8+
go build -o main2 main2.go common.go
9+
10+
.PHONY: run1 run2 clean
11+
12+
run1: main1 input
13+
./main1 <input
14+
15+
run2: main2 input
16+
./main2 <input
17+
18+
clean:
19+
rm -f main1 main2 input

2023/24/common.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
)
8+
9+
type P struct{ x, y, z int }
10+
11+
type Hail struct {
12+
ID int
13+
Pos P
14+
Vel P
15+
}
16+
17+
func (h Hail) String() string {
18+
return fmt.Sprintf("Hailstone : (%d, %d, %d) @ (%d, %d, %d)", h.Pos.x, h.Pos.y, h.Pos.z, h.Vel.x, h.Vel.y, h.Vel.z)
19+
}
20+
21+
func parseInput() []Hail {
22+
hail := []Hail{}
23+
24+
scanner := bufio.NewScanner(os.Stdin)
25+
for id := 0; scanner.Scan(); id++ {
26+
line := scanner.Text()
27+
var px, py, pz, vx, vy, vz int
28+
fmt.Sscanf(line, "%d, %d, %d @ %d, %d, %d", &px, &py, &pz, &vx, &vy, &vz)
29+
hail = append(hail, Hail{
30+
ID: id,
31+
Pos: P{px, py, pz},
32+
Vel: P{vx, vy, vz},
33+
})
34+
}
35+
36+
return hail
37+
}

2023/24/example

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
19, 13, 30 @ -2, 1, -2
2+
18, 19, 22 @ -1, -1, -2
3+
20, 25, 34 @ -2, -2, -4
4+
12, 31, 28 @ -1, -2, -1
5+
20, 19, 15 @ 1, -5, -3
6+

0 commit comments

Comments
 (0)