107 lines
1.7 KiB
Go
107 lines
1.7 KiB
Go
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
|
|
}
|