You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.go 882B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/csmith/aoc-2019/common"
  5. "github.com/csmith/aoc-2019/intcode"
  6. "sync"
  7. )
  8. func run(pool *sync.Pool, input []int, noun, verb int) int {
  9. vm := pool.Get().(*intcode.VirtualMachine)
  10. defer pool.Put(vm)
  11. vm.Reset(input)
  12. vm.Memory[1] = noun
  13. vm.Memory[2] = verb
  14. vm.Run()
  15. return vm.Memory[0]
  16. }
  17. func findOutput(pool *sync.Pool, input []int, target int) int {
  18. res := make(chan int)
  19. for n := 0; n < 100; n++ {
  20. go func(n int) {
  21. for v := 0; v < 100; v++ {
  22. if run(pool, input, n, v) == target {
  23. res <- 100*n + v
  24. }
  25. }
  26. }(n)
  27. }
  28. return <-res
  29. }
  30. func main() {
  31. input := common.Atoi(common.ReadCommas("02/input.txt"))
  32. pool := &sync.Pool{
  33. New: func() interface{} {
  34. return intcode.NewVirtualMachine(make([]int, len(input)))
  35. },
  36. }
  37. fmt.Println(run(pool, input, 12, 2))
  38. fmt.Println(findOutput(pool, input, 19690720))
  39. }