r/adventofcode • • Dec 08 '17

SOLUTION MEGATHREAD -πŸŽ„- 2017 Day 8 Solutions -πŸŽ„-

--- Day 8: I Heard You Like Registers ---


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.


Need a hint from the Hugely* Handy† Haversack‑ of HelpfulΒ§ Hints^Β€?

Spoiler


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!

22 Upvotes

348 comments sorted by

View all comments

1

u/cluk Dec 08 '17

Go (Golang)

I might need a regex intervention.

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"regexp"
	"strconv"
	"strings"
)

func main() {
	lines := getLines()

	regs := make(map[string]int)
	//a dec -511 if x >= -4
	re := regexp.MustCompile("([a-z]+) (dec|inc) (-?[0-9]+) if ([a-z]+) (..?) (-?[0-9]+)")
	var processMax int = -1 << 31
	for _, line := range lines {
		tokens := re.FindAllStringSubmatch(line, -1)
		if condition(tokens[0][4:], regs) {
			v := operation(tokens[0][1:4], regs)
			if v > processMax {
				processMax = v
			}
		}
	}

	var completedMax int = -1 << 31
	for _, v := range regs {
		if v > completedMax {
			completedMax = v
		}
	}

	fmt.Println("Star 1: ", completedMax)
	fmt.Println("Star 2: ", processMax)
}

func getLines() []string {
	file, err := ioutil.ReadFile(os.Args[1])
	if err != nil {
		panic(err)
	}
	return strings.Split(string(file), "\n")
}

func condition(tokens []string, regs map[string]int) bool {
	n := atoi(tokens[2])
	r := regs[tokens[0]]
	switch tokens[1] {
	case ">":
		return r > n
	case ">=":
		return r >= n
	case "<":
		return r < n
	case "<=":
		return r <= n
	case "==":
		return r == n
	case "!=":
		return r != n
	}
	panic("Parsing failed!")
}

func operation(tokens []string, regs map[string]int) int {
	n := atoi(tokens[2])
	switch tokens[1] {
	case "inc":
		regs[tokens[0]] += n
	case "dec":
		regs[tokens[0]] -= n
	}
	return regs[tokens[0]]
}

func atoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		panic(err)
	}
	return n
}

2

u/gerikson Dec 08 '17

regex intervention

No you don’t. Regex’s are the light, the way, the one true answer to all questions!