private void PlayGame(RingList <int> cups, int roundCount) { int maxVal = cups.Count; for (int round = 0; round < roundCount; ++round) { if (round % 10000 == 0) { Console.WriteLine($"{round}"); } int[] pocket = Enumerable.Range(1, 3).Select(_ => cups.RemoveAt(1)).ToArray(); int target = cups[0] - 1; while (pocket.Contains(target) || target < 1) { --target; if (target < 1) { target = maxVal; } } for (int sidx = cups.Raw.Count - 1; sidx >= 0; --sidx) { if (cups.Raw[sidx] == target) { cups.Raw.InsertRange(sidx + 1, pocket); break; } } cups.RotateLeft(); } }
public override string P2() { RingList <int> cups; { int[] vals = new int[30]; for (int idx = 0; idx < vals.Count(); ++idx) { vals[idx] = idx + 1; } int[] bottom = Input[0].Select(ch => ch - '0').ToArray(); for (int idx = 0; idx < bottom.Count(); ++idx) { vals[idx] = bottom[idx]; } cups = new RingList <int>(vals); } PlayGame2(cups, 1000, false); int oneIndex = cups.Raw.IndexOf(1); long nextCup = cups[oneIndex + 1]; long nextCup2 = cups[oneIndex + 2]; return((nextCup * nextCup2).ToString()); }
// 45798623 public override string P1() { RingList <int> cups = new RingList <int>(Input[0].Select(ch => ch - '0')); PlayGame2(cups, 100, false); return(string.Join("", Enumerable.Range(cups.Raw.IndexOf(1) + 1, cups.Count - 1).Select(idx => cups[idx].ToString()))); }
public string SolveFirstTask() { _ringList = new RingList(_input); _ringList.MoveToNext(); RunGame(100); return(GetFirstTaskResult()); }
public string SolveSecondTask() { _input.AddRange(Enumerable.Range(_input.Max() + 1, 1000000 - _input.Count).ToList()); _ringList = new RingList(_input); _ringList.MoveToNext(); RunGame(10000000); _ringList.SetCurrent(1); return($"{(long)_ringList.MoveToNext() * _ringList.MoveToNext()}"); }
public void AddFirst_MoreThenLimit_CountShouldBeLimit() { var limit = 100; var ringList = new RingList <int>(limit); for (var i = 0; i < 150; i++) { ringList.AddFirst(i); } Assert.AreEqual(limit, ringList.Count); }
public void Start() { Spawner.init(this); _screenPlay = ScreenPlay.loadFromFile(screenPlayName); _audioSourcesRing = new RingList <AudioSource>(_audioSources); _imageSourcesRing = new RingList <GameObject> (_imageSources); instanciateSpawners(); if (_autostart) { play(); } }
public ServerGame(GameRules rules, NetworkSettings settings) { lock (this) { _process = true; _initialized = false; _startGameTimerStarted = false; } this.rules = rules; _messagesToProcess = new List <PlayerMessage>(1024); _players = new RingList(); _unusedPlayerID = 0; _handNumber = 0; _turn = 0; _totalTurns = 1; _startGameTimerSeconds = InitialStartGameTimerSeconds; _shutdown = false; _deck = null; _table = null; _discard = null; _gameStarted = false; _gameOver = false; Thread t; //start rules Server! _rulesServer = new RulesServer(); _connServer = new ConnServer(this, this.rules, settings); if ((!_rulesServer.IsBound()) || (!_connServer.IsBound())) { _rulesServer.Unbind(); _connServer.Stop(); return; } else { lock (this) { _initialized = true; } } t = new Thread(new ThreadStart(_rulesServer.Start)); t.Name = "PXServer Rules Server"; //t.IsBackground = true; t.Start(); t = new Thread(new ThreadStart(_connServer.Start)); t.Name = "PXServer Connection Server"; //t.IsBackground = true; t.Start(); t = new Thread(new ThreadStart(this.process)); t.Name = "PXServer Message Processing thread"; //t.IsBackground = true; t.Start(); t = new Thread(new ThreadStart(this.doHeartBeat)); t.Name = "Heartbeat thread"; //t.IsBackground = true; t.Start(); }
private void PlayGame2(RingList <int> cups, int roundCount, bool print) { int[] reverseLookup = new int[cups.Count + 1]; for (int idx = 0; idx < cups.Count; ++idx) { reverseLookup[cups[idx]] = idx; } int current = cups[0]; int maxVal = cups.Count; for (int round = 0; round < roundCount; ++round) { if (print) { Console.Write($"{round,5}: "); int oneIndex = cups.Raw.IndexOf(1); Console.Write($"1 -> {cups[oneIndex + 1],2} -> {cups[oneIndex + 2],2}: "); StringBuilder sb = new StringBuilder(); foreach (int val in cups.Raw) { if (val == 1) { Console.ForegroundColor = ConsoleColor.Red; } else { Console.ForegroundColor = ConsoleColor.Gray; } if (cups[0] == val) { sb.AppendFormat($"({val,2}) "); Console.Write($"({val,2}) "); } else { sb.AppendFormat($" {val,2} "); Console.Write($" {val,2} "); } } Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(); } int offset = 0; int[] pocket = Enumerable.Range(1, 3).Select(_ => { if (reverseLookup[current] + 1 >= cups.Count) { ++offset; } return(cups.RemoveAt(reverseLookup[current] - offset + 1)); }).ToArray(); int target = current - 1; while (pocket.Contains(target) || target < 1) { --target; if (target < 1) { target = maxVal; } } // Get target index int offset2 = 0; foreach (int val in pocket) { if (reverseLookup[val] < reverseLookup[target]) { ++offset2; } } //Debug.Assert(offset == offset2); cups.Raw.InsertRange(reverseLookup[target] - offset2 + 1, pocket); int begin, end; if (reverseLookup[target] > reverseLookup[current]) { begin = reverseLookup[current] + 1; end = reverseLookup[target]; } else { begin = reverseLookup[target] + 1; end = reverseLookup[current] + 3; } if (cups.Count - reverseLookup[current] <= 3) { begin = 0; end = cups.Count - 1; } for (; begin <= end; ++begin) { reverseLookup[cups[begin]] = begin; } current = cups[reverseLookup[current] + 1]; } }