Guest User

Day 11 Part 2 (TypeScript)

a guest
Dec 11th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Intcode } from "../intcode/intcode";
  2. import { Store } from "../intcode/store";
  3. import { handleByLine, colorize, Colors } from "../utils";
  4.  
  5. enum Color {
  6.   Black = 0,
  7.   White = 1
  8. }
  9.  
  10. enum Turn {
  11.   Left = 0,
  12.   Right = 1
  13. }
  14.  
  15. enum Direction {
  16.   Up,
  17.   Right,
  18.   Down,
  19.   Left
  20. }
  21.  
  22. class Painter {
  23.   private readonly initialPosition = `0,0`;
  24.   private currentPosition: string = this.initialPosition;
  25.   private currentDirection: Direction = Direction.Up;
  26.   private positions: { [position: string]: Color } = {
  27.     [this.initialPosition]: Color.White
  28.   };
  29.   constructor(private intcode: Intcode) {}
  30.  
  31.   run() {
  32.     while (true) {
  33.       let input = this.getColor(this.currentPosition);
  34.       this.paintCurrentPosition(this.intcode.run(input).output);
  35.       this.turn(this.intcode.run(input).output);
  36.       this.move(1);
  37.  
  38.       if (this.intcode.isTerminated()) {
  39.         break;
  40.       }
  41.     }
  42.     this.visualize();
  43.   }
  44.  
  45.   getColor(position: string): Color {
  46.     return this.positions[position] || Color.Black;
  47.   }
  48.  
  49.   paintCurrentPosition(color: Color) {
  50.     this.positions[this.currentPosition] = color;
  51.   }
  52.  
  53.   move(steps: number) {
  54.     let [x, y] = this.currentPosition.split(",").map(p => parseInt(p));
  55.     switch (this.currentDirection) {
  56.       case Direction.Up:
  57.         y += steps;
  58.         break;
  59.       case Direction.Right:
  60.         x += steps;
  61.         break;
  62.       case Direction.Down:
  63.         y -= steps;
  64.         break;
  65.       case Direction.Left:
  66.         x -= steps;
  67.         break;
  68.     }
  69.     this.currentPosition = `${x},${y}`;
  70.   }
  71.  
  72.   turn(direction: Turn) {
  73.     switch (this.currentDirection) {
  74.       case Direction.Up:
  75.         this.currentDirection =
  76.           direction === Turn.Left ? Direction.Left : Direction.Right;
  77.         break;
  78.       case Direction.Right:
  79.         this.currentDirection =
  80.           direction === Turn.Left ? Direction.Up : Direction.Down;
  81.         break;
  82.       case Direction.Down:
  83.         this.currentDirection =
  84.           direction === Turn.Left ? Direction.Right : Direction.Left;
  85.         break;
  86.       case Direction.Left:
  87.         this.currentDirection =
  88.           direction === Turn.Left ? Direction.Down : Direction.Up;
  89.         break;
  90.     }
  91.   }
  92.  
  93.   visualize() {
  94.     //TODO: Possible optimization: consolidate loops
  95.     const coords = Object.keys(this.positions).map(p => {
  96.       const [x, y] = p.split(",").map(p => parseInt(p));
  97.       return { x, y };
  98.     });
  99.     const minX = Math.min(...coords.map(c => c.x));
  100.     const maxX = Math.max(...coords.map(c => c.x));
  101.     const minY = Math.min(...coords.map(c => c.y));
  102.     const maxY = Math.max(...coords.map(c => c.y));
  103.  
  104.     const padding = 2;
  105.     for (let y = maxY + padding; y >= minY - padding; y--) {
  106.       for (let x = minX - padding; x <= maxX + padding; x++) {
  107.         let color = this.getColor(`${x},${y}`);
  108.         let output = colorize(
  109.           color === Color.Black ? Colors.FgBlack : Colors.FgWhite,
  110.           "██"
  111.         );
  112.         process.stdout.write(output);
  113.       }
  114.       process.stdout.write("\n");
  115.     }
  116.   }
  117. }
  118.  
  119. handleByLine(`data.txt`, line => {
  120.   const intcode = new Intcode(new Store(line.split(",")));
  121.   const painter = new Painter(intcode);
  122.   painter.run();
  123. });
Add Comment
Please, Sign In to add comment