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.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/csmith/aoc-2019/common"
  5. "github.com/csmith/aoc-2019/intcode"
  6. "math"
  7. )
  8. type point struct {
  9. x int
  10. y int
  11. }
  12. var dirs = [4]point{
  13. 0: {y: -1},
  14. 1: {x: +1},
  15. 2: {y: +1},
  16. 3: {x: -1},
  17. }
  18. func run(memory []int, startWhite bool) (int, map[point]bool) {
  19. input := make([]int, len(memory))
  20. copy(input, memory)
  21. vm := intcode.NewVirtualMachine(input)
  22. vm.Input = make(chan int, 1)
  23. vm.Output = make(chan int, 1)
  24. go vm.Run()
  25. grid := make(map[point]bool, 50)
  26. paintCount := 0
  27. pos := point{x: 0, y: 0}
  28. dir := 0
  29. colour := startWhite
  30. for {
  31. if colour {
  32. vm.Input <- 1
  33. } else {
  34. vm.Input <- 0
  35. }
  36. paint, more := <-vm.Output
  37. if !more {
  38. break
  39. }
  40. turn := <-vm.Output
  41. if _, ok := grid[pos]; !ok {
  42. paintCount++
  43. }
  44. grid[pos] = paint == 1
  45. dir = (4 + dir + 2*turn - 1) % 4
  46. pos.x += dirs[dir].x
  47. pos.y += dirs[dir].y
  48. colour = grid[pos]
  49. }
  50. return paintCount, grid
  51. }
  52. func main() {
  53. input := common.ReadCsvAsInts("11/input.txt")
  54. paintCount, _ := run(input, false)
  55. _, grid := run(input, true)
  56. fmt.Println(paintCount)
  57. minX, minY := math.MaxInt64, math.MaxInt64
  58. maxX, maxY := math.MinInt64, math.MinInt64
  59. for pos := range grid {
  60. minX, maxX = common.Min(minX, pos.x), common.Max(maxX, pos.x)
  61. minY, maxY = common.Min(minY, pos.y), common.Max(maxY, pos.y)
  62. }
  63. for y := minY; y <= maxY; y++ {
  64. for x := minX; x <= maxX; x++ {
  65. if grid[point{x: x, y: y}] {
  66. fmt.Print("█")
  67. } else {
  68. fmt.Print(" ")
  69. }
  70. }
  71. fmt.Println()
  72. }
  73. }