mirror of
				https://github.com/onyx-and-iris/aoc2023.git
				synced 2025-11-03 22:31:46 +00:00 
			
		
		
		
	Compare commits
	
		
			3 Commits
		
	
	
		
			58f36581f0
			...
			e541631a35
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| e541631a35 | |||
| 038dd531d9 | |||
| 497da642b2 | 
							
								
								
									
										106
									
								
								day-16/one.go
									
									
									
									
									
								
							
							
						
						
									
										106
									
								
								day-16/one.go
									
									
									
									
									
								
							@ -25,84 +25,82 @@ const (
 | 
				
			|||||||
var steps int
 | 
					var steps int
 | 
				
			||||||
var dirs = []string{"N", "S", "W", "E"}
 | 
					var dirs = []string{"N", "S", "W", "E"}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func runner(move *mover, lines []string) {
 | 
					func runner(mover *mover, lines []string) {
 | 
				
			||||||
	for steps < math.MaxInt && move.Y >= 0 && move.Y < len(lines) && move.X >= 0 && move.X < len(lines[move.Y]) {
 | 
						for steps < math.MaxInt && mover.Y >= 0 && mover.Y < len(lines) && mover.X >= 0 && mover.X < len(lines[mover.Y]) {
 | 
				
			||||||
		//log.Debug(move.X, ":", move.Y, " ", string(lines[move.Y][move.X]), " ", dirs[move.direction()])
 | 
							if nodeInNodes(mover.node, mover.nodes) {
 | 
				
			||||||
		//log.Debug(move.nodes)
 | 
								log.Debug(mover.node, " in nodes, breaking.")
 | 
				
			||||||
		if nodeInNodes(move.node, move.nodes) {
 | 
					 | 
				
			||||||
			log.Debug(move.node, " in nodes, breaking.")
 | 
					 | 
				
			||||||
			break
 | 
								break
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		move.nodes = append(move.nodes, move.node)
 | 
							mover.nodes = append(mover.nodes, mover.node)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		switch lines[move.Y][move.X] {
 | 
							switch lines[mover.Y][mover.X] {
 | 
				
			||||||
		case SPACE: // '.'
 | 
							case SPACE: // '.'
 | 
				
			||||||
			//log.Debug("we have space and direction is ", dirs[move.direction()])
 | 
								log.Debug("we have space and direction is ", dirs[mover.direction()])
 | 
				
			||||||
			move.move()
 | 
								mover.move()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		case F_MIRROR: // '/'
 | 
							case F_MIRROR: // '/'
 | 
				
			||||||
			//log.Debug("we have forward mirror and direction is ", dirs[move.direction()])
 | 
								log.Debug("we have forward mirror and direction is ", dirs[mover.direction()])
 | 
				
			||||||
			switch move.direction() {
 | 
								switch mover.direction() {
 | 
				
			||||||
			case N:
 | 
								case N:
 | 
				
			||||||
				move.setDirection(E)
 | 
									mover.setDirection(E)
 | 
				
			||||||
			case S:
 | 
								case S:
 | 
				
			||||||
				move.setDirection(W)
 | 
									mover.setDirection(W)
 | 
				
			||||||
			case W:
 | 
								case W:
 | 
				
			||||||
				move.setDirection(S)
 | 
									mover.setDirection(S)
 | 
				
			||||||
			case E:
 | 
								case E:
 | 
				
			||||||
				move.setDirection(N)
 | 
									mover.setDirection(N)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			//log.Debug("step: ", steps, " ", string(F_MIRROR), " direction changed to ", dirs[move.direction()])
 | 
								log.Debug("step: ", steps, " ", string(F_MIRROR), " direction changed to ", dirs[mover.direction()])
 | 
				
			||||||
			move.move()
 | 
								mover.move()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		case B_MIRROR: // '\'
 | 
							case B_MIRROR: // '\'
 | 
				
			||||||
			//log.Debug("we have backwards mirror and direction is ", dirs[move.direction()])
 | 
								log.Debug("we have backwards mirror and direction is ", dirs[mover.direction()])
 | 
				
			||||||
			switch move.direction() {
 | 
								switch mover.direction() {
 | 
				
			||||||
			case N:
 | 
								case N:
 | 
				
			||||||
				move.setDirection(W)
 | 
									mover.setDirection(W)
 | 
				
			||||||
			case S:
 | 
								case S:
 | 
				
			||||||
				move.setDirection(E)
 | 
									mover.setDirection(E)
 | 
				
			||||||
			case W:
 | 
								case W:
 | 
				
			||||||
				move.setDirection(N)
 | 
									mover.setDirection(N)
 | 
				
			||||||
			case E:
 | 
								case E:
 | 
				
			||||||
				move.setDirection(S)
 | 
									mover.setDirection(S)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			//log.Debug("step: ", steps, " ", string(B_MIRROR), " direction changed to ", dirs[move.direction()])
 | 
								log.Debug("step: ", steps, " ", string(B_MIRROR), " direction changed to ", dirs[mover.direction()])
 | 
				
			||||||
			move.move()
 | 
								mover.move()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		case V_MIRROR: // '|'
 | 
							case V_MIRROR: // '|'
 | 
				
			||||||
			//log.Debug("we have vertical mirror and direction is ", dirs[move.direction()])
 | 
								log.Debug("we have vertical mirror and direction is ", dirs[mover.direction()])
 | 
				
			||||||
			if move.direction() == N || move.direction() == S {
 | 
								if mover.direction() == N || mover.direction() == S {
 | 
				
			||||||
				move.move()
 | 
									mover.move()
 | 
				
			||||||
				continue
 | 
									continue
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if move.direction() == W || move.direction() == E {
 | 
								if mover.direction() == W || mover.direction() == E {
 | 
				
			||||||
				c := move.coords
 | 
									c := mover.coords
 | 
				
			||||||
				move.setDirection(N)
 | 
									mover.setDirection(N)
 | 
				
			||||||
				move.move()
 | 
									mover.move()
 | 
				
			||||||
				runner(move, lines)
 | 
									runner(mover, lines)
 | 
				
			||||||
				move.coords = c
 | 
									mover.coords = c
 | 
				
			||||||
				move.setDirection(S)
 | 
									mover.setDirection(S)
 | 
				
			||||||
				move.move()
 | 
									mover.move()
 | 
				
			||||||
				runner(move, lines)
 | 
									runner(mover, lines)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		case H_MIRROR: // '-'
 | 
							case H_MIRROR: // '-'
 | 
				
			||||||
			//log.Debug("we have horizontal mirror and direction is ", dirs[move.direction()])
 | 
								log.Debug("we have horizontal mirror and direction is ", dirs[mover.direction()])
 | 
				
			||||||
			if move.direction() == W || move.direction() == E {
 | 
								if mover.direction() == W || mover.direction() == E {
 | 
				
			||||||
				move.move()
 | 
									mover.move()
 | 
				
			||||||
				continue
 | 
									continue
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if move.direction() == N || move.direction() == S {
 | 
								if mover.direction() == N || mover.direction() == S {
 | 
				
			||||||
				c := move.coords
 | 
									c := mover.coords
 | 
				
			||||||
				move.setDirection(W)
 | 
									mover.setDirection(W)
 | 
				
			||||||
				move.move()
 | 
									mover.move()
 | 
				
			||||||
				runner(move, lines)
 | 
									runner(mover, lines)
 | 
				
			||||||
				move.coords = c
 | 
									mover.coords = c
 | 
				
			||||||
				move.setDirection(E)
 | 
									mover.setDirection(E)
 | 
				
			||||||
				move.move()
 | 
									mover.move()
 | 
				
			||||||
				runner(move, lines)
 | 
									runner(mover, lines)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		default:
 | 
							default:
 | 
				
			||||||
@ -114,14 +112,14 @@ func runner(move *mover, lines []string) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// one
 | 
					// one
 | 
				
			||||||
func one(lines []string) int {
 | 
					func one(lines []string) int {
 | 
				
			||||||
	move := newMover(newNode(0, 0, E))
 | 
						mover := newMover(newNode(0, 0, E))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	runner(move, lines)
 | 
						runner(mover, lines)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if log.GetLevel() == log.DebugLevel {
 | 
						if log.GetLevel() == log.DebugLevel {
 | 
				
			||||||
		n := printDebug(move, lines)
 | 
							n := printDebug(mover, lines)
 | 
				
			||||||
		log.Debug("total: ", n)
 | 
							log.Debug("total: ", n)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return uniqueNodes(move)
 | 
						return uniqueNodes(mover)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										3
									
								
								day-17/go.mod
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								day-17/go.mod
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,3 @@
 | 
				
			|||||||
 | 
					module github.com/onyx-and-iris/aoc2023/day-17
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					go 1.21.5
 | 
				
			||||||
							
								
								
									
										10
									
								
								day-17/makefile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								day-17/makefile
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					TEST="test.txt"
 | 
				
			||||||
 | 
					INPUT="input.txt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test:
 | 
				
			||||||
 | 
						go run . < $(TEST)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					run:
 | 
				
			||||||
 | 
						go run . < $(INPUT)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					all: test
 | 
				
			||||||
							
								
								
									
										32
									
								
								day-17/node.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								day-17/node.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,32 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import "fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type coords struct {
 | 
				
			||||||
 | 
						X int
 | 
				
			||||||
 | 
						Y int
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func newCoords(x, y int) coords {
 | 
				
			||||||
 | 
						return coords{X: x, Y: y}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// node represents a single point on the graph
 | 
				
			||||||
 | 
					type node struct {
 | 
				
			||||||
 | 
						cost       int
 | 
				
			||||||
 | 
						distance   int
 | 
				
			||||||
 | 
						directionX int
 | 
				
			||||||
 | 
						directionY int
 | 
				
			||||||
 | 
						coords
 | 
				
			||||||
 | 
						index int
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func newNode(cost, distance, directionX, directionY, x, y int) *node {
 | 
				
			||||||
 | 
						c := newCoords(x, y)
 | 
				
			||||||
 | 
						return &node{cost: cost, distance: distance, directionX: directionX, directionY: directionY, coords: c}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// String implements the fmt.Stringer interface
 | 
				
			||||||
 | 
					func (n node) String() string {
 | 
				
			||||||
 | 
						return fmt.Sprintf("%d%d%d%v", n.distance, n.directionX, n.directionY, n.coords)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										114
									
								
								day-17/one.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										114
									
								
								day-17/one.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,114 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"container/heap"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						log "github.com/sirupsen/logrus"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type option func(*dijkstra)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func WithMinDistance(distance int) option {
 | 
				
			||||||
 | 
						return func(d *dijkstra) {
 | 
				
			||||||
 | 
							d.minDistance = distance
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func WithMaxDistance(distance int) option {
 | 
				
			||||||
 | 
						return func(d *dijkstra) {
 | 
				
			||||||
 | 
							d.maxDistance = distance
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type dijkstra struct {
 | 
				
			||||||
 | 
						graph       [][]int
 | 
				
			||||||
 | 
						minDistance int
 | 
				
			||||||
 | 
						maxDistance int
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func newDijkstra(graph [][]int, opts ...option) *dijkstra {
 | 
				
			||||||
 | 
						d := &dijkstra{graph: graph}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, opt := range opts {
 | 
				
			||||||
 | 
							opt(d)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return d
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (d dijkstra) initialize(start coords) *pqueue {
 | 
				
			||||||
 | 
						pq := newPriorityQueue()
 | 
				
			||||||
 | 
						heap.Init(pq)
 | 
				
			||||||
 | 
						// we don't encounter heat loss for start point unless we enter this block again
 | 
				
			||||||
 | 
						heap.Push(pq, newNode(0, 0, 0, 0, start.X, start.Y))
 | 
				
			||||||
 | 
						return pq
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// run performs the lowest cost dijkstra algorithm with a min heap
 | 
				
			||||||
 | 
					func (d dijkstra) run(start, end coords) int {
 | 
				
			||||||
 | 
						pq := d.initialize(start)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						visited := map[string]bool{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for pq.Len() > 0 {
 | 
				
			||||||
 | 
							cost, node := func() (int, *node) {
 | 
				
			||||||
 | 
								x := heap.Pop(pq).(*node)
 | 
				
			||||||
 | 
								return x.cost, x
 | 
				
			||||||
 | 
							}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// we reached final location, return its lowest cost
 | 
				
			||||||
 | 
							if node.X == end.X && node.Y == end.Y && node.distance >= d.minDistance {
 | 
				
			||||||
 | 
								log.Debug("returning lowest cost with min distance >= ", d.minDistance)
 | 
				
			||||||
 | 
								return node.cost
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if _, ok := visited[node.String()]; ok {
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							visited[node.String()] = true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var neighbours = [][]int{{0, -1}, {0, 1}, {-1, 0}, {1, 0}} // N, S, W, E
 | 
				
			||||||
 | 
							for _, n := range neighbours {
 | 
				
			||||||
 | 
								nextX := node.X + n[0]
 | 
				
			||||||
 | 
								nextY := node.Y + n[1]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if nextY < 0 || nextY >= len(d.graph) || nextX < 0 || nextX >= len(d.graph[nextY]) {
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if node.directionX == -n[0] && node.directionY == -n[1] { // are we going backwards?
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								var distance = 1
 | 
				
			||||||
 | 
								if node.directionX == n[0] || node.directionY == n[1] { // same direction
 | 
				
			||||||
 | 
									distance = node.distance + 1
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									if node.distance < d.minDistance {
 | 
				
			||||||
 | 
										continue
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if distance > d.maxDistance {
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								new_cost := cost + d.graph[nextY][nextX]
 | 
				
			||||||
 | 
								heap.Push(pq, newNode(new_cost, distance, n[0], n[1], nextX, nextY))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return 0
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// one returns the lowest cost path from start to end
 | 
				
			||||||
 | 
					func one(lines []string) int {
 | 
				
			||||||
 | 
						graph := buildGraph(lines)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						start := newCoords(0, 0)
 | 
				
			||||||
 | 
						end := newCoords(len(graph[0])-1, len(graph)-1)
 | 
				
			||||||
 | 
						dijkstra := newDijkstra(graph, WithMaxDistance(3))
 | 
				
			||||||
 | 
						cost := dijkstra.run(start, end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return cost
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										49
									
								
								day-17/pqueue.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								day-17/pqueue.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,49 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"container/heap"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// pqueue implements the heap.Interface interface
 | 
				
			||||||
 | 
					// it represents a min heap priority queue
 | 
				
			||||||
 | 
					type pqueue []*node
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func newPriorityQueue() *pqueue {
 | 
				
			||||||
 | 
						pq := make(pqueue, 0)
 | 
				
			||||||
 | 
						return &pq
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (pq pqueue) Len() int {
 | 
				
			||||||
 | 
						return len(pq)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (pq *pqueue) Push(x interface{}) {
 | 
				
			||||||
 | 
						n := len(*pq)
 | 
				
			||||||
 | 
						node := x.(*node)
 | 
				
			||||||
 | 
						node.index = n
 | 
				
			||||||
 | 
						*pq = append(*pq, node)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (pq *pqueue) Pop() interface{} {
 | 
				
			||||||
 | 
						old := *pq
 | 
				
			||||||
 | 
						n := len(old)
 | 
				
			||||||
 | 
						node := old[n-1]
 | 
				
			||||||
 | 
						node.index = -1
 | 
				
			||||||
 | 
						*pq = old[0 : n-1]
 | 
				
			||||||
 | 
						return node
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (pq *pqueue) Update(node *node, value int) {
 | 
				
			||||||
 | 
						node.cost = value
 | 
				
			||||||
 | 
						heap.Fix(pq, node.index)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (pq pqueue) Less(i, j int) bool {
 | 
				
			||||||
 | 
						return pq[i].cost < pq[j].cost
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (pq pqueue) Swap(i, j int) {
 | 
				
			||||||
 | 
						pq[i], pq[j] = pq[j], pq[i]
 | 
				
			||||||
 | 
						pq[i].index = i
 | 
				
			||||||
 | 
						pq[j].index = j
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										27
									
								
								day-17/pqueue_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								day-17/pqueue_test.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,27 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"container/heap"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/go-playground/assert/v2"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestPriorityQueue(t *testing.T) {
 | 
				
			||||||
 | 
						//t.Skip("skipping test")
 | 
				
			||||||
 | 
						pq := newPriorityQueue()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						heap.Push(pq, newNode(30, 0, 0, 0, 0, 0))
 | 
				
			||||||
 | 
						heap.Push(pq, newNode(10, 0, 0, 0, 8, 0))
 | 
				
			||||||
 | 
						heap.Push(pq, newNode(20, 0, 0, 0, 13, 0))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						t.Run("Should create a queue size 3", func(t *testing.T) {
 | 
				
			||||||
 | 
							assert.Equal(t, 3, pq.Len())
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						item := heap.Pop(pq).(*node)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						t.Run("Should return item with cost 10", func(t *testing.T) {
 | 
				
			||||||
 | 
							assert.Equal(t, 10, item.cost)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										21
									
								
								day-17/solution.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								day-17/solution.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,21 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						log "github.com/sirupsen/logrus"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						log.SetLevel(log.InfoLevel)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func main() {
 | 
				
			||||||
 | 
						lines := readlines()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ans := one(lines)
 | 
				
			||||||
 | 
						fmt.Printf("solution one: %d\n", ans)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ans = two(lines)
 | 
				
			||||||
 | 
						fmt.Printf("solution two: %d\n", ans)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										14
									
								
								day-17/two.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								day-17/two.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,14 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// two returns the lowest cost path from start to end
 | 
				
			||||||
 | 
					// with a min/max distance set
 | 
				
			||||||
 | 
					func two(lines []string) int {
 | 
				
			||||||
 | 
						graph := buildGraph(lines)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						start := newCoords(0, 0)
 | 
				
			||||||
 | 
						end := newCoords(len(graph[0])-1, len(graph)-1)
 | 
				
			||||||
 | 
						dijkstra := newDijkstra(graph, WithMinDistance(4), WithMaxDistance(10))
 | 
				
			||||||
 | 
						cost := dijkstra.run(start, end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return cost
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										26
									
								
								day-17/two_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								day-17/two_test.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,26 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						_ "embed"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/stretchr/testify/assert"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						//go:embed test2.txt
 | 
				
			||||||
 | 
						testInput2 []byte
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestDjistraWithMinDistance(t *testing.T) {
 | 
				
			||||||
 | 
						//t.Skip("skipping test")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						input := strings.Split(string(testInput2), "\n")
 | 
				
			||||||
 | 
						cost := two(input)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						t.Run("Should return a lowest cost of 71", func(t *testing.T) {
 | 
				
			||||||
 | 
							assert.Equal(t, 71, cost)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										38
									
								
								day-17/util.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								day-17/util.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,38 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"bufio"
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// readlines reads lines from stdin.
 | 
				
			||||||
 | 
					// it returns them as an array of strings
 | 
				
			||||||
 | 
					func readlines() []string {
 | 
				
			||||||
 | 
						lines := []string{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						scanner := bufio.NewScanner(os.Stdin)
 | 
				
			||||||
 | 
						for scanner.Scan() {
 | 
				
			||||||
 | 
							lines = append(lines, scanner.Text())
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if err := scanner.Err(); err != nil {
 | 
				
			||||||
 | 
							log.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return lines
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// buildGraph parses lines into costs for graph
 | 
				
			||||||
 | 
					func buildGraph(lines []string) [][]int {
 | 
				
			||||||
 | 
						graph := make([][]int, len(lines))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for i, line := range lines {
 | 
				
			||||||
 | 
							graph[i] = make([]int, len(line))
 | 
				
			||||||
 | 
							for j, r := range line {
 | 
				
			||||||
 | 
								graph[i][j] = int(r - '0')
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return graph
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										3
									
								
								day-18/go.mod
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								day-18/go.mod
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,3 @@
 | 
				
			|||||||
 | 
					module github.com/onyx-and-iris/aoc2023/day-18
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					go 1.21.5
 | 
				
			||||||
							
								
								
									
										37
									
								
								day-18/imager.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								day-18/imager.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,37 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type coords struct {
 | 
				
			||||||
 | 
						X int
 | 
				
			||||||
 | 
						Y int
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func newCoords(x, y int) coords {
 | 
				
			||||||
 | 
						return coords{X: x, Y: y}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type imager struct {
 | 
				
			||||||
 | 
						point coords
 | 
				
			||||||
 | 
						space []coords
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// newImager returns an imager type
 | 
				
			||||||
 | 
					func newImager() *imager {
 | 
				
			||||||
 | 
						return &imager{point: newCoords(0, 0), space: make([]coords, 0)}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// add appends new coordinates to all points that describe the polygon
 | 
				
			||||||
 | 
					func (i *imager) add(direction string, count int) {
 | 
				
			||||||
 | 
						for j := 0; j < count; j++ {
 | 
				
			||||||
 | 
							switch direction {
 | 
				
			||||||
 | 
							case "U":
 | 
				
			||||||
 | 
								i.point.Y--
 | 
				
			||||||
 | 
							case "D":
 | 
				
			||||||
 | 
								i.point.Y++
 | 
				
			||||||
 | 
							case "L":
 | 
				
			||||||
 | 
								i.point.X--
 | 
				
			||||||
 | 
							case "R":
 | 
				
			||||||
 | 
								i.point.X++
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							i.space = append(i.space, newCoords(i.point.X, i.point.Y))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										10
									
								
								day-18/makefile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								day-18/makefile
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					TEST="test.txt"
 | 
				
			||||||
 | 
					INPUT="input.txt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test:
 | 
				
			||||||
 | 
						go run . < $(TEST)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					run:
 | 
				
			||||||
 | 
						go run . < $(INPUT)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					all: test
 | 
				
			||||||
							
								
								
									
										39
									
								
								day-18/one.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								day-18/one.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,39 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"math"
 | 
				
			||||||
 | 
						"regexp"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var r = regexp.MustCompile(`(?P<direction>[A-Z]) (?P<count>[0-9]+) \((?P<colour>.*)\)`)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func fromRegex(imager *imager, lines []string) {
 | 
				
			||||||
 | 
						for _, line := range lines {
 | 
				
			||||||
 | 
							direction, count := func() (string, int) {
 | 
				
			||||||
 | 
								x := getParams(r, line)
 | 
				
			||||||
 | 
								return x["direction"], mustConv(x["count"])
 | 
				
			||||||
 | 
							}()
 | 
				
			||||||
 | 
							imager.add(direction, count)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func buildImage(withParser func(imager *imager, lines []string), imager *imager, lines []string) {
 | 
				
			||||||
 | 
						withParser(imager, lines)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// one returns the area of the polygon described by imager.space
 | 
				
			||||||
 | 
					func one(lines []string) int {
 | 
				
			||||||
 | 
						imager := newImager()
 | 
				
			||||||
 | 
						buildImage(fromRegex, imager, lines)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						area := 0
 | 
				
			||||||
 | 
						for i := 0; i < len(imager.space); i++ {
 | 
				
			||||||
 | 
							next := imager.space[(i+1)%len(imager.space)]
 | 
				
			||||||
 | 
							area += imager.space[i].X*next.Y - imager.space[i].Y*next.X
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// add perimeter to area within perimeter
 | 
				
			||||||
 | 
						area = len(imager.space) + (int(math.Abs(float64(area))) / 2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return area - len(imager.space)/2 + 1
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										22
									
								
								day-18/solution.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								day-18/solution.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,22 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						log "github.com/sirupsen/logrus"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						log.SetLevel(log.InfoLevel)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func main() {
 | 
				
			||||||
 | 
						lines := readlines()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ans := one(lines)
 | 
				
			||||||
 | 
						fmt.Printf("solution one: %d\n", ans)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ans = two(lines)
 | 
				
			||||||
 | 
						fmt.Printf("solution two: %d\n", ans)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										36
									
								
								day-18/two.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								day-18/two.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,36 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"math"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func fromHex(imager *imager, lines []string) {
 | 
				
			||||||
 | 
						for _, line := range lines {
 | 
				
			||||||
 | 
							direction, count := func() (string, int) {
 | 
				
			||||||
 | 
								var dirs = []string{"R", "D", "L", "U"}
 | 
				
			||||||
 | 
								m := getParams(r, line)
 | 
				
			||||||
 | 
								code := strings.TrimLeft(m["colour"], "#")
 | 
				
			||||||
 | 
								return dirs[mustConv(string(code[len(code)-1]))], mustConvHex(code[:len(code)-1])
 | 
				
			||||||
 | 
							}()
 | 
				
			||||||
 | 
							imager.add(direction, count)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// two returns the area of the polygon described by imager.space
 | 
				
			||||||
 | 
					// it uses the hex codes to define points
 | 
				
			||||||
 | 
					func two(lines []string) int {
 | 
				
			||||||
 | 
						imager := newImager()
 | 
				
			||||||
 | 
						buildImage(fromHex, imager, lines)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						area := 0
 | 
				
			||||||
 | 
						for i := 0; i < len(imager.space); i++ {
 | 
				
			||||||
 | 
							next := imager.space[(i+1)%len(imager.space)]
 | 
				
			||||||
 | 
							area += imager.space[i].X*next.Y - imager.space[i].Y*next.X
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// add perimeter to area within perimeter
 | 
				
			||||||
 | 
						area = len(imager.space) + (int(math.Abs(float64(area))) / 2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return area - len(imager.space)/2 + 1
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										58
									
								
								day-18/util.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								day-18/util.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,58 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"bufio"
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"regexp"
 | 
				
			||||||
 | 
						"strconv"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// readlines reads lines from stdin.
 | 
				
			||||||
 | 
					// it returns them as an array of strings
 | 
				
			||||||
 | 
					func readlines() []string {
 | 
				
			||||||
 | 
						lines := []string{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						scanner := bufio.NewScanner(os.Stdin)
 | 
				
			||||||
 | 
						for scanner.Scan() {
 | 
				
			||||||
 | 
							lines = append(lines, scanner.Text())
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if err := scanner.Err(); err != nil {
 | 
				
			||||||
 | 
							log.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return lines
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func getParams(rexp *regexp.Regexp, url string) map[string]string {
 | 
				
			||||||
 | 
						match := rexp.FindStringSubmatch(url)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						m := make(map[string]string)
 | 
				
			||||||
 | 
						for i, name := range rexp.SubexpNames() {
 | 
				
			||||||
 | 
							if i > 0 && i <= len(match) {
 | 
				
			||||||
 | 
								m[name] = match[i]
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return m
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// mustConv converts string to int
 | 
				
			||||||
 | 
					// it will panic if an error occurs
 | 
				
			||||||
 | 
					func mustConv(s string) int {
 | 
				
			||||||
 | 
						n, err := strconv.Atoi(s)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							panic(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return n
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// mustConv converts string to int
 | 
				
			||||||
 | 
					// it will panic if an error occurs
 | 
				
			||||||
 | 
					func mustConvHex(s string) int {
 | 
				
			||||||
 | 
						n, err := strconv.ParseInt(s, 16, 64)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							panic(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return int(n)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user