about summary refs log tree commit diff
path: root/aoc2019/Day15.cs
blob: 046f52f346492d7287e20c802420230dbf36c15c (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
namespace aoc2019;

public sealed class Day15 : Day
{
    private readonly bool verbose = false;
    private readonly IntCodeVM vm;

    public Day15() : base(15, "Oxygen System")
    {
        vm = new IntCodeVM(Input.First());
    }

    public override string Part1()
    {
        vm.Reset();
        var currentLocation = new Location(0, 0);
        var halt = IntCodeVM.HaltType.Waiting;
        while (halt == IntCodeVM.HaltType.Waiting)
        {
            var direction = currentLocation!.NextDirection();
            if (direction <= 4)
            {
                var (x, y) = currentLocation.Neighbor(direction);
                if (Location.GetLocation(x, y) == null)
                {
                    halt = vm.Run(direction);
                    currentLocation = vm.Result switch
                    {
                        Location.Wall   => new Location(x, y, Location.Opposites[direction], Location.Wall),
                        Location.Empty  => new Location(x, y, Location.Opposites[direction]),
                        Location.System => new Location(x, y, Location.Opposites[direction], Location.System),
                        _ => throw new Exception($"Unknown IntCodeVM response: {vm.Result}"),
                    };
                }
            }
            else
            {
                direction = currentLocation.PreviousDirection;
                if (direction > 0)
                {
                    halt = vm.Run(direction);
                    currentLocation = vm.Result switch
                    {
                        Location.Empty or Location.System => Location.GetLocation(currentLocation.Neighbor(direction)),
                        _ => throw new Exception($"Unknown or unexpected response for previous room: {vm.Result}"),
                    };
                }
                else
                {
                    if (verbose)
                    {
                        // find extents of canvas
                        int xMin, xMax, yMin, yMax;
                        xMin = yMin = int.MaxValue;
                        xMax = yMax = int.MinValue;
                        foreach (var (x, y) in Location.AllLocations.Keys)
                        {
                            if (x < xMin) xMin = x;
                            if (x > xMax) xMax = x;
                            if (y < yMin) yMin = y;
                            if (y > yMax) yMax = y;
                        }

                        Console.WriteLine($"Canvas extends from ({xMin}, {yMin}) to ({xMax}, {yMax})");

                        // print board
                        for (var y = yMin; y <= yMax; y++)
                        {
                            var line = "";
                            for (var x = xMin; x <= xMax; x++)
                                if (Location.AllLocations.ContainsKey((x, y)))
                                    line += Location.AllLocations[(x, y)].Image();
                                else
                                    line += "@";

                            Console.WriteLine(line);
                        }
                    }

                    currentLocation = Location.OxygenLocation;
                    var distance = 0;
                    while (currentLocation?.PreviousDirection != 0)
                    {
                        distance++;
                        currentLocation = Location.GetLocation(currentLocation!.PreviousLocation());
                    }

                    return $"{distance}";
                }
            }
        }

        return "";
    }

    public override string Part2()
    {
        var changed = true;
        while (changed)
        {
            changed = false;
            foreach (var location in Location.AllLocations.Values)
                changed = location.UpdateDistanceToOxygenSystem() || changed;
        }

        return Location.AllLocations.Values
            .Where(l => !l.IsWall)
            .Max(l => l.DistanceToOxygenSystem)
            .ToString();
    }

    private class Location
    {
        public const int Wall = 0;
        public const int Empty = 1;
        public const int System = 2;

        private static readonly int[] Dx = { 0, 0, 0, 1, -1 };
        private static readonly int[] Dy = { 0, 1, -1, 0, 0 };
        public static readonly int[] Opposites = { 0, 2, 1, 4, 3 };

        public static readonly Dictionary<(int x, int y), Location> AllLocations = new();

        private readonly int currentType;
        public int DistanceToOxygenSystem = int.MaxValue - 1;

        private int searchDirection = 1;

        public Location(int x, int y, int prev = 0, int type = Empty)
        {
            PreviousDirection = prev;
            currentType = type;
            X = x;
            Y = y;

            if (type == System)
            {
                OxygenLocation = this;
                DistanceToOxygenSystem = 0;
                // Console.WriteLine($"Found Oxygen System at ({x}, {y})");
            }

            AllLocations.Add((x, y), this);
        }

        public static Location? OxygenLocation { get; private set; }
        public int PreviousDirection { get; }
        private int X { get; }
        private int Y { get; }

        public bool IsWall => currentType == Wall;

        public string Image()
        {
            return currentType switch
            {
                Wall => "\u2587",
                Empty => X == 0 && Y == 0 ? "S" : " ",
                System => "O",
                _ => "?"
            };
        }

        public bool UpdateDistanceToOxygenSystem()
        {
            if (currentType != Empty) return false;

            foreach (var direction in Enumerable.Range(1, 4))
            {
                var distance = GetLocation(Neighbor(direction))?.DistanceToOxygenSystem ?? int.MaxValue;
                if (distance + 1 < DistanceToOxygenSystem)
                {
                    DistanceToOxygenSystem = distance + 1;
                    return true;
                }
            }

            return false;
        }

        public (int, int) Neighbor(int direction)
        {
            return (X + Dx[direction], Y + Dy[direction]);
        }

        public (int, int) PreviousLocation()
        {
            return Neighbor(PreviousDirection);
        }

        public int NextDirection()
        {
            return searchDirection++;
        }

        public static Location? GetLocation(int x, int y)
        {
            return AllLocations.ContainsKey((x, y)) ? AllLocations[(x, y)] : null;
        }

        public static Location? GetLocation((int x, int y) coords)
        {
            return GetLocation(coords.x, coords.y);
        }
    }
}