From 4f48f24c74ffb1bc7ba6b185b2bbaec6fa776552 Mon Sep 17 00:00:00 2001 From: Alphyron Date: Tue, 3 Dec 2019 08:46:39 +0100 Subject: [PATCH] :sparkles: Finish part 2 of day 2 --- day2/main.go | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/day2/main.go b/day2/main.go index 740b137..eee53c4 100644 --- a/day2/main.go +++ b/day2/main.go @@ -32,12 +32,39 @@ func main() { } t2 = append(t2, j) } + wip := make([]int, len(t2)) // ========= Finish now, we can solve the task ====== - t2[1] = 12 - t2[2] = 2 - ParseArray(t2) + copy(wip, t2) + wip[1] = 12 + wip[2] = 2 + ParseArray(wip) + log.Println("Part1: " + strconv.Itoa(wip[0])) - log.Println("Part1: " + strconv.Itoa(t2[0])) + noun, verb := Brutforce(t2, 19690720) + + if noun == -1 || verb == -1 { + log.Fatal("Part2: No values found!") + } + + log.Println("Part2: noun=" + strconv.Itoa(noun) + " verb=" + strconv.Itoa(verb)) +} + +func Brutforce(list []int, des int) (int, int) { + wip := make([]int, len(list)) + copy(wip, list) + + for i := 0; i < 100; i++ { + for j := 0; j < 100; j++ { + copy(wip, list) + wip[1] = i + wip[2] = j + ParseArray(wip) + if wip[0] == des { + return i, j + } + } + } + return -1, -1 } func ParseArray(list []int) error {