Compare commits

..

8 Commits

Author SHA1 Message Date
cacd140961 ^^ 2024-01-02 09:20:51 +00:00
0f357df19d Merge branch 'main' of https://github.com/onyx-and-iris/aoc2023 2024-01-02 09:19:19 +00:00
5b225a9166 adjust bound factory method to set open end of interval 2024-01-02 09:18:14 +00:00
9ba5ad695e add mutex lock 2024-01-02 06:58:03 +00:00
cc452c76a4 fix day-5 p2 2024-01-01 11:32:43 +00:00
73298dae8d move area calculation into a function
fix mustConvHex docstring
2023-12-24 10:50:41 +00:00
35776a5470 upd docstrings 2023-12-23 11:58:21 +00:00
2814b12750 rename var move for p2 2023-12-22 22:23:43 +00:00
13 changed files with 185 additions and 140 deletions

View File

@@ -3,9 +3,9 @@ package main
import "sync" import "sync"
// returns the number of unique nodes (reducing multiple nodes with different directions to one) // returns the number of unique nodes (reducing multiple nodes with different directions to one)
func uniqueNodes(move *mover) int { func uniqueNodes(mover *mover) int {
uniqueCoords := []coords{} uniqueCoords := []coords{}
for _, node := range move.nodes { for _, node := range mover.nodes {
if !coordInCoords(node.coords, uniqueCoords) { if !coordInCoords(node.coords, uniqueCoords) {
uniqueCoords = append(uniqueCoords, node.coords) uniqueCoords = append(uniqueCoords, node.coords)
} }

View File

@@ -44,7 +44,7 @@ func (d dijkstra) initialize(start coords) *pqueue {
return pq return pq
} }
// run performs the lowest cost dijkstra algorithm with a min heap // run performs the lowest cost dijkstra algorithm from start to end
func (d dijkstra) run(start, end coords) int { func (d dijkstra) run(start, end coords) int {
pq := d.initialize(start) pq := d.initialize(start)
@@ -101,7 +101,7 @@ func (d dijkstra) run(start, end coords) int {
return 0 return 0
} }
// one returns the lowest cost path from start to end // one returns the lowest cost path for the given graph from start to end coords
func one(lines []string) int { func one(lines []string) int {
graph := buildGraph(lines) graph := buildGraph(lines)

View File

@@ -4,8 +4,8 @@ import (
"container/heap" "container/heap"
) )
// pqueue implements the heap.Interface interface // pqueue represents a min priority queue
// it represents a min heap priority queue // it implements the heap.Interface interface
type pqueue []*node type pqueue []*node
func newPriorityQueue() *pqueue { func newPriorityQueue() *pqueue {

View File

@@ -1,6 +1,6 @@
package main package main
// two returns the lowest cost path from start to end // two returns the lowest cost path for a given graph from start to end coords
// with a min/max distance set // with a min/max distance set
func two(lines []string) int { func two(lines []string) int {
graph := buildGraph(lines) graph := buildGraph(lines)

View File

@@ -1,7 +1,6 @@
package main package main
import ( import (
"math"
"regexp" "regexp"
) )
@@ -26,14 +25,5 @@ func one(lines []string) int {
imager := newImager() imager := newImager()
buildImage(fromRegex, imager, lines) buildImage(fromRegex, imager, lines)
area := 0 return calculateArea(imager.space)
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
} }

View File

@@ -1,7 +1,6 @@
package main package main
import ( import (
"math"
"strings" "strings"
) )
@@ -23,14 +22,5 @@ func two(lines []string) int {
imager := newImager() imager := newImager()
buildImage(fromHex, imager, lines) buildImage(fromHex, imager, lines)
area := 0 return calculateArea(imager.space)
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
} }

View File

@@ -3,6 +3,7 @@ package main
import ( import (
"bufio" "bufio"
"log" "log"
"math"
"os" "os"
"regexp" "regexp"
"strconv" "strconv"
@@ -47,7 +48,7 @@ func mustConv(s string) int {
return n return n
} }
// mustConv converts string to int // mustConvHex converts a hex string to int
// it will panic if an error occurs // it will panic if an error occurs
func mustConvHex(s string) int { func mustConvHex(s string) int {
n, err := strconv.ParseInt(s, 16, 64) n, err := strconv.ParseInt(s, 16, 64)
@@ -56,3 +57,17 @@ func mustConvHex(s string) int {
} }
return int(n) return int(n)
} }
// calculateArea returns the area of the polygon described by imager.space
func calculateArea(space []coords) int {
area := 0
for i := 0; i < len(space); i++ {
next := space[(i+1)%len(space)]
area += space[i].X*next.Y - space[i].Y*next.X
}
// add perimeter to area within perimeter
area = len(space) + (int(math.Abs(float64(area))) / 2)
return area - len(space)/2 + 1
}

View File

@@ -1,19 +1,22 @@
package main package main
type Data struct { type data struct {
dest int dest int
source int source int
offset int offset int
} }
var seeds = []int{} func newData(nums ...int) data {
return data{dest: nums[0], source: nums[1], offset: nums[2]}
var dataMap = map[string][]Data{
"seed-to-soil": make([]Data, 0),
"soil-to-fertilizer": make([]Data, 0),
"fertilizer-to-water": make([]Data, 0),
"water-to-light": make([]Data, 0),
"light-to-temperature": make([]Data, 0),
"temperature-to-humidity": make([]Data, 0),
"humidity-to-location": make([]Data, 0),
} }
func (d data) transform(start, end int) (int, int) {
f := func(x int) int {
return x - d.source + d.dest
}
return f(start), f(end - 1)
}
var dataMap = map[string][]data{}
var identifiers = []string{}

View File

@@ -2,43 +2,36 @@ package main
import ( import (
"math" "math"
log "github.com/sirupsen/logrus"
) )
var identifiers = []string{"seed-to-soil", "soil-to-fertilizer", "fertilizer-to-water", "water-to-light", "light-to-temperature", "temperature-to-humidity", "humidity-to-location"} var seeds = []int{}
// next recursively calculates each destination for each set of data in dataMap
func next(i int, datapoint int) int { func next(i int, datapoint int) int {
if i == len(identifiers) { if i == len(identifiers) {
return datapoint return datapoint
} }
dest := func() int { dest := 0
datas := dataMap[identifiers[i]] for _, data := range dataMap[identifiers[i]] {
dest := 0 if datapoint >= data.source && datapoint <= data.source+data.offset {
for _, data := range datas { dest = data.dest + (datapoint - data.source)
if datapoint >= data.source && datapoint <= data.source+data.offset { break
dest = data.dest + (datapoint - data.source)
break
}
} }
if dest == 0 { }
dest = datapoint if dest == 0 {
} dest = datapoint
return dest }
}()
//log.Debug(identifiers[i], ": ", dest)
return next(i+1, dest) return next(i+1, dest)
} }
// one returns the lowest location // one returns the lowest location for any seed in seeds
func one(lines []string) (int, error) { func one(lines []string) (int, error) {
lowest := math.MaxInt
parseLines(lines) parseLines(lines)
lowest := math.MaxInt
for _, seed := range seeds { for _, seed := range seeds {
location := next(0, seed) location := next(0, seed)
log.Info(location)
if location < lowest { if location < lowest {
lowest = location lowest = location
} }

51
day-5/queue.go Normal file
View File

@@ -0,0 +1,51 @@
package main
import (
log "github.com/sirupsen/logrus"
)
// queue represents a FIFO queue of bounds
type queue struct {
size int
elements []bound
}
// newQueue returns a queue type
// it initializes the queue size and elements
func newQueue(size int, elems []bound) queue {
return queue{size: size, elements: elems}
}
// enqueue adds an item to the queue
func (q *queue) enqueue(elem bound) {
if q.size >= 0 && q.getLength() == q.size {
log.Info("Queue Overflow")
return
}
q.elements = append(q.elements, elem)
}
// dequeue pops an element from the start of the queue
func (q *queue) dequeue() bound {
if q.isEmpty() {
log.Info("Queue UnderFlow")
return bound{}
}
element := q.elements[0]
if q.getLength() == 1 {
q.elements = nil
return element
}
q.elements = q.elements[1:]
return element
}
// getLength returns the number of items in the queue
func (q *queue) getLength() int {
return len(q.elements)
}
// isEmpty returns true if no items are in the queue
func (q *queue) isEmpty() bool {
return len(q.elements) == 0
}

View File

@@ -7,7 +7,7 @@ import (
) )
func init() { func init() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.InfoLevel)
} }
func main() { func main() {
@@ -19,9 +19,6 @@ func main() {
} }
fmt.Printf("solution one: %d\n", ans) fmt.Printf("solution one: %d\n", ans)
ans, err = two(lines) ans = two(lines)
if err != nil {
log.Fatal(err)
}
fmt.Printf("solution two: %d\n", ans) fmt.Printf("solution two: %d\n", ans)
} }

View File

@@ -1,78 +1,89 @@
package main package main
import ( import (
"fmt"
"math" "math"
"sync" "sync"
"sync/atomic"
"time"
log "github.com/sirupsen/logrus"
) )
type WaitGroupCount struct { var wg sync.WaitGroup
sync.WaitGroup var mu sync.Mutex
count int64
}
func (wg *WaitGroupCount) Add(delta int) { const UNLIMITED = -1
atomic.AddInt64(&wg.count, int64(delta))
wg.WaitGroup.Add(delta)
}
func (wg *WaitGroupCount) Done() {
atomic.AddInt64(&wg.count, -1)
wg.WaitGroup.Done()
}
func (wg *WaitGroupCount) GetCount() int {
return int(atomic.LoadInt64(&wg.count))
}
var wg = WaitGroupCount{}
//var checked = make([]bound, 0)
// bound represents the lower and upper limits of a single range
type bound struct { type bound struct {
start int lower, upper int
end int
} }
var bounds = []bound{} // newBound returns a bound type
// it defines an open ended interval
func newBound(lower, upper int) bound {
return bound{lower: lower, upper: upper - 1}
}
// two returns the lowest location // nextTransform recursively calculates each new set of seed ranges for each set of data in dataMap
func two(lines []string) (int, error) { func nextTransform(i int, in []bound) []bound {
lowest := math.MaxInt if i == len(identifiers) {
return in
for i := 0; i < len(seeds); i += 2 {
bounds = append(bounds, bound{start: seeds[i], end: seeds[i] + seeds[i+1]})
} }
startTime := time.Now() q := newQueue(UNLIMITED, in)
in = make([]bound, 0)
for !q.isEmpty() {
r := q.dequeue()
go func() { hasOverlap := func() bool {
for { for _, data := range dataMap[identifiers[i]] {
elapsed := time.Since(startTime) start := max(r.lower, data.source)
fmt.Printf("[%s] wg count: %d\n", elapsed.Round(time.Second), wg.GetCount()) end := min(r.upper, data.source+data.offset)
time.Sleep(time.Second)
}
}()
for _, bound := range bounds { if isOverlapping(start, end) {
wg.Add(1) // add new seed range
go func(start int, end int) { in = append(in, newBound(data.transform(start, end)))
defer wg.Done()
for i := start; i < end; i++ { // append unmatched portions of seed range back into queue
location := next(0, i) if start > r.lower {
if location < lowest { q.enqueue(newBound(r.lower, start))
lowest = location }
if r.upper > end {
q.enqueue(newBound(end, r.upper))
}
return true
} }
} }
}(bound.start, bound.end) return false
log.Info(bound, " completed") }()
// there was no overlap, add the seed range as is
if !hasOverlap {
in = append(in, r)
}
}
return nextTransform(i+1, in)
}
// two returns the lowest location for any seed in seedRanges
func two(lines []string) int {
var seedRanges = []bound{}
for i := 0; i < len(seeds); i += 2 {
wg.Add(1)
go func(i int) {
defer wg.Done()
mu.Lock()
seedRanges = append(seedRanges, nextTransform(0, []bound{newBound(seeds[i], seeds[i]+seeds[i+1])})...)
mu.Unlock()
}(i)
} }
wg.Wait() wg.Wait()
return lowest - 1, nil // returning a value one too high? not sure why. lowest := math.MaxInt
for _, r := range seedRanges {
if r.lower < lowest {
lowest = r.lower
}
}
return lowest
} }

View File

@@ -2,12 +2,13 @@ package main
import ( import (
"bufio" "bufio"
"log"
"os" "os"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"unicode" "unicode"
log "github.com/sirupsen/logrus"
) )
// readlines reads lines from stdin. // readlines reads lines from stdin.
@@ -27,28 +28,27 @@ func readlines() []string {
return lines return lines
} }
var r = regexp.MustCompile(`([\w-]+) map[:]`)
// parseLines parses input to a data map // parseLines parses input to a data map
func parseLines(lines []string) { func parseLines(lines []string) {
var _regex_identifier = regexp.MustCompile(`(?P<identifier>[\w-]+) map[:]`)
f := func(c rune) bool { f := func(c rune) bool {
return !unicode.IsDigit(c) return !unicode.IsDigit(c)
} }
for i := 0; i < len(lines); i++ { seeds = convertToInts(strings.FieldsFunc(lines[0], f))
if i == 0 {
seeds = convertToInts(strings.FieldsFunc(lines[i], f))
continue
}
m := _regex_identifier.FindStringSubmatch(lines[i]) for i := 2; i < len(lines); i++ {
m := r.FindStringSubmatch(lines[i])
if len(m) == 2 { if len(m) == 2 {
identifiers = append(identifiers, m[1])
for i = i + 1; i < len(lines) && len(lines[i]) != 0; i++ { for i = i + 1; i < len(lines) && len(lines[i]) != 0; i++ {
nums := convertToInts(strings.FieldsFunc(lines[i], f)) d := newData(convertToInts(strings.FieldsFunc(lines[i], f))...)
dataMap[m[1]] = append(dataMap[m[1]], Data{dest: nums[0], source: nums[1], offset: nums[2]}) dataMap[m[1]] = append(dataMap[m[1]], d)
} }
} }
} }
log.Debug(identifiers)
} }
// convertToInts converts a string representing ints to an array of ints // convertToInts converts a string representing ints to an array of ints
@@ -61,13 +61,8 @@ func convertToInts(data []string) []int {
return nums return nums
} }
/* // isOverlapping returns true if two ranges overlap. see:
func isChecked(i int) bool { // https://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap/325964#325964
for _, bound := range checked { func isOverlapping(maxStart, minEnd int) bool {
if i > bound.start && i < bound.end { return maxStart < minEnd
return true
}
}
return false
} }
*/