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 {