foo
This commit is contained in:
1 parent
4fef30bfba
commit
bbd000a14f
6 files changed
+189
-2
No files matched your search
@@ -0,0 +1,99 @@
|
||||
namespace adventOfCode.day12;
|
||||
|
||||
|
||||
record Pin(int X, int Y);
|
||||
|
||||
class Block
|
||||
{
|
||||
private HashSet<Pin> Pins;
|
||||
|
||||
public Block(HashSet<Pin> pins)
|
||||
{
|
||||
Pins = pins;
|
||||
}
|
||||
|
||||
public static Block CreateBlock(string blockSection)
|
||||
{
|
||||
blockSection = blockSection.Replace("\n", "");
|
||||
var pins = new HashSet<Pin>(9);
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
for (var j = 0; j < 3; j++)
|
||||
{
|
||||
if (blockSection[3 *i + j] == '#')
|
||||
{
|
||||
pins.Add(new Pin(i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new Block(pins);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
var ret = "";
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
for (var j = 0; j < 3; j++)
|
||||
{
|
||||
ret += Pins.Contains(new Pin(i, j)) ? "#" : ".";
|
||||
|
||||
}
|
||||
|
||||
ret += "\n";
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public Block Flip()
|
||||
{
|
||||
return new Block((from pin in Pins select new Pin(pin.Y, pin.X)).ToHashSet());
|
||||
}
|
||||
};
|
||||
|
||||
public class Day12
|
||||
{
|
||||
private static string GetBlockSections(string document)
|
||||
{
|
||||
var xIndex = document.IndexOf("x");
|
||||
|
||||
var index = -1;
|
||||
while (true)
|
||||
{
|
||||
var newIndex = document.IndexOf("\n", index + 1);
|
||||
if (newIndex > xIndex)
|
||||
{
|
||||
return document.Substring(0, index -1);
|
||||
}
|
||||
|
||||
index = newIndex;
|
||||
}
|
||||
}
|
||||
|
||||
static string ToBlockString(string blockSection)
|
||||
{
|
||||
return blockSection.Substring(blockSection.IndexOf("\n"));
|
||||
|
||||
}
|
||||
|
||||
static List<Block> ParseBlocks(string document)
|
||||
{
|
||||
var blockSections = GetBlockSections(document).Split("\n\n");
|
||||
return (from blockSection in blockSections select Block.CreateBlock(ToBlockString(blockSection))).ToList();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var b = Block.CreateBlock("###\n##.\n##.");
|
||||
Console.WriteLine(b);
|
||||
Console.WriteLine("\n\n\n");
|
||||
Console.WriteLine(b.Flip());
|
||||
|
||||
Console.WriteLine("\n\n\n");
|
||||
Console.WriteLine(b.Flip().Flip());
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user