|
|
@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
using AdventOfCode.Models;
|
|
|
|
|
|
|
|
using AOC2021.Helper;
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace AdventOfCode._2022.Models
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
public class CargoCrateBoard
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
private readonly CrateBoard _board;
|
|
|
|
|
|
|
|
public CargoCrateBoard(AOCVersion version, string[] crates, string[] craneMoves)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_board = new CrateBoard(crates);
|
|
|
|
|
|
|
|
ProcessMoves(version, craneMoves);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<string> GetResults()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return _board.GetResults();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ProcessMoves(AOCVersion version, string[] moves)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
foreach (var move in moves)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
string numbersOnly = Regex.Replace(move, "[^0-9.]", " ");
|
|
|
|
|
|
|
|
var pendingMoves = Regex.Replace(numbersOnly, @"\s+", " ").Trim().Split(" ");
|
|
|
|
|
|
|
|
_board.Move(version, pendingMoves[0].ToInt(), pendingMoves[1].ToInt(), pendingMoves[2].ToInt());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CrateBoard
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
private List<Stack<Crate>> _crates;
|
|
|
|
|
|
|
|
public CrateBoard(string[] crates)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_crates = new List<Stack<Crate>>();
|
|
|
|
|
|
|
|
var boardSize = Regex.Replace(crates.Last(), @"\s+", " ").Trim().Split(" ").Count();
|
|
|
|
|
|
|
|
for (int i = 0; i < boardSize; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_crates.Add(new Stack<Crate>());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Fix input to handle spaces by replacing with empty crates
|
|
|
|
|
|
|
|
for (int i = (crates.Length - 2); i >=0; i--)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var crateRow = crates[i];
|
|
|
|
|
|
|
|
crateRow = crateRow.Replace("] ", "] [-]");
|
|
|
|
|
|
|
|
crateRow = crateRow.Replace(" [", "[-] [");
|
|
|
|
|
|
|
|
crateRow = crateRow.Replace(" ", " [-] ");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var cratesPerRow = crateRow.Split(" ");
|
|
|
|
|
|
|
|
if (cratesPerRow.Length != boardSize) throw new ArgumentException("Crates don't match board size");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int x = 0; x < cratesPerRow.Length; x++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!cratesPerRow[x].Equals("[-]"))
|
|
|
|
|
|
|
|
_crates[x].Push(new Crate(cratesPerRow[x]));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Move(AOCVersion version, int amount, int sourceStackId, int destinationStackId)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var sourceStack = _crates[(sourceStackId - 1)];
|
|
|
|
|
|
|
|
var destStack = _crates[(destinationStackId - 1)];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (version == AOCVersion.A)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
for (int i = 0; i < amount; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
destStack.Push(sourceStack.Pop());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var temp = new Stack<Crate>();
|
|
|
|
|
|
|
|
for (int i = 0; i < amount; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
temp.Push(sourceStack.Pop());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < amount; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
destStack.Push(temp.Pop());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<string> GetResults()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var list = new List<string>();
|
|
|
|
|
|
|
|
for (int i = 0; i < _crates.Count(); i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
list.Add(_crates[i].Peek().Name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Crate
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
|
|
|
public Crate(string crateName)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
this.Name = crateName;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|