about summary refs log tree commit diff
path: root/aoc2019/Day11.cs
blob: 31a5d882ef7ac30ee3e339a4efcd6cd0527db6b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System.Collections.Generic;
using System.Linq;
using aoc2019.lib;

namespace aoc2019
{
    public sealed class Day11 : Day
    {
        private readonly IntCodeVM vm;
        private Direction heading;
        private long x, y;

        public Day11() : base(11, "Space Police")
        {
            vm = new IntCodeVM(Input.First());
        }

        private void Move()
        {
            switch (heading)
            {
                case Direction.Up:
                    y++;
                    break;
                case Direction.Down:
                    y--;
                    break;
                case Direction.Left:
                    x--;
                    break;
                case Direction.Right:
                    x++;
                    break;
            }

            ;
        }

        private void Turn(long direction)
        {
            switch (heading)
            {
                case Direction.Up:
                    heading = direction == 0 ? Direction.Left : Direction.Right;
                    break;
                case Direction.Down:
                    heading = direction == 0 ? Direction.Right : Direction.Left;
                    break;
                case Direction.Left:
                    heading = direction == 0 ? Direction.Down : Direction.Up;
                    break;
                case Direction.Right:
                    heading = direction == 0 ? Direction.Up : Direction.Down;
                    break;
            }

            Move();
        }

        private Dictionary<(long x, long y), long> PaintShip(int initialVal)
        {
            var map = new Dictionary<(long, long), long>();
            vm.Reset();
            heading = Direction.Up;
            x = 0;
            y = 0;
            map[(x, y)] = initialVal;

            var haltType = IntCodeVM.HaltType.Waiting;
            while (haltType == IntCodeVM.HaltType.Waiting)
            {
                haltType = vm.Run(map.GetValueOrDefault((x, y)));
                map[(x, y)] = vm.Result;
                Turn(vm.Result);
            }

            return map;
        }

        public override string Part1()
        {
            return $"{PaintShip(0).Count}";
        }

        public override string Part2()
        {
            var map = PaintShip(1);
            var minX = (int) map.Keys.Select(x => x.x).Min();
            var maxX = (int) map.Keys.Select(x => x.x).Max();
            var minY = (int) map.Keys.Select(x => x.y).Min();
            var maxY = (int) map.Keys.Select(x => x.y).Max();

            return Enumerable.Range(minY, maxY - minY + 1)
                .Select(y =>
                    Enumerable.Range(minX, maxX - minX + 1)
                        .Select(x => map.GetValueOrDefault((x, y)) == 0 ? ' ' : '#')
                        .ToDelimitedString()
                )
                .Reverse()
                .ToDelimitedString("\n");
        }

        private enum Direction
        {
            Up,
            Down,
            Left,
            Right
        }
    }
}