Created
December 15, 2019 13:10
-
-
Save Rudyzio/619056c00e6b24ff8d6a7024cad6ce20 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Common; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Day_15_Solver | |
{ | |
public static class Day15Solver | |
{ | |
public static long Part1Solution(long[] input) | |
{ | |
IntCodeProgram intCodeProgram = new IntCodeProgram(input); | |
Queue<Droid> droids = new Queue<Droid>(); | |
List<Position> exploredPositions = new List<Position>(); | |
Position currentPosition = new Position(0, 0, Answer.Allowed); | |
exploredPositions.Add(currentPosition); | |
long distance = 0; | |
droids.Enqueue(new Droid(currentPosition, 0, Command.North)); | |
droids.Enqueue(new Droid(currentPosition, 0, Command.South)); | |
droids.Enqueue(new Droid(currentPosition, 0, Command.West)); | |
droids.Enqueue(new Droid(currentPosition, 0, Command.East)); | |
while (droids.Count > 0) | |
{ | |
var currentDroid = droids.Dequeue(); | |
var command = currentDroid.Command; | |
intCodeProgram.Input.Enqueue((long)command); | |
intCodeProgram.Run(); | |
var answer = (Answer)intCodeProgram.Output.Dequeue(); | |
switch (answer) | |
{ | |
case Answer.Wall: | |
Position newPositiona = null; | |
switch (command) | |
{ | |
case Command.North: | |
newPositiona = new Position(currentDroid.CurrentPosition.X, currentDroid.CurrentPosition.Y + 1, Answer.Wall); | |
if (!exploredPositions.Any(pos => pos.X == newPositiona.X && pos.Y == newPositiona.Y)) | |
{ | |
exploredPositions.Add(newPositiona); | |
} | |
break; | |
case Command.East: | |
newPositiona = new Position(currentDroid.CurrentPosition.X + 1, currentDroid.CurrentPosition.Y, Answer.Wall); | |
if (!exploredPositions.Any(pos => pos.X == newPositiona.X && pos.Y == newPositiona.Y)) | |
{ | |
exploredPositions.Add(newPositiona); | |
} | |
break; | |
case Command.West: | |
newPositiona = new Position(currentDroid.CurrentPosition.X - 1, currentDroid.CurrentPosition.Y, Answer.Wall); | |
if (!exploredPositions.Any(pos => pos.X == newPositiona.X && pos.Y == newPositiona.Y)) | |
{ | |
exploredPositions.Add(newPositiona); | |
} | |
break; | |
case Command.South: | |
newPositiona = new Position(currentDroid.CurrentPosition.X, currentDroid.CurrentPosition.Y - 1, Answer.Wall); | |
if (!exploredPositions.Any(pos => pos.X == newPositiona.X && pos.Y == newPositiona.Y)) | |
{ | |
exploredPositions.Add(newPositiona); | |
} | |
break; | |
} | |
break; | |
case Answer.Allowed: | |
Position newPosition = null; | |
switch (command) | |
{ | |
case Command.North: | |
newPosition = new Position(currentDroid.CurrentPosition.X, currentDroid.CurrentPosition.Y + 1, Answer.Allowed); | |
if (!exploredPositions.Any(pos => pos.X == newPosition.X && pos.Y == newPosition.Y)) | |
{ | |
droids.Enqueue(new Droid(newPosition, currentDroid.Distance + 1, Command.North)); | |
droids.Enqueue(new Droid(newPosition, currentDroid.Distance + 1, Command.East)); | |
droids.Enqueue(new Droid(newPosition, currentDroid.Distance + 1, Command.West)); | |
exploredPositions.Add(newPosition); | |
} | |
break; | |
case Command.East: | |
newPosition = new Position(currentDroid.CurrentPosition.X + 1, currentDroid.CurrentPosition.Y, Answer.Allowed); | |
if (!exploredPositions.Any(pos => pos.X == newPosition.X && pos.Y == newPosition.Y)) | |
{ | |
droids.Enqueue(new Droid(newPosition, currentDroid.Distance + 1, Command.North)); | |
droids.Enqueue(new Droid(newPosition, currentDroid.Distance + 1, Command.East)); | |
droids.Enqueue(new Droid(newPosition, currentDroid.Distance + 1, Command.South)); | |
exploredPositions.Add(newPosition); | |
} | |
break; | |
case Command.West: | |
newPosition = new Position(currentDroid.CurrentPosition.X - 1, currentDroid.CurrentPosition.Y, Answer.Allowed); | |
if (!exploredPositions.Any(pos => pos.X == newPosition.X && pos.Y == newPosition.Y)) | |
{ | |
droids.Enqueue(new Droid(newPosition, currentDroid.Distance + 1, Command.North)); | |
droids.Enqueue(new Droid(newPosition, currentDroid.Distance + 1, Command.West)); | |
droids.Enqueue(new Droid(newPosition, currentDroid.Distance + 1, Command.South)); | |
exploredPositions.Add(newPosition); | |
} | |
break; | |
case Command.South: | |
newPosition = new Position(currentDroid.CurrentPosition.X, currentDroid.CurrentPosition.Y - 1, Answer.Allowed); | |
if (!exploredPositions.Any(pos => pos.X == newPosition.X && pos.Y == newPosition.Y)) | |
{ | |
droids.Enqueue(new Droid(newPosition, currentDroid.Distance + 1, Command.West)); | |
droids.Enqueue(new Droid(newPosition, currentDroid.Distance + 1, Command.East)); | |
droids.Enqueue(new Droid(newPosition, currentDroid.Distance + 1, Command.South)); | |
exploredPositions.Add(newPosition); | |
} | |
break; | |
} | |
break; | |
case Answer.Success: | |
distance = currentDroid.Distance; | |
break; | |
} | |
} | |
return distance; | |
} | |
public static int Part2Solution(long[] input) | |
{ | |
return 0; | |
} | |
} | |
public class Droid | |
{ | |
public Position CurrentPosition { get; } | |
public long Distance { get; } | |
public Command Command { get; } | |
public Droid(Position currentPosition, long distance, Command command) | |
{ | |
CurrentPosition = currentPosition; | |
Distance = distance; | |
Command = command; | |
} | |
} | |
public class Position | |
{ | |
public Position(int x, int y, Answer whatHas) | |
{ | |
X = x; | |
Y = y; | |
WhatHas = whatHas; | |
} | |
public int X { get; set; } | |
public int Y { get; set; } | |
public Answer WhatHas { get; set; } | |
} | |
public enum Command | |
{ | |
North = 1, | |
South = 2, | |
West = 3, | |
East = 4 | |
} | |
public enum Answer | |
{ | |
Wall = 0, | |
Allowed = 1, | |
Success = 2 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment