~lh2/adventofcode18

294649dede0536a18f9355ffa2cde8f90a5d645a — Lukas Henkel 5 years ago eec8d12
Day 21 Task 2
1 files changed, 23 insertions(+), 0 deletions(-)

M d21/main.go
M d21/main.go => d21/main.go +23 -0
@@ 47,3 47,26 @@ func task1(in chan string) string {
	}
	return strconv.Itoa(r[2])
}

func task2(in chan string) string {
	is, ip := parseInstructions(in)
	r := Register{}
	var idx int
	resMap := make(map[int]bool)
	var res int
	for {
		idx = r[ip]
		if idx == 28 {
			v := r[2]
			if _, ok := resMap[v]; ok {
				break
			}
			resMap[v] = true
			res = v
		}
		a := is[idx].Args
		r = AllOps[is[idx].Name](r, a[0], a[1], a[2])
		r[ip]++
	}
	return strconv.Itoa(res)
}