my advent of code solutions
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 77 lines 2.2 kB view raw
1namespace Solutions._2019; 2 3/// <summary> 4/// Day 13: <a href="https://adventofcode.com/2019/day/13"/> 5/// </summary> 6public sealed class Day13CarePackage() : Day(2019, 13, "Care Package") 7{ 8 private IntCodeVM? _vm; 9 private readonly Dictionary<(long x, long y), long> _board = []; 10 private readonly List<(long x, long y)> _updatedCoordinates = []; 11 12 public override void ProcessInput() => 13 _vm = new(Input.First()); 14 15 private void PrintBoard() 16 { 17 var coords = _updatedCoordinates.Count != 0 ? _updatedCoordinates : _board.Keys.ToList(); 18 foreach (var (x, y) in coords) 19 { 20 if (x < 0 || y < 0) continue; 21 Console.SetCursorPosition((int)x, (int)y); 22 var value = _board[(x, y)]; 23 Console.Write(value switch 24 { 25 0 => " ", 26 1 => "█", 27 2 => "#", 28 3 => "_", 29 4 => ".", 30 _ => value, 31 }); 32 } 33 } 34 35 public override object Part1() 36 { 37 _vm!.Reset(); 38 _vm.Run(); 39 return _vm.Output.Where((v, i) => (i + 1) % 3 == 0 && v == 2).Count(); 40 } 41 42 public override object Part2() 43 { 44 _vm!.Reset(); 45 _vm.Memory[0] = 2; 46 var printBoard = false; 47 var gameTicks = 0; 48 if (printBoard) 49 { 50 Console.Clear(); 51 Console.CursorVisible = false; 52 } 53 54 var haltType = IntCodeVM.HaltType.Waiting; 55 while (haltType == IntCodeVM.HaltType.Waiting) 56 { 57 haltType = _vm.Run(); 58 if (printBoard) _updatedCoordinates.Clear(); 59 while (_vm.Output.Count != 0) 60 { 61 long x = _vm.Result, y = _vm.Result; 62 _board[(x, y)] = _vm.Result; 63 if (printBoard) _updatedCoordinates.Add((x, y)); 64 } 65 66 if (printBoard) PrintBoard(); 67 68 var (ball, _) = _board.Single(t => t.Value == 4).Key; 69 var (paddle, _) = _board.Single(t => t.Value == 3).Key; 70 _vm.AddInput(Math.Sign(ball.CompareTo(paddle))); 71 72 gameTicks++; 73 } 74 75 return $"after {gameTicks} moves, the score is: {_board[(-1, 0)]}"; 76 } 77}