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"
// 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{}
for _, node := range move.nodes {
for _, node := range mover.nodes {
if !coordInCoords(node.coords, uniqueCoords) {
uniqueCoords = append(uniqueCoords, node.coords)
}

View File

@@ -44,7 +44,7 @@ func (d dijkstra) initialize(start coords) *pqueue {
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 {
pq := d.initialize(start)
@@ -101,7 +101,7 @@ func (d dijkstra) run(start, end coords) int {
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 {
graph := buildGraph(lines)

View File

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

View File

@@ -1,6 +1,6 @@
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
func two(lines []string) int {
graph := buildGraph(lines)

View File

@@ -1,7 +1,6 @@
package main
import (
"math"
"regexp"
)
@@ -26,14 +25,5 @@ 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
return calculateArea(imager.space)
}

View File

@@ -1,7 +1,6 @@
package main
import (
"math"
"strings"
)
@@ -23,14 +22,5 @@ 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
return calculateArea(imager.space)
}

View File

@@ -3,6 +3,7 @@ package main
import (
"bufio"
"log"
"math"
"os"
"regexp"
"strconv"
@@ -47,7 +48,7 @@ func mustConv(s string) int {
return n
}
// mustConv converts string to int
// mustConvHex converts a hex string to int
// it will panic if an error occurs
func mustConvHex(s string) int {
n, err := strconv.ParseInt(s, 16, 64)
@@ -56,3 +57,17 @@ func mustConvHex(s string) int {
}
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
type Data struct {
type data struct {
dest int
source int
offset int
}
var seeds = []int{}
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 newData(nums ...int) data {
return data{dest: nums[0], source: nums[1], offset: nums[2]}
}
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,21 +2,18 @@ package main
import (
"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 {
if i == len(identifiers) {
return datapoint
}
dest := func() int {
datas := dataMap[identifiers[i]]
dest := 0
for _, data := range datas {
for _, data := range dataMap[identifiers[i]] {
if datapoint >= data.source && datapoint <= data.source+data.offset {
dest = data.dest + (datapoint - data.source)
break
@@ -25,20 +22,16 @@ func next(i int, datapoint int) int {
if dest == 0 {
dest = datapoint
}
return dest
}()
//log.Debug(identifiers[i], ": ", 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) {
lowest := math.MaxInt
parseLines(lines)
lowest := math.MaxInt
for _, seed := range seeds {
location := next(0, seed)
log.Info(location)
if location < lowest {
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() {
log.SetLevel(log.DebugLevel)
log.SetLevel(log.InfoLevel)
}
func main() {
@@ -19,9 +19,6 @@ func main() {
}
fmt.Printf("solution one: %d\n", ans)
ans, err = two(lines)
if err != nil {
log.Fatal(err)
}
ans = two(lines)
fmt.Printf("solution two: %d\n", ans)
}

View File

@@ -1,78 +1,89 @@
package main
import (
"fmt"
"math"
"sync"
"sync/atomic"
"time"
log "github.com/sirupsen/logrus"
)
type WaitGroupCount struct {
sync.WaitGroup
count int64
}
var wg sync.WaitGroup
var mu sync.Mutex
func (wg *WaitGroupCount) Add(delta int) {
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)
const UNLIMITED = -1
// bound represents the lower and upper limits of a single range
type bound struct {
start int
end int
lower, upper 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
func two(lines []string) (int, error) {
lowest := math.MaxInt
for i := 0; i < len(seeds); i += 2 {
bounds = append(bounds, bound{start: seeds[i], end: seeds[i] + seeds[i+1]})
// nextTransform recursively calculates each new set of seed ranges for each set of data in dataMap
func nextTransform(i int, in []bound) []bound {
if i == len(identifiers) {
return in
}
startTime := time.Now()
q := newQueue(UNLIMITED, in)
in = make([]bound, 0)
for !q.isEmpty() {
r := q.dequeue()
go func() {
for {
elapsed := time.Since(startTime)
fmt.Printf("[%s] wg count: %d\n", elapsed.Round(time.Second), wg.GetCount())
time.Sleep(time.Second)
hasOverlap := func() bool {
for _, data := range dataMap[identifiers[i]] {
start := max(r.lower, data.source)
end := min(r.upper, data.source+data.offset)
if isOverlapping(start, end) {
// add new seed range
in = append(in, newBound(data.transform(start, end)))
// append unmatched portions of seed range back into queue
if start > r.lower {
q.enqueue(newBound(r.lower, start))
}
if r.upper > end {
q.enqueue(newBound(end, r.upper))
}
return true
}
}
return false
}()
for _, bound := range bounds {
// 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(start int, end int) {
go func(i int) {
defer wg.Done()
for i := start; i < end; i++ {
location := next(0, i)
if location < lowest {
lowest = location
}
}
}(bound.start, bound.end)
log.Info(bound, " completed")
mu.Lock()
seedRanges = append(seedRanges, nextTransform(0, []bound{newBound(seeds[i], seeds[i]+seeds[i+1])})...)
mu.Unlock()
}(i)
}
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 (
"bufio"
"log"
"os"
"regexp"
"strconv"
"strings"
"unicode"
log "github.com/sirupsen/logrus"
)
// readlines reads lines from stdin.
@@ -27,28 +28,27 @@ func readlines() []string {
return lines
}
var r = regexp.MustCompile(`([\w-]+) map[:]`)
// parseLines parses input to a data map
func parseLines(lines []string) {
var _regex_identifier = regexp.MustCompile(`(?P<identifier>[\w-]+) map[:]`)
f := func(c rune) bool {
return !unicode.IsDigit(c)
}
for i := 0; i < len(lines); i++ {
if i == 0 {
seeds = convertToInts(strings.FieldsFunc(lines[i], f))
continue
}
seeds = convertToInts(strings.FieldsFunc(lines[0], f))
m := _regex_identifier.FindStringSubmatch(lines[i])
for i := 2; i < len(lines); i++ {
m := r.FindStringSubmatch(lines[i])
if len(m) == 2 {
identifiers = append(identifiers, m[1])
for i = i + 1; i < len(lines) && len(lines[i]) != 0; i++ {
nums := convertToInts(strings.FieldsFunc(lines[i], f))
dataMap[m[1]] = append(dataMap[m[1]], Data{dest: nums[0], source: nums[1], offset: nums[2]})
d := newData(convertToInts(strings.FieldsFunc(lines[i], f))...)
dataMap[m[1]] = append(dataMap[m[1]], d)
}
}
}
log.Debug(identifiers)
}
// convertToInts converts a string representing ints to an array of ints
@@ -61,13 +61,8 @@ func convertToInts(data []string) []int {
return nums
}
/*
func isChecked(i int) bool {
for _, bound := range checked {
if i > bound.start && i < bound.end {
return true
}
}
return false
// isOverlapping returns true if two ranges overlap. see:
// https://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap/325964#325964
func isOverlapping(maxStart, minEnd int) bool {
return maxStart < minEnd
}
*/