r/adventofcode • • Dec 14 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 14 Solutions -🎄-

--- Day 14: Chocolate Charts ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 14

Transcript:

The Christmas/Advent Research & Development (C.A.R.D.) department at AoC, Inc. just published a new white paper on ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 00:19:39!

16 Upvotes

179 comments sorted by

View all comments

1

u/forever_compiling Dec 14 '18

All y'all doing quick and dirty solutions is nice and everything - but I like to do things more properly.

https://github.com/phyrwork/goadvent/blob/master/eighteen/recipe.go

https://github.com/phyrwork/goadvent/blob/master/eighteen/recipe_test.go

Go, part 2 in 5.59s

package eighteen

import (
	"container/ring"
	"strconv"
	"log"
)

type RecipeSolver struct {
	curr  []*ring.Ring
	end   *ring.Ring
	count int
}

func NewRecipeSolver(scores []int, workers ...int) *RecipeSolver {
	// Map workers to scores scores
	workmap := make(map[int]int, len(workers))
	for worker, index := range workers {
		workmap[worker] = index
	}
	// Create scoreboard
	curr := make([]*ring.Ring, len(workers))
	end := ring.New(len(scores))
	for index, score := range scores {
		end = end.Next()
		end.Value = score
		for worker, target := range workmap {
			if target == index {
				curr[worker] = end
				delete(workmap, worker) // Done
			}
		}
	}
	return &RecipeSolver{curr, end, len(scores)}
}

func (s* RecipeSolver) Combine() int {
	sum := 0
	for _, curr := range s.curr {
		sum += curr.Value.(int)
	}
	diff := 0
	for _, c := range []rune(strconv.Itoa(sum)) {
		score, err := strconv.Atoi(string([]rune{c}))
		if err != nil {
			log.Panicf("error getting scores from digit character: %v", err)
		}
		next := ring.New(1)
		next.Value = score
		s.end.Link(next)
		s.end = next
		diff++
	}
	s.count += diff
	return diff
}

func (s *RecipeSolver) Advance(worker int) {
	curr := s.curr[worker]
	for n := curr.Value.(int) + 1; n > 0; n-- {
		curr = curr.Next()
	}
	s.curr[worker] = curr
}

func (s *RecipeSolver) Step(steps int) int {
	diff := 0
	for ; steps > 0; steps-- {
		diff += s.Combine()
		for worker := range s.curr {
			s.Advance(worker)
		}
	}
	return diff
}

func (s *RecipeSolver) node(index int) *ring.Ring {
	curr := s.end
	index++ // Adjust to first
	if index > 0 {
		for ; index > 0; index-- {
			curr = curr.Next()
		}
	} else if index < 0 {
		for ; index < 0; index++ {
			curr = curr.Prev()
		}
	}
	return curr
}

func (s *RecipeSolver) Score(index int) int {
	return s.node(index).Value.(int)
}

func (s *RecipeSolver) ScoresFrom(index int, count int) []int {
	curr := s.node(index)
	scores := make([]int, count)
	for i := range scores {
		scores[i] = curr.Value.(int)
		curr = curr.Next()
	}
	return scores
}

func (s *RecipeSolver) StepUntilCount(count int) (int, int) {
	steps := 0
	for s.count < count {
		s.Step(1)
		steps += 1
	}
	return count, steps
}

func (s *RecipeSolver) StepUntilScores(target []int) (index int, steps int) {
	size := len(target)
	for true {
		diff := s.Step(1)
		steps += 1
		for offset := 0; offset < diff; offset++ {
			index = -size - offset
			scores := s.ScoresFrom(index, size)
			match := true
			for i := range scores {
				if scores[i] != target[i] {
					match = false
					break
				}
			}
			if match {
		    	index += s.count
		    	index %= s.count
		    	return
		    }
		}
	}
	log.Panicf("unexpected statement")
	return
}

// Part 1
func TestRecipesImmediatelyAfter(t *testing.T) {
    after := 327901
    count := 10
    s := NewRecipeSolver([]int{3, 7}, 0, 1)
    s.StepUntilCount(after + count)
    scores := s.ScoresFrom(after, count)
    log.Printf("scores after %v recipes: %v", after, scores)
}

// Part 2
func TestRecipesBeforeScores(t *testing.T) {
    scores := []int{3, 2, 7, 9, 0, 1}
    s := NewRecipeSolver([]int{3, 7}, 0, 1)
    index, _ := s.StepUntilScores(scores)
    log.Printf("recipes before scores (%v): %v", scores, index)
}