mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2025-04-20 11:13:47 +01:00
Compare commits
No commits in common. "e541631a35aee728a7d9029df7583190c28307f9" and "58f36581f07e518559821a73a027442d8f63953d" have entirely different histories.
e541631a35
...
58f36581f0
106
day-16/one.go
106
day-16/one.go
@ -25,82 +25,84 @@ const (
|
|||||||
var steps int
|
var steps int
|
||||||
var dirs = []string{"N", "S", "W", "E"}
|
var dirs = []string{"N", "S", "W", "E"}
|
||||||
|
|
||||||
func runner(mover *mover, lines []string) {
|
func runner(move *mover, lines []string) {
|
||||||
for steps < math.MaxInt && mover.Y >= 0 && mover.Y < len(lines) && mover.X >= 0 && mover.X < len(lines[mover.Y]) {
|
for steps < math.MaxInt && move.Y >= 0 && move.Y < len(lines) && move.X >= 0 && move.X < len(lines[move.Y]) {
|
||||||
if nodeInNodes(mover.node, mover.nodes) {
|
//log.Debug(move.X, ":", move.Y, " ", string(lines[move.Y][move.X]), " ", dirs[move.direction()])
|
||||||
log.Debug(mover.node, " in nodes, breaking.")
|
//log.Debug(move.nodes)
|
||||||
|
if nodeInNodes(move.node, move.nodes) {
|
||||||
|
log.Debug(move.node, " in nodes, breaking.")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
mover.nodes = append(mover.nodes, mover.node)
|
move.nodes = append(move.nodes, move.node)
|
||||||
|
|
||||||
switch lines[mover.Y][mover.X] {
|
switch lines[move.Y][move.X] {
|
||||||
case SPACE: // '.'
|
case SPACE: // '.'
|
||||||
log.Debug("we have space and direction is ", dirs[mover.direction()])
|
//log.Debug("we have space and direction is ", dirs[move.direction()])
|
||||||
mover.move()
|
move.move()
|
||||||
|
|
||||||
case F_MIRROR: // '/'
|
case F_MIRROR: // '/'
|
||||||
log.Debug("we have forward mirror and direction is ", dirs[mover.direction()])
|
//log.Debug("we have forward mirror and direction is ", dirs[move.direction()])
|
||||||
switch mover.direction() {
|
switch move.direction() {
|
||||||
case N:
|
case N:
|
||||||
mover.setDirection(E)
|
move.setDirection(E)
|
||||||
case S:
|
case S:
|
||||||
mover.setDirection(W)
|
move.setDirection(W)
|
||||||
case W:
|
case W:
|
||||||
mover.setDirection(S)
|
move.setDirection(S)
|
||||||
case E:
|
case E:
|
||||||
mover.setDirection(N)
|
move.setDirection(N)
|
||||||
}
|
}
|
||||||
log.Debug("step: ", steps, " ", string(F_MIRROR), " direction changed to ", dirs[mover.direction()])
|
//log.Debug("step: ", steps, " ", string(F_MIRROR), " direction changed to ", dirs[move.direction()])
|
||||||
mover.move()
|
move.move()
|
||||||
|
|
||||||
case B_MIRROR: // '\'
|
case B_MIRROR: // '\'
|
||||||
log.Debug("we have backwards mirror and direction is ", dirs[mover.direction()])
|
//log.Debug("we have backwards mirror and direction is ", dirs[move.direction()])
|
||||||
switch mover.direction() {
|
switch move.direction() {
|
||||||
case N:
|
case N:
|
||||||
mover.setDirection(W)
|
move.setDirection(W)
|
||||||
case S:
|
case S:
|
||||||
mover.setDirection(E)
|
move.setDirection(E)
|
||||||
case W:
|
case W:
|
||||||
mover.setDirection(N)
|
move.setDirection(N)
|
||||||
case E:
|
case E:
|
||||||
mover.setDirection(S)
|
move.setDirection(S)
|
||||||
}
|
}
|
||||||
log.Debug("step: ", steps, " ", string(B_MIRROR), " direction changed to ", dirs[mover.direction()])
|
//log.Debug("step: ", steps, " ", string(B_MIRROR), " direction changed to ", dirs[move.direction()])
|
||||||
mover.move()
|
move.move()
|
||||||
|
|
||||||
case V_MIRROR: // '|'
|
case V_MIRROR: // '|'
|
||||||
log.Debug("we have vertical mirror and direction is ", dirs[mover.direction()])
|
//log.Debug("we have vertical mirror and direction is ", dirs[move.direction()])
|
||||||
if mover.direction() == N || mover.direction() == S {
|
if move.direction() == N || move.direction() == S {
|
||||||
mover.move()
|
move.move()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if mover.direction() == W || mover.direction() == E {
|
if move.direction() == W || move.direction() == E {
|
||||||
c := mover.coords
|
c := move.coords
|
||||||
mover.setDirection(N)
|
move.setDirection(N)
|
||||||
mover.move()
|
move.move()
|
||||||
runner(mover, lines)
|
runner(move, lines)
|
||||||
mover.coords = c
|
move.coords = c
|
||||||
mover.setDirection(S)
|
move.setDirection(S)
|
||||||
mover.move()
|
move.move()
|
||||||
runner(mover, lines)
|
runner(move, lines)
|
||||||
}
|
}
|
||||||
|
|
||||||
case H_MIRROR: // '-'
|
case H_MIRROR: // '-'
|
||||||
log.Debug("we have horizontal mirror and direction is ", dirs[mover.direction()])
|
//log.Debug("we have horizontal mirror and direction is ", dirs[move.direction()])
|
||||||
if mover.direction() == W || mover.direction() == E {
|
if move.direction() == W || move.direction() == E {
|
||||||
mover.move()
|
move.move()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if mover.direction() == N || mover.direction() == S {
|
if move.direction() == N || move.direction() == S {
|
||||||
c := mover.coords
|
c := move.coords
|
||||||
mover.setDirection(W)
|
move.setDirection(W)
|
||||||
mover.move()
|
move.move()
|
||||||
runner(mover, lines)
|
runner(move, lines)
|
||||||
mover.coords = c
|
move.coords = c
|
||||||
mover.setDirection(E)
|
move.setDirection(E)
|
||||||
mover.move()
|
move.move()
|
||||||
runner(mover, lines)
|
runner(move, lines)
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -112,14 +114,14 @@ func runner(mover *mover, lines []string) {
|
|||||||
|
|
||||||
// one
|
// one
|
||||||
func one(lines []string) int {
|
func one(lines []string) int {
|
||||||
mover := newMover(newNode(0, 0, E))
|
move := newMover(newNode(0, 0, E))
|
||||||
|
|
||||||
runner(mover, lines)
|
runner(move, lines)
|
||||||
|
|
||||||
if log.GetLevel() == log.DebugLevel {
|
if log.GetLevel() == log.DebugLevel {
|
||||||
n := printDebug(mover, lines)
|
n := printDebug(move, lines)
|
||||||
log.Debug("total: ", n)
|
log.Debug("total: ", n)
|
||||||
}
|
}
|
||||||
|
|
||||||
return uniqueNodes(mover)
|
return uniqueNodes(move)
|
||||||
}
|
}
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
module github.com/onyx-and-iris/aoc2023/day-17
|
|
||||||
|
|
||||||
go 1.21.5
|
|
@ -1,10 +0,0 @@
|
|||||||
TEST="test.txt"
|
|
||||||
INPUT="input.txt"
|
|
||||||
|
|
||||||
test:
|
|
||||||
go run . < $(TEST)
|
|
||||||
|
|
||||||
run:
|
|
||||||
go run . < $(INPUT)
|
|
||||||
|
|
||||||
all: test
|
|
@ -1,32 +0,0 @@
|
|||||||
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
114
day-17/one.go
@ -1,114 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
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)
|
|
||||||
})
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
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)
|
|
||||||
})
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
module github.com/onyx-and-iris/aoc2023/day-18
|
|
||||||
|
|
||||||
go 1.21.5
|
|
@ -1,37 +0,0 @@
|
|||||||
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))
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
TEST="test.txt"
|
|
||||||
INPUT="input.txt"
|
|
||||||
|
|
||||||
test:
|
|
||||||
go run . < $(TEST)
|
|
||||||
|
|
||||||
run:
|
|
||||||
go run . < $(INPUT)
|
|
||||||
|
|
||||||
all: test
|
|
@ -1,39 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
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)
|
|
||||||
|
|
||||||
}
|
|
@ -1,36 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
@ -1,58 +0,0 @@
|
|||||||
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