2017 Day 08 Complete!

This commit is contained in:
Brian Buller 2017-12-08 06:37:19 -06:00
parent 287c764399
commit a27cbca52d
3 changed files with 1166 additions and 0 deletions

106
2017/day08/day08.go Normal file
View File

@ -0,0 +1,106 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
const (
// Conditions
EQL = "=="
NEQ = "!="
GTN = ">"
LTN = "<"
GEQ = ">="
LEQ = "<="
// Operations
DEC = "dec"
INC = "inc"
)
var registers map[string]int
func main() {
registers = make(map[string]int)
inp := StdinToStrings()
allocMem := 0
for i := range inp {
pts := strings.Split(inp[i], " ")
var condMet bool
switch pts[5] {
case EQL:
condMet = getReg(pts[4]) == Atoi(pts[6])
case NEQ:
condMet = getReg(pts[4]) != Atoi(pts[6])
case GTN:
condMet = getReg(pts[4]) > Atoi(pts[6])
case LTN:
condMet = getReg(pts[4]) < Atoi(pts[6])
case GEQ:
condMet = getReg(pts[4]) >= Atoi(pts[6])
case LEQ:
condMet = getReg(pts[4]) <= Atoi(pts[6])
}
if condMet {
doOp(pts[0], pts[1], Atoi(pts[2]))
}
if getReg(pts[4]) > allocMem {
allocMem = getReg(pts[4])
}
}
var hReg string
var hVal int
for k, v := range registers {
if v > hVal {
hReg = k
hVal = v
}
}
fmt.Println(hReg, ":", hVal)
fmt.Println("Needed Memory:", allocMem)
}
func doOp(reg, op string, val int) {
st := getReg(reg)
switch op {
case DEC:
st -= val
case INC:
st += val
}
setReg(reg, st)
}
func getReg(nm string) int {
if _, ok := registers[nm]; !ok {
registers[nm] = 0
}
return registers[nm]
}
func setReg(nm string, val int) {
registers[nm] = val
}
func StdinToStrings() []string {
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}
func Atoi(i string) int {
var ret int
var err error
if ret, err = strconv.Atoi(i); err != nil {
log.Fatal("Invalid Atoi")
}
return ret
}

1000
2017/day08/input Normal file

File diff suppressed because it is too large Load Diff

60
2017/day08/problem Normal file
View File

@ -0,0 +1,60 @@
Advent of Code
--- Day 8: I Heard You Like Registers ---
You receive a signal directly from the CPU. Because of your recent assistance with jump instructions, it would like you to compute the result of a series of unusual register instructions.
Each instruction consists of several parts: the register to modify, whether to increase or decrease that register's value, the amount by which to increase or decrease it, and a condition. If the condition
fails, skip the instruction without modifying the register. The registers all start at 0. The instructions look like this:
b inc 5 if a > 1
a inc 1 if b < 5
c dec -10 if a >= 1
c inc -20 if c == 10
These instructions would be processed as follows:
* Because a starts at 0, it is not greater than 1, and so b is not modified.
* a is increased by 1 (to 1) because b is less than 5 (it is 0).
* c is decreased by -10 (to 10) because a is now greater than or equal to 1 (it is 1).
* c is increased by -20 (to -10) because c is equal to 10.
After this process, the largest value in any register is 1.
You might also encounter <= (less than or equal to) or != (not equal to). However, the CPU doesn't have the bandwidth to tell you what all the registers are named, and leaves that to you to determine.
What is the largest value in any register after completing the instructions in your puzzle input?
Your puzzle answer was ____.
--- Part Two ---
To be safe, the CPU also needs to know the highest value held in any register during this process so that it can decide how much memory to allocate to these operations. For example, in the above
instructions, the highest value ever held was 10 (in register c after the third instruction was evaluated).
Your puzzle answer was ____.
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your advent calendar and try another puzzle.
If you still want to see it, you can get your puzzle input.
References
Visible links
. http://adventofcode.com/
. http://adventofcode.com/2017/about
. http://adventofcode.com/2017/support
. http://adventofcode.com/2017/events
. http://adventofcode.com/2017/settings
. http://adventofcode.com/2017/auth/logout
. http://adventofcode.com/2017
. http://adventofcode.com/2017
. http://adventofcode.com/2017/leaderboard
. http://adventofcode.com/2017/stats
. http://adventofcode.com/2017/sponsors
. http://adventofcode.com/2017/sponsors
. http://adventofcode.com/2017/day/5
. http://adventofcode.com/2017
. http://adventofcode.com/2017/day/8/input