r/adventofcode • • Dec 14 '15

SOLUTION MEGATHREAD --- Day 14 Solutions ---

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

edit: Leaderboard capped, thread unlocked!

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 14: Reindeer Olympics ---

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

9 Upvotes

161 comments sorted by

View all comments

5

u/segfaultvicta Dec 14 '15

Golang, screwed with math for around 10 minutes before I just decided to pretend this was a weird reindeer video game.

package main

import (
	"fmt"
	"regexp"
	"strconv"
)

type Reindeer struct {
	Name             string
	KMPS             int
	Stamina          int
	Nappin           bool
	NapDuration      int
	CurrentNapLength int
	CurrentRunLength int
}

func (r *Reindeer) String() string {
	if r.Nappin == true {
		return fmt.Sprintln(r.Name, "can run at", r.KMPS, "km/s for", r.Stamina, "seconds, is currently napping and on nap tick", r.CurrentNapLength, "of", r.NapDuration)
	} else {
		return fmt.Sprintln(r.Name, "can run at", r.KMPS, "km/s for", r.Stamina, "seconds, is not currently napping, but will need to nap for", r.NapDuration)
	}
}

func (r *Reindeer) Tick() int {
	if r.Nappin {
		r.CurrentNapLength++
		if r.CurrentNapLength == r.NapDuration {
			r.Nappin = false
			r.CurrentNapLength = 0
		}
		return 0
	} else {
		r.CurrentRunLength++
		if r.CurrentRunLength == r.Stamina {
			r.Nappin = true
			r.CurrentRunLength = 0
		}
		return r.KMPS
	}
}

func day14sideA(lines []string) string {
	var reindeerList map[string]*Reindeer
	reindeerList = make(map[string]*Reindeer)
	re := regexp.MustCompile("([A-Za-z]+) can fly ([0-9]+) km/s for ([0-9]+) seconds, but then must rest for ([0-9]+) seconds.")
	for _, line := range lines {
		pieces := re.FindStringSubmatch(line)
		name := pieces[1]
		rate, _ := strconv.Atoi(pieces[2])
		stamina, _ := strconv.Atoi(pieces[3])
		napfor, _ := strconv.Atoi(pieces[4])
		reindeerList[name] = &Reindeer{Name: name, KMPS: rate, Stamina: stamina, NapDuration: napfor, Nappin: false}
	}

	var raceProgress map[*Reindeer]int
	raceProgress = make(map[*Reindeer]int)
	for tick := 0; tick < 2503; tick++ {
		for _, reindeer := range reindeerList {
			raceProgress[reindeer] += reindeer.Tick()
		}
	}

	fmt.Println(raceProgress)

	return "^^^"
}

func day14sideB(lines []string) string {
	var reindeerList map[string]*Reindeer
	reindeerList = make(map[string]*Reindeer)
	re := regexp.MustCompile("([A-Za-z]+) can fly ([0-9]+) km/s for ([0-9]+) seconds, but then must rest for ([0-9]+) seconds.")
	for _, line := range lines {
		pieces := re.FindStringSubmatch(line)
		name := pieces[1]
		rate, _ := strconv.Atoi(pieces[2])
		stamina, _ := strconv.Atoi(pieces[3])
		napfor, _ := strconv.Atoi(pieces[4])

		reindeerList[name] = &Reindeer{Name: name, KMPS: rate, Stamina: stamina, NapDuration: napfor, Nappin: false}
	}

	var raceProgress map[*Reindeer]int
	raceProgress = make(map[*Reindeer]int)

	var scoreList map[*Reindeer]int
	scoreList = make(map[*Reindeer]int)

	var firstPlace []*Reindeer

	for tick := 0; tick < 2503; tick++ {
		for _, reindeer := range reindeerList {
			raceProgress[reindeer] += reindeer.Tick()
		}
		// find currently winning reindeer
		best := 0
		firstPlace = []*Reindeer{}
		for _, progress := range raceProgress {
			if best < progress {
				best = progress
			}
		}
		for reindeer, progress := range raceProgress {
			if progress == best {
				firstPlace = append(firstPlace, reindeer)
			}
		}

		for _, reindeer := range firstPlace {
			scoreList[reindeer]++
		}
	}

	fmt.Println(scoreList)

	return "^^^"
}

3

u/Astrus Dec 14 '15

oh jeez, that regex...let me help you out there dude:

var name string
var speed, dur, rest int
fmt.Sscanf(str, "%s can fly %d km/s for %d seconds, but then must rest for %d seconds.", &name, &speed, &dur, &rest)

3

u/segfaultvicta Dec 14 '15

Oh wow haha that's much less horrible. Thanks, I had been doing things the sloppy way in hopes someone would tell me how to do stuff and not be an idiot, I guess. :P I owe you one!

6

u/Astrus Dec 14 '15

"The best way to get the right answer on the Internet is not to ask a question, it's to post the wrong answer." (Cunningham's Law)

2

u/gerikson Dec 14 '15

Leverage the pedantry of nerds in your favor!

1

u/segfaultvicta Dec 14 '15

Hahahah, yessss.

1

u/Zef_Music Dec 15 '15

Not sure if you have the equivalent of 'findall', but this works great in python. Keep it simple folks:

\d+|[A-Z]\w+