about summary refs log tree commit diff
path: root/Day11.cs
blob: 1c1e2705ec57d0361b19683ecc859acef91b31d5 (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
using aoc2019.lib;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

namespace aoc2019
{
    internal class Day11 : Day
    {
        public override int DayNumber => 11;

        private IntCodeVM vm;
        List<List<bool>> paintmap;
        long x, y;
        Direction heading;

        public Day11()
        {
            vm = new IntCodeVM(Input.First());
            paintmap = new List<List<bool>>();
            x = 0; y = 0;
            heading = Direction.Up;
        }

        enum Direction
        {
            Up, Down, Left, Right
        }

        private (long, long) DxDy()
        {
            return heading switch
            {
                Direction.Up => (0, 1),
                Direction.Down => (0, -1),
                Direction.Left => (-1, 0),
                Direction.Right => (1, 0)
            };
        }

        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;
            }
        }

        public override string Part1()
        {
            vm.Reset();
            vm.Run();
            var output = vm.output.ToList();
            long dx, dy;
            for (var i = 0; i < output.Count; i += 2)
            {
                long color = output[i];
                Turn(output[i + 1]);
                paintmap[x][y] = color == 0;
                (dx, dy) = DxDy();
                x += dx; y += dy;
            }
            return $"{paintmap.Count(x => x != null)}";
        }

        public override string Part2()
        {
            return "";
        }
    }
}