private void LoadTrace(string traceFile) { var trace = TraceReader.Read(File.ReadAllBytes(traceFile)); var state = new TState(new TModel("FA055_tgt.mdl"), true); var reader = new TCommandsReader(trace); while (!reader.AtEnd()) { state.Step(reader); } }
public void Step(TCommandsReader commands) { if (Harmonics == EHarmonics.Low) { Energy += 3 * Model.R * Model.R * Model.R; } else if (Harmonics == EHarmonics.High) { Energy += 30 * Model.R * Model.R * Model.R; } Energy += 20 * Bots.Count; var botsCount = Bots.Count; var fusionPrimaries = new Dictionary <int, CoordDiff>(); var fusionSecondaries = new Dictionary <int, CoordDiff>(); var volatiles = new Volatiles(EnableValidation); for (var botIdx = 0; botIdx < botsCount; ++botIdx) { var command = commands.GetCommand(botIdx); var bot = Bots[botIdx]; switch (command) { case Halt halt: { volatiles.Update(bot.Coord, bot); break; } case Wait wait: { volatiles.Update(bot.Coord, bot); break; } case Flip flip: { volatiles.Update(bot.Coord, bot); if (Harmonics == EHarmonics.High) { Harmonics = EHarmonics.Low; } else { Harmonics = EHarmonics.High; } break; } case StraightMove move: { MoveBot(bot, move.Diff, volatiles); Energy += 2 * move.Diff.MLen(); break; } case LMove lMove: { MoveBot(bot, lMove.Diff1, volatiles); MoveBot(bot, lMove.Diff2, volatiles); Energy += 2 * (lMove.Diff1.MLen() + 2 + lMove.Diff2.MLen()); break; } case Fission fission: { bot.Seeds.Sort(); var newBot = new TBot { Bid = bot.Seeds[0] }; for (var i = 1; i <= fission.M; ++i) { newBot.Seeds.Add(bot.Seeds[i]); } bot.Seeds.RemoveRange(0, fission.M + 1); newBot.Coord = bot.Coord; newBot.Coord.Apply(fission.Diff); volatiles.Update(bot.Coord, bot); volatiles.Update(newBot.Coord, newBot); Bots.Add(newBot); Energy += 24; break; } case Fill fill: { var newCoord = bot.Coord; newCoord.Apply(fill.Diff); volatiles.Update(bot.Coord, bot); volatiles.Update(newCoord, bot); if (Matrix[newCoord.X, newCoord.Y, newCoord.Z] > 0) { Energy += 6; } else { Matrix[newCoord.X, newCoord.Y, newCoord.Z] = 1; if (EnableValidation) { if (Harmonics == EHarmonics.Low && !IsGrounded(newCoord, new HashSet <TCoord>())) { throw new InvalidStateException($"{newCoord} is not grounded"); } } Energy += 12; } break; } case Void @void: { var newCoord = bot.Coord; newCoord.Apply(@void.Diff); volatiles.Update(bot.Coord, bot); volatiles.Update(newCoord, bot); if (Matrix[newCoord.X, newCoord.Y, newCoord.Z] > 0) { Matrix[newCoord.X, newCoord.Y, newCoord.Z] = 0; Energy -= 12; } else { Energy += 3; } break; } case FusionP fusionP: { fusionPrimaries.Add(botIdx, fusionP.Diff); break; } case FusionS fusionS: { fusionSecondaries.Add(botIdx, fusionS.Diff); break; } default: throw new InvalidStateException($"unknown item type {command}"); } } if (fusionPrimaries.Count > 0) { Fuse(fusionPrimaries, fusionSecondaries, volatiles); } SortBots(); commands.Advance(botsCount); }
public List <ICommand> MakeReassemblyTrace(TModel srcModel, TModel tgtModel) { Fill doFill = new Fill(); doFill.Diff.Dy = -1; Void doVoid = new Void(); doVoid.Diff.Dy = -1; var result = new List <ICommand>(); result.Add(new Flip()); var current = new TCoord(); var dumpCureTraverse = new TDumpCubeTraverse(srcModel, tgtModel); TState state = new TState(srcModel); List <ICommand> ss = new List <ICommand>(); var iteration = 0; while ((iteration == 0) || !current.IsAtStart()) { var next = dumpCureTraverse.Next(); if (srcModel[next] > 0) { Void curVoid = new Void(); curVoid.Diff = dumpCureTraverse.GetDirection(); result.Add(curVoid); { if (ss.Count == 0) { ss.Add(curVoid); } else { ss[0] = curVoid; } ss.Add(curVoid); TCommandsReader cr = new TCommandsReader(ss); state.Step(cr); } } var move = new StraightMove(); move.Diff = dumpCureTraverse.GetDirection(); result.Add(move); { if (ss.Count == 0) { ss.Add(move); } else { ss[0] = move; } TCommandsReader cr = new TCommandsReader(ss); state.Step(cr); } if ((next.Y > 0) && (tgtModel[next.X, next.Y - 1, next.Z] > 0) && state.Matrix[next.X, next.Y - 1, next.Z] == 0) { result.Add(doFill); { if (ss.Count == 0) { ss.Add(doFill); } else { ss[0] = doFill; } TCommandsReader cr = new TCommandsReader(ss); state.Step(cr); } } if ((next.Y > 0) && (tgtModel[next.X, next.Y - 1, next.Z] == 0) && state.Matrix[next.X, next.Y - 1, next.Z] > 0) { result.Add(doVoid); { if (ss.Count == 0) { ss.Add(doVoid); } else { ss[0] = doVoid; } TCommandsReader cr = new TCommandsReader(ss); state.Step(cr); } } current = next; ++iteration; } result.Add(new Flip()); result.Add(new Halt()); return(result); }