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 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package main
  2. import (
  3. "github.com/csmith/aoc-2019/common"
  4. "github.com/csmith/aoc-2019/intcode"
  5. )
  6. func patch(input []int, width int) {
  7. // Insert two quarters
  8. input[0] = 2
  9. // Find the bottom line of the play area and change it to wall
  10. for i := len(input) - 1; i > 0; i-- {
  11. if input[i] == 1 && input[i-width] == 1 {
  12. allEmpty := true
  13. for j := i - width + 1; j < i-1; j++ {
  14. if input[j] != 0 {
  15. allEmpty = false
  16. break
  17. }
  18. }
  19. if allEmpty {
  20. for j := i - width + 1; j < i-1; j++ {
  21. input[j] = 1
  22. }
  23. break
  24. }
  25. }
  26. }
  27. }
  28. func feedInput(input chan int) {
  29. for {
  30. input <- 0
  31. }
  32. }
  33. func readOutput(output chan int, handler func(int, int, int)) {
  34. for {
  35. x, more := <-output
  36. if !more {
  37. break
  38. }
  39. y := <-output
  40. tile := <-output
  41. handler(x, y, tile)
  42. }
  43. }
  44. func countBlocks(screen [60][80]int) (blocks int) {
  45. for _, line := range screen {
  46. for _, pixel := range line {
  47. if pixel == 2 {
  48. blocks++
  49. }
  50. }
  51. }
  52. return
  53. }
  54. func run(input []int, handler func(int, int, int)) {
  55. memory := make([]int, len(input))
  56. copy(memory, input)
  57. vm := intcode.NewVirtualMachine(memory)
  58. vm.Input = make(chan int, 1)
  59. vm.Output = make(chan int, 6)
  60. go feedInput(vm.Input)
  61. go vm.Run()
  62. readOutput(vm.Output, handler)
  63. }
  64. func main() {
  65. input := common.ReadCsvAsInts("13/input.txt")
  66. memory := make([]int, len(input))
  67. copy(memory, input)
  68. width := 0
  69. var screen [60][80]int
  70. run(memory, func(x int, y int, tile int) {
  71. screen[y][x] = tile
  72. if x > width {
  73. width = x
  74. }
  75. })
  76. println(countBlocks(screen))
  77. patch(input, width)
  78. score := 0
  79. run(input, func(x int, y int, tile int) {
  80. if x == -1 {
  81. score = tile
  82. }
  83. })
  84. println(score)
  85. }