~lh2/adventofcode18

eec8d12bd3333d62e53a8812705771107b75f462 — Lukas Henkel 5 years ago de220f6
Day 21 Task 1
2 files changed, 50 insertions(+), 0 deletions(-)

A d21/main.go
A d21/op.go
A d21/main.go => d21/main.go +49 -0
@@ 0,0 1,49 @@
package main

import (
	"strconv"
	"strings"
)

type Instruction struct {
	Name string
	Args [3]int
}

type Register [6]int

func parseInstructions(in chan string) (is []Instruction, ipIndex int) {
	is = make([]Instruction, 0)
	for line := range in {
		parts := strings.Split(line, " ")
		if parts[0] == "#ip" {
			ipIndex = mustAtoi(parts[1])
			continue
		}
		is = append(is, Instruction{
			Name: parts[0],
			Args: [3]int{
				mustAtoi(parts[1]),
				mustAtoi(parts[2]),
				mustAtoi(parts[3]),
			},
		})
	}
	return
}

func task1(in chan string) string {
	is, ip := parseInstructions(in)
	r := Register{}
	var idx int
	for {
		idx = r[ip]
		if idx == 28 || idx > len(is) {
			break
		}
		a := is[idx].Args
		r = AllOps[is[idx].Name](r, a[0], a[1], a[2])
		r[ip]++
	}
	return strconv.Itoa(r[2])
}

A d21/op.go => d21/op.go +1 -0
@@ 0,0 1,1 @@
../d16/op.go
\ No newline at end of file