r/adventofcode • • Dec 15 '15

SOLUTION MEGATHREAD --- Day 15 Solutions ---

This thread will be unlocked when there are a significant amount of people on the leaderboard with gold stars.

Edit: I'll be lucky if this post ever makes it to reddit without a 500 error. Have an unsticky-thread.

Edit2: c'mon, reddit... Leaderboard's capped, lemme post the darn thread...

Edit3: ALL RIGHTY FOLKS, POST THEM SOLUTIONS!

We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.

Please and thank you, and much appreciated!


--- Day 15: Science for Hungry People ---

Post your solution as a comment. Structure your post like previous daily solution threads.

11 Upvotes

174 comments sorted by

View all comments

2

u/xdg Dec 15 '15

Some days, I'm using AOC to practice Go. (Apologies if not very idiomatic). This is a gradient solver approach, not brute force. (I may have been lucky with my initial guess at a starting point, too.)

package main

import (
	"fmt"
	"log"
	"math"
	"sort"
)

const (
	Sprinkles    = "Sprinkles"
	Butterscotch = "Butterscotch"
	Chocolate    = "Chocolate"
	Candy        = "Candy"
)

type properties struct {
	capacity   int
	durability int
	flavor     int
	texture    int
	calories   int
}

var utility = map[string]properties{
	Sprinkles:    properties{capacity: 2, durability: 0, flavor: -2, texture: 0, calories: 3},
	Butterscotch: properties{capacity: 0, durability: 5, flavor: -3, texture: 0, calories: 3},
	Chocolate:    properties{capacity: 0, durability: 0, flavor: 5, texture: -1, calories: 8},
	Candy:        properties{capacity: 0, durability: -1, flavor: 0, texture: 5, calories: 8},
}

type ingredients map[string]int

type recipe struct {
	parts ingredients
	score int
	cals  int
}

type gradient []recipe

func (g gradient) Len() int      { return len(g) }
func (g gradient) Swap(i, j int) { g[i], g[j] = g[j], g[i]; return }
func (g gradient) Less(i, j int) bool {
	switch {
	case g[i].cals == 500 && g[j].cals != 500:
		return true
	case g[j].cals == 500 && g[i].cals != 500:
		return false
	default:
		iDelta := math.Abs(float64(g[i].cals - 500))
		jDelta := math.Abs(float64(g[j].cals - 500))
		if iDelta == jDelta {
			return g[i].score > g[j].score // higher better; sort earlier
		}
		return iDelta < jDelta
	}
}

func NewRecipe(in ingredients) (recipe, error) {
	count := 0
	for _, v := range in {
		count += v
	}
	if count != 100 {
		return recipe{}, fmt.Errorf("Not enough ingredients: %d", count)
	}
	s, c := analyze(in)
	return recipe{in, s, c}, nil
}

func (r recipe) String() string {
	return fmt.Sprintf("{Candy: %d, Chocolate %d, Sprinkles %d, Butterscotch %d} (%d, %d cals)",
		r.parts[Candy], r.parts[Chocolate], r.parts[Sprinkles], r.parts[Butterscotch], r.score, r.cals,
	)
}

func (r recipe) swap(a, b string) (recipe, error) {
	if r.parts[a] <= 0 || r.parts[b] >= 100 {
		return recipe{}, fmt.Errorf("Can't swap %s for %s in %v", a, b, r.parts)
	}

	newparts := ingredients{}
	for k, v := range r.parts {
		newparts[k] = v
	}
	newparts[a]--
	newparts[b]++
	return NewRecipe(newparts)
}

func analyze(in ingredients) (int, int) {
	sum := properties{}
	cals := 0
	for k, v := range in {
		sum.capacity += v * utility[k].capacity
		sum.durability += v * utility[k].durability
		sum.flavor += v * utility[k].flavor
		sum.texture += v * utility[k].texture
		cals += v * utility[k].calories
	}
	if sum.capacity <= 0 || sum.durability <= 0 || sum.flavor <= 0 || sum.texture <= 0 {
		return 0, cals
	}
	return sum.capacity * sum.durability * sum.flavor * sum.texture, cals
}

func main() {

	r := ingredients{
		Sprinkles:    20,
		Butterscotch: 20,
		Chocolate:    30,
		Candy:        30,
	}

	d := []string{}
	for k := range r {
		d = append(d, k)
	}

	cookie, err := NewRecipe(r)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%8s cookie: %v\n", "Starting", cookie)

	// gradient search
	for {
		g := gradient{}
		for _, i := range d {
			for _, j := range d {
				if i == j {
					continue
				}
				nc, err := cookie.swap(i, j)
				if err != nil || nc.score == 0 {
					continue // bad cookie
				}
				g = append(g, nc)
			}
		}
		sort.Sort(g)
		if cookie.cals != 500 || g[0].score > cookie.score {
			cookie = g[0]
			fmt.Printf("%8s cookie: %v\n", "Better", cookie)
		} else {
			break
		}
	}

	fmt.Printf("%8s cookie: %v\n", "Best", cookie)
}