public bool ValidateCommand(string[] commands) { bool isValid = true; foreach (var command in commands) { isValid = command.ToWireType() != WireType.Invalid; if (!isValid) { break; } } if (isValid) { _commands = new WireList(commands.ToWireTypeArray()); } else { _commands = null; } return(isValid); }
/// <summary> /// The FPGA is already read in, this time only read wire statements /// </summary> /// <param name="line"></param> /// <param name="sr"></param> public void ParseWire(Tile tile, XDLStreamReaderWithUndo sr, UnresolvedWires unresWires) { WireList wireList = new WireList(); string line = ""; while ((line = sr.ReadLine()) != null) { if (line.Contains("(wire")) { wireList = XDLWireParser.Parse(tile, sr, unresWires); } else if (line.Contains("tile_summary") && unresWires == null) { StoreAndShareWireList(tile, wireList); //consume closing bracket and exit line = sr.ReadLine(); return; } else if (line.Contains("tile_summary") && unresWires != null) { //consume closing bracket and exit line = sr.ReadLine(); return; } } }
public void WireTypeListConstructor_Explode() { WireList list = new WireList(new List <WireType>() { WireType.White, WireType.Orange, WireType.Green, WireType.White }); Assert.AreEqual(ResultType.Explode, list.CutWires()); }
public void WireTypeListConstructor_Disarm() { WireList list = new WireList(new List <WireType>() { WireType.White, WireType.Red, WireType.Green, WireType.White }); Assert.AreEqual(ResultType.Disarm, list.CutWires()); }
public void Explode() { WireList list = new WireList(); list.Add(WireType.White); list.Add(WireType.Orange); list.Add(WireType.Green); list.Add(WireType.White); Assert.AreEqual(ResultType.Explode, list.CutWires()); }
public void Disarm() { WireList list = new WireList(); list.Add(WireType.White); list.Add(WireType.Red); list.Add(WireType.Green); list.Add(WireType.White); Assert.AreEqual(ResultType.Disarm, list.CutWires()); }
public static void StoreAndShareWireList(Tile tile, WireList wireList) { // reached end of wire statements -> // clean up after switch matrix is read in // no need to remove wires, as the filtering takes place during wire parsing //wires.RemoveNonExistingWires(tile); bool equalWLFound = false; // try finding an equal wire list foreach (WireList other in FPGA.FPGA.Instance.GetAllWireLists().Where(wl => wl.GetHashCode() == wireList.GetHashCode())) { if (other.Equals(wireList)) { wireList.Key = other.Key; equalWLFound = true; break; } } if (!equalWLFound) { foreach (WireList other in FPGA.FPGA.Instance.GetAllWireLists().Where(wl => wl.Count == wireList.Count)) { if (other.Equals(wireList)) { wireList.Key = other.Key; equalWLFound = true; break; } } } if (!equalWLFound) { wireList.Key = FPGA.FPGA.Instance.WireListCount; } // promote wire list key to tile tile.WireListHashCode = wireList.Key; // now share common wire list if (!FPGA.FPGA.Instance.ContainsWireList(tile.WireListHashCode)) { FPGA.FPGA.Instance.Add(tile.WireListHashCode, wireList); } else { } }
//private static Regex m_closingBrackes = new Regex(@"^\s+\)", RegexOptions.Compiled); //private static Regex m_summary = new Regex("(pip)|(tile_summary)", RegexOptions.Compiled); public static WireList Parse(Tile containingTile, XDLStreamReaderWithUndo sr, UnresolvedWires unresWires) { WireList wires = new WireList(); sr.UndoLastRead(); while (true) { // (wire WS5BEG1 3 // (conn INT_BUFS_R_X18Y78 INT_BUFS_WS5B1) // (conn INT_X18Y78 WS5A1) // (conn CLBLM_X18Y78 CLB_WS5A1) // ) string line = sr.ReadLine(); bool conn = line.Contains("(conn"); if (!conn) { if (line.Contains("pip") || line.Contains("tile_summary")) { sr.UndoLastRead(); break; } } // skip closing brackets //if (WireParser.m_closingBrackes.IsMatch(nextLine)) if (line.EndsWith(")")) { continue; } int firstBracketWire = line.IndexOf('(', 0); int firstBlankWire = line.IndexOf(' ', firstBracketWire); int secondBlankWire = line.IndexOf(' ', firstBlankWire + 1); // e.g. (wire WS5BEG1 3 // wireName becomes WS5BEG1 string wireName = line.Substring(firstBlankWire + 1, secondBlankWire - firstBlankWire - 1); // wireCount becomes 3 string wireCountStr = line.Substring(secondBlankWire + 1, line.Length - secondBlankWire - 1); int wireCount = int.Parse(wireCountStr); // wire classification bool beginPip = false; bool endPip = false; beginPip = containingTile.SwitchMatrix.ContainsRight(new Port(wireName)); // HasArcs is expensive, only search if the the pip is a not begin pip if (!beginPip) { endPip = containingTile.SwitchMatrix.ContainsLeft(new Port(wireName)); } for (int i = 0; i < wireCount; i++) { line = sr.ReadLine(); if (beginPip || endPip) { Tile targetTile; Wire w = GetWire(line, containingTile, wireName, beginPip, false, unresWires, out targetTile); if (w == null) { continue; } bool destinationExists = Navigator.DestinationAndWireExists(containingTile, w); if (destinationExists && unresWires == null) { // regular wire (bipartite case: wire, pip, wire, pip) wires.Add(w); } else if (!destinationExists && unresWires != null) { // store wire for later unresWires.Add(containingTile, w); } } else if (unresWires != null) { Tile targetTile; Wire w = GetWire(line, containingTile, wireName, beginPip, true, unresWires, out targetTile); if (w == null) { continue; } // store wire for later unresWires.Add(containingTile, w); } } } return(wires); }
private void ResolveWires(UnresolvedWires unresWires) { Watch.Start("ResolveWires"); if (unresWires == null) { return; } List <Tuple <Tile, WireList> > updates = new List <Tuple <Tile, WireList> >(); foreach (Tuple <Tile, WireList> tuple in unresWires.Where(t => t.Item1.SwitchMatrix.ArcCount != 0)) { //Console.WriteLine(tuple.Item1.Location + " with " + tuple.Item2.Count); WireList wiresToNonExistingTiles = new WireList(); foreach (Wire srcWire in tuple.Item2) { Watch.Start("GetDestinationByWire 1"); Tile destTile = Navigator.GetDestinationByWire(tuple.Item1, srcWire); Watch.Stop("GetDestinationByWire 1"); Watch.Start("contains"); bool contains = unresWires.Contains(destTile.TileKey.X, destTile.TileKey.Y); Watch.Stop("contains"); if (!contains || !tuple.Item1.SwitchMatrix.Contains(new Port(srcWire.LocalPip))) { wiresToNonExistingTiles.Add(srcWire, false); continue; } WireList dstWireList = unresWires.Get(destTile); Watch.Start("inner loop"); foreach (Wire dstWire in dstWireList.Where(d => d.LocalPipKey.Equals(srcWire.PipOnOtherTileKey) && !d.PipOnOtherTile.Equals(srcWire.LocalPip))) { Watch.Start("GetDestinationByWire 2"); Tile otherTile = Navigator.GetDestinationByWire(destTile, dstWire); Watch.Stop("GetDestinationByWire 2"); // tile a -> wire -> tile b -> wire -> tile a , but tile b has no switch matrix. only add wires to tiles with non empty switch matrices // as otherwise the wire can not used Watch.Start("Match"); bool match = otherTile.Location.Equals(tuple.Item1.Location) && //!dstWire.PipOnOtherTile.Equals(srcWire.LocalPip) && //tuple.Item1.SwitchMatrix.ArcCount > 0 && //tuple.Item1.SwitchMatrix.Contains(new Port(srcWire.LocalPip)) && tuple.Item1.SwitchMatrix.Contains(new Port(dstWire.PipOnOtherTile)); Watch.Stop("Match"); if (match) { //this.OutputManager.WriteOutput //Console.WriteLine("Add loop wire on " + tuple.Item1.Location + " connecting " + srcWire.LocalPip + " with " + dstWire.PipOnOtherTile + " (via " + destTile.Location + ")"); Watch.Start("Parse"); Wire w = new Wire(srcWire.LocalPipKey, dstWire.PipOnOtherTileKey, true, 0, 0); Watch.Stop("Parse"); // no duplicates Watch.Start("HasWire"); if (!tuple.Item1.WireList.HasWire(w)) { tuple.Item1.WireList.Add(w, false); m_loopWires++; } Watch.Stop("HasWire"); } } Watch.Stop("inner loop"); } if (wiresToNonExistingTiles.Count < tuple.Item2.Count) { updates.Add(new Tuple <Tile, WireList>(tuple.Item1, wiresToNonExistingTiles)); } } Watch.Start("Clean"); foreach (Tuple <Tile, WireList> tuple in updates) { //Console.WriteLine(tuple.Item1.Location + " reduction from " + unresWires.Get(tuple.Item1).Count + " to " + tuple.Item2.Count); unresWires.Set(tuple.Item1, tuple.Item2); if (tuple.Item2.Count == 0) { unresWires.Remove(tuple.Item1.TileKey.X, tuple.Item1.TileKey.Y); } } Watch.Stop("Clean"); Watch.Stop("ResolveWires"); }
static void Main(string[] args) { var fuelRequirements = FuelRequirements.LoadFromFile("Day01/ModuleMasses.txt").GetFuelRequirements(); Console.WriteLine($"Day 1 Part 1: {fuelRequirements}"); var totalFuelRequirements = FuelRequirements.LoadFromFile("Day01/ModuleMasses.txt").GetFuelRequirementsWithFuelForFuel(); Console.WriteLine($"Day 1 Part 2: {totalFuelRequirements}"); var day2Part1Result = Intcode.LoadFromFile("Day02/GravityAssistProgram.txt").Repair(1, 12).Repair(2, 2).Execute().ReadMemory(0); Console.WriteLine($"Day 2 Part 1: {day2Part1Result}"); var day2Part2Result = Intcode.FindNounAndVerb("Day02/GravityAssistProgram.txt", 19690720); Console.WriteLine($"Day 2 Part 2: {day2Part2Result}"); var day3Part1Result = WireList.LoadFromFile("Day03/Wires.txt").FindClosestIntersection(0, 1); Console.WriteLine($"Day 3 Part 1: {day3Part1Result}"); var day3Part2Result = WireList.LoadFromFile("Day03/Wires.txt").FindClosestSignalIntersection(0, 1); Console.WriteLine($"Day 3 Part 2: {day3Part2Result}"); var day4Part1Result = PasswordCracker.GetPossiblePasswords("152085-670283"); Console.WriteLine($"Day 4 Part 1: {day4Part1Result}"); var day4Part2Result = PasswordCracker.GetPossiblePasswordsWithDigitPair("152085-670283"); Console.WriteLine($"Day 4 Part 2: {day4Part2Result}"); Console.WriteLine("Running program for Day 5 Part 1 - Input should be 1"); Day05.Intcode.LoadFromFile("Day05/Diagnostics.txt").Execute(); Console.WriteLine("Running program for Day 5 Part 2 - Input should be 5"); Day05.Intcode.LoadFromFile("Day05/Diagnostics.txt").Execute(); var day6Part1Result = OrbitMap.LoadFromFile("Day06/Orbits.txt").CountOrbits(); Console.WriteLine($"Day 6 Part 1: {day6Part1Result}"); var day6Part2Result = OrbitMap.LoadFromFile("Day06/Orbits.txt").CalculateOrbitalTransfers("YOU", "SAN"); Console.WriteLine($"Day 6 Part 2: {day6Part2Result}"); var day7Part1Result = SignalMaximizer.GetMaximumThrustSignal(); Console.WriteLine($"Day 7 Part 1: {day7Part1Result}"); var day7Part2Result = SignalMaximizer.GetMaximumFeedbackThrustSignal().Result; Console.WriteLine($"Day 7 Part 2: {day7Part2Result}"); var day8Part1Result = SpaceImage.LoadFromFile("Day08/SpaceImage.txt", 25, 6).FindLayerWithFewestZerosAndGetNumberOfOnesTimeNumberOfTwos(); Console.WriteLine($"Day 8 Part 1: {day8Part1Result}"); Console.WriteLine($"Day 8 Part 2:"); SpaceImage.LoadFromFile("Day08/SpaceImage.txt", 25, 6).RenderImage(); Console.WriteLine($"Day 9 Part 1:"); Day09.Intcode.Intcode.LoadFromFile("Day09/Boost.txt").SetInput(new PreparedInput(1)).Execute().Wait(); Console.WriteLine($"Day 9 Part 2:"); Day09.Intcode.Intcode.LoadFromFile("Day09/Boost.txt").SetInput(new PreparedInput(2)).Execute().Wait(); var day10Part1Result = AsteroidMap.LoadFromFile("Day10/AsteroidMap.txt").GetMaximumVisibility(); Console.WriteLine($"Day 10 Part 1: {day10Part1Result}"); //var day10Part2Result = AsteroidMap.LoadFromFile("Day10/AsteroidMap.txt").GetNthDestroyedAsteroidLocation(200); //Console.WriteLine($"Day 10 Part 2: {day10Part2Result}"); var day11Part1Result = new Day11.Robot().Execute().GetEverWhiteCount(); Console.WriteLine($"Day 11 Part 1: {day11Part1Result}"); Console.WriteLine($"Day 11 Part 2:"); new Day11.Robot().StartOnWhite().Execute().RenderHull(); var day12Part1Result = GravitationalEnergy.LoadFromFile("Day12/MoonLocations.txt").Simulate(1000).GetTotalEnergy(); Console.WriteLine($"Day 12 Part 1: {day12Part1Result}"); var day12Part2Result = GravitationalEnergy.LoadFromFile("Day12/MoonLocations.txt").FindCycle(); Console.WriteLine($"Day 12 Part 2: {day12Part2Result}"); var day13Part1Result = Game.LoadFromFile("Day13/Game.txt").Execute(false).CountCharacters('+'); Console.WriteLine($"Day 13 Part 1: {day13Part1Result}"); var day13Part2Result = Game.LoadFromFile("Day13/Game.txt").ExecuteWithInput(false).GetScore(); Console.WriteLine($"Day 13 Part 2: {day13Part2Result}"); var day14Part1Result = OreForFuelCalculator.LoadFromFile("Day14/Reactions.txt").OreRequiredForChemical("FUEL", 1); Console.WriteLine($"Day 14 Part 1: {day14Part1Result}"); //var day14Part2Result = OreForFuelCalculator.LoadFromFile("Day14/Reactions.txt").MaximumChemicalWithOre("FUEL", 1000000000000); //Console.WriteLine($"Day 14 Part 2: {day14Part2Result}"); var day15Part1Result = new OxygenSystemLocater().GetShortestRouteToOxygen(); Console.WriteLine($"Day 15 Part 1: {day15Part1Result}"); var day15Part2Result = new OxygenSystemLocater().GetTimeToFillWithOxygen(); Console.WriteLine($"Day 15 Part 2: {day15Part2Result}"); var day16Part1Result = FlawedFrequencyTransmission.LoadFromFile("Day16/Signal.txt").ProcessSignal(100); Console.WriteLine($"Day 16 Part 1: {day16Part1Result}"); //var day16Part2Result = FlawedFrequencyTransmission.LoadFromFile("Day16/Signal.txt").ProcessRepeatedSignal(100, 10000); //Console.WriteLine($"Day 16 Part 2: {day16Part2Result}"); var day17Part1Result = new Scaffold().GetSumOfAlignmentParameters(); Console.WriteLine($"Day 17 Part 1: {day17Part1Result}"); var day17Part2Result = new Scaffold().CollectSpaceDust().Result; Console.WriteLine($"Day 17 Part 2: {day17Part2Result}"); var day19Part1Result = new TractorMapper().GetTractorArea(50); Console.WriteLine($"Day 19 Part 1: {day19Part1Result}"); Console.ReadKey(); }
private void ProcessWires(string line, StreamReader sr, ref long currentcharCount, long totalCharCount) { string currentTileName = ""; Tile currentTile = null; WireList wl = new WireList(); string wireLine = line; WireHelper wireHelper = new WireHelper(); do { currentcharCount += wireLine.Length; if (PrintProgress) { ProgressInfo.Progress = (int)(((double)currentcharCount / (double)totalCharCount) * (ExcludePipsToBidirectionalWiresFromBlocking ? 50 : 100)); } if (m_commentRegexp.IsMatch(wireLine)) { Console.WriteLine(wireLine); continue; } int equalIndex = wireLine.IndexOf('='); int sepIndex = wireLine.IndexOf('>', equalIndex); string left = wireLine.Substring(equalIndex + 1, sepIndex - equalIndex - 2); int slashIndexLeft = left.IndexOf("/"); string fromTile = left.Substring(0, slashIndexLeft); string fromPip = left.Substring(slashIndexLeft + 1);//, left.Length - slashIndexLeft-1); if (string.IsNullOrEmpty(currentTileName)) { currentTileName = fromTile; currentTile = FPGA.FPGA.Instance.GetTile(currentTileName); } else if (!fromTile.Equals(currentTileName)) { if (currentTile.WireList != null) { throw new ArgumentException("Wirelist should be null"); } XDLTileParser.StoreAndShareWireList(currentTile, wl); currentTileName = fromTile; currentTile = FPGA.FPGA.Instance.GetTile(currentTileName); wl = new WireList(); } string right = wireLine.Substring(sepIndex + 1);//, wireLine.Length - sepIndex - 1); int slashIndexRight = right.IndexOf("/"); string toTile = right.Substring(0, slashIndexRight); string toPip = right.Substring(slashIndexRight + 1);//, right.Length - slashIndexRight - 1); Tile tile = FPGA.FPGA.Instance.GetTile(fromTile); Tile target = FPGA.FPGA.Instance.GetTile(toTile); uint localPipKey = FPGA.FPGA.Instance.IdentifierListLookup.GetKey(fromPip); if (WireHelper.GetIncludeFlag(WireHelper.IncludeFlag.WiresTrajectoriesData)) { tile.AddWireTrajectoryData(localPipKey, FPGA.FPGA.Instance.IdentifierListLookup.GetKey(toTile)); } if (!currentTile.SwitchMatrix.Contains(fromPip)) { if (!WireHelper.GetIncludeFlag(WireHelper.IncludeFlag.BELOutWires) || !wireHelper.IsBELOutPip(currentTile, fromPip)) { ReadVivadoFPGADebugger.DebugWire(fromTile, fromPip, toTile, toPip); continue; } } short xIncr = (short)(target.TileKey.X - currentTile.TileKey.X); short yIncr = (short)(target.TileKey.Y - currentTile.TileKey.Y); // Check if we should consider U-turn wires bool condition = WireHelper.GetIncludeFlag(WireHelper.IncludeFlag.UTurnWires); condition = condition ? fromTile != toTile || fromPip != toPip : xIncr != 0 || yIncr != 0; if (!condition) { ReadVivadoFPGADebugger.DebugWire(fromTile, fromPip, toTile, toPip); continue; } if (!target.SwitchMatrix.Contains(toPip)) { if (!WireHelper.GetIncludeFlag(WireHelper.IncludeFlag.BELInWires) || !wireHelper.IsBELInPip(target, toPip)) { ReadVivadoFPGADebugger.DebugWire(fromTile, fromPip, toTile, toPip); continue; } } uint pipOnOtherTileKey = FPGA.FPGA.Instance.IdentifierListLookup.GetKey(toPip); Port from = new Port(fromPip); //Port to = new Port(toPip); bool fromIsBegin = currentTile.SwitchMatrix.ContainsRight(from); Wire w = new Wire(localPipKey, pipOnOtherTileKey, fromIsBegin, xIncr, yIncr); wl.Add(w); if (WireHelper.GetIncludeFlag(WireHelper.IncludeFlag.IncomingWires)) { target.AddIncomingWire(w); } }while ((wireLine = sr.ReadLine()) != null); wireHelper.ProcessStopoverArcs(); }
protected override void DoCommandAction() { // reset PRIOR to reading to reset high lighter CommandExecuter.Instance.Execute(new Reset()); FPGA.FPGA.Instance.Reset(); FPGA.FPGA.Instance.BackendType = FPGATypes.BackendType.Vivado; // create reader & open file StreamReader sr = new StreamReader(FileName); FileInfo fi = new FileInfo(FileName); long charCount = 0; long lineCount = 0; string line = ""; while ((line = sr.ReadLine()) != null) { lineCount++; charCount += line.Length; if (PrintProgress) { ProgressInfo.Progress = (int)(((double)charCount / (double)fi.Length) * (ExcludePipsToBidirectionalWiresFromBlocking ? 50 : 100)); } if (m_commentRegexp.IsMatch(line)) { continue; } int length = line.IndexOf('='); string prefix = line.Substring(0, length); switch (prefix) { case "Device": Watch.Start("ProcessDevice"); ProcessDevice(line); Watch.Stop("ProcessDevice"); break; case "R": Watch.Start("ProcessTile"); ProcessTile(line); Watch.Stop("ProcessTile"); break; case "Site": Watch.Start("ProcessSite"); ProcessSite(line); Watch.Stop("ProcessSite"); break; case "Pips": Watch.Start("ProcessPips"); ProcessPips(line); Watch.Stop("ProcessPips"); break; case "Wire": Watch.Start("ProcessWire"); ProcessWires(line, sr, ref charCount, fi.Length); Watch.Stop("ProcessWire"); break; default: { throw new ArgumentException("Unknown line type: " + line + " (line" + lineCount + ")"); } } } sr.Close(); ReadVivadoFPGADebugger.CloseStream(); WireList emptyWl = new WireList(); foreach (Tile tile in FPGA.FPGA.Instance.GetAllTiles().Where(t => t.WireList == null)) { XDLTileParser.StoreAndShareWireList(tile, emptyWl); } if (ExcludePipsToBidirectionalWiresFromBlocking) { ExcludePipsToBidirectionalWiresFromBlocking exclCmd = new ExcludePipsToBidirectionalWiresFromBlocking(); exclCmd.Profile = Profile; exclCmd.PrintProgress = PrintProgress; exclCmd.ProgressStart = 50; exclCmd.ProgressShare = 50; exclCmd.FileName = ""; CommandExecuter.Instance.Execute(exclCmd); } CommandExecuter.Instance.Execute(new Reset()); // no LoadFPGAFamilyScript here! LoadFPGAFamilyScript is called through Reset // remember for other stuff how we read in this FPGA Blackboard.Instance.LastLoadCommandForFPGA = ToString(); }