using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
public class Message
{
public Message(int _time, int _code, int _from, int _to, int _tok, int _pay, int _lamport)
{
(time, code, from, to, tok, pay, lamport) = (_time, _code, _from, _to, _tok, _pay, _lamport);
}
public int time;
public int code;
public int from;
public int to;
public int tok;
public int pay;
public int lamport;
public readonly static int CodeReceive = 1;
public readonly static int CodeSend = 2;
public readonly static int TokForward = 1; // fan-out
public readonly static int TokReturn = 2; // fan-in
public readonly static int TokSnap = 9;
// public readonly static int TokStop = 3; // stop
}
public class Common
{
public static void Print(Message m)
{
Console.WriteLine(
$” {m.time,3} {(m.code == 1 ? ” ” : “”)}{m.code,3}{(m.code == 2 ? ” ” : “”)} {m.from,3} {m.to,3} {m.tok,3} {m.pay,3} {m.lamport,3}”);
}
public static Message[] SortToArray(IEnumerable
{
return ms.OrderBy(m => (m.time, m.code, m.from, m.to, m.tok, m.pay)).ToArray();
}
}
public class Node
{
public Node(int self, int[] neigh, int amount, int fwd_pay, int ret_pay, int snap_lamport)
{
this.self = self;
this.neigh = neigh;
this.amount = amount;
this.fwd_pay = fwd_pay;
this.ret_pay = ret_pay;
this.snap_lamport = snap_lamport;
}
int self;
int[] neigh;
int parent = -1;
int rec = 0;
int size = 1;
int lamport = 0;
int amount;
int fwd_pay;
int ret_pay;
int snap_lamport;
int snap_amount = -1;
// strict: force snap to happen when lamport is updated late
private void SnapAmount(bool strict = false)
{
if ((strict ? lamport > snap_lamport + 1 : lamport >= snap_lamport) && snap_amount == -1)
{
snap_amount = amount;
}
}
public Message[] Process(Message[] msgs)
{
//receive inbound messages
//process
//returns outbound messages
if (msgs[0].tok == Message.TokSnap)
{
var m = msgs[0];
return new[] { new Message(m.time, m.code, m.to, m.@from, m.tok, snap_amount == -1 ? amount : snap_amount, lamport), };
}
SnapAmount(); // initial snap
bool init = msgs.Where(m => m.@from != 0).ToArray().Length == 0;
if (!init)
{
lamport = Math.Max(lamport, msgs.Select(m => m.lamport).Max()) + 1;
foreach (var m in msgs)
{
m.lamport = lamport;
}
msgs.ToList().ForEach(m => Common.Print(m));
SnapAmount(true); // force snap to happen when lamport is updated late
amount += msgs.Select(m => m.pay).Sum();
SnapAmount();
}
var clock = msgs[0].time;
var msgsout = Enumerable.Empty
if (parent == -1)
{
var index = msgs.Min(m => Array.IndexOf(neigh, m.from));
if (index == -1)
{
parent = 0;
rec += -1;
}
else
{
parent = neigh[index];
}
msgsout = neigh.Where(n => n != parent)
.Select(n => new Message(clock, Message.CodeSend, self, n, Message.TokForward, fwd_pay, lamport));
}
else
{
size += msgs.Select(m => m.pay).Sum();
}
rec += msgs.Length;
if (rec >= neigh.Length)
{
msgsout = msgsout.Prepend(new Message(clock, Message.CodeSend, self, parent, Message.TokReturn, ret_pay,
lamport));
}
bool finish = msgsout.Where(m => m.to != 0).ToArray().Length == 0;
if (!finish)
{
lamport++;
foreach (var m in msgsout)
{
m.lamport = lamport;
}
msgsout = Common.SortToArray(msgsout);
msgsout.ToList().ForEach(m => Common.Print(m));
amount -= msgsout.Select(m => m.pay).Sum();
SnapAmount();
}
return msgsout.ToArray();
}
}
public class Arcs
{
static IEnumerable
{
var line = “”;
while ((line = inp.ReadLine()) != null)
{
yield return line;
}
}
// read and store configuration
// initialise nodes
static void Config(TextReader inp)
{
var lines = ReadAllLines(inp)
.Select(line =>
{
var com = line.IndexOf(“//”);
return com < 0 ? line : line.Substring(0, com);
})
.Select(line => line.Trim())
.Where(line => line != “”)
.Select(line => line.Split((char[])null, StringSplitOptions.RemoveEmptyEntries))
.ToArray();
var mline = lines[0].Select(n => int.Parse(n)).ToArray();
var nlines = lines.Skip(1).TakeWhile(line => line[0] != “.”)
.Select(lines => lines.Select(item => int.Parse(item)).ToArray())
.ToArray();
var alines = lines.Skip(nlines.Count() + 2)
.Select(lines => lines.Select(item => int.Parse(item)).ToArray())
.ToArray();
Source = nlines[0][0];
neighs = new Dictionary
nodes = new Dictionary
delays = new Dictionary<(int, int), int>();
amount = mline[0];
fwd_pay = mline[1];
ret_pay = mline[2];
snap_lamport = mline[3];
foreach (var line in nlines)
{
var n = line[0];
var ns = line[1..^0];
neighs[n] = ns;
nodes[n] = new Node(n, ns, amount, fwd_pay, ret_pay, snap_lamport);
}
//nodes.Dump ();
foreach (var line in alines)
{
delays[(line[0], line[1])] = line[2];
if (MaxDelay < line[2]) MaxDelay = line[2];
}
//delays.Dump ();
}
static int Clock;
static int Source; // initiator
static int MaxDelay;
static int MessageCount;
static int amount;
static int fwd_pay;
static int ret_pay;
static int snap_lamport;
static Dictionary
static Dictionary
static Dictionary<(int, int), int> delays;
static int[] Neigh(int n)
{
// not needed
return neighs.ContainsKey(n) ? neighs[n] : Array.Empty
}
static Node Node(int n)
{
return nodes[n];
}
static int Delay(int n1, int n2)
{
return delays.ContainsKey((n1, n2)) ? delays[(n1, n2)] : delays[(0, 0)];
}
// priority queue for organising messages
static Message[] Msgs;
public static void Main(string[] args)
{
try
{
Clock = 0;
// Source; // initiator
MaxDelay = 1;
MessageCount = 0;
// Linqpad stuff
// Directory.SetCurrentDirectory (Path.GetDirectoryName (Util.CurrentQueryPath));
//Config(new StreamReader(“a3-inp-1.txt”));
Config (Console.In);
Msgs = Array.Empty
MessageCount = 0;
var m0 = new Message(Clock, 2, 0, Source, 1, 0, 0);
var msgs = Node(Source).Process(new[] { m0 });
// sends and receives messages on behalf of nodes,
// adding required delays, and tracing
while (Msgs.Length + msgs.Length > 0)
{
if (msgs.Length > 0 && msgs[0].to == 0)
{
break;
}
MessageCount += msgs.Length;
var msgs2 = Common.SortToArray(msgs);
for (var i = 0; i < msgs2.Length; i++)
{
var m = msgs2[i];
m.time += Delay(m.from, m.to);
}
Msgs = Common.SortToArray(Msgs.Concat(msgs2));
Clock = Msgs[0].time;
var msgsout = Msgs.TakeWhile(m => m.time == Clock).ToArray();
for (var i = 0; i < msgsout.Length; i++)
{
var m = msgsout[i];
m.code = 1;
}
Msgs = Msgs[msgsout.Length..];
var msgsin = Enumerable.Empty
var msgsout_grouped = msgsout.GroupBy(m => m.to).OrderBy(g => g.Key);
foreach (var mgroup in msgsout_grouped)
{
msgsin = msgsin.Concat(Node(mgroup.Key).Process(mgroup.ToArray()));
}
msgs = msgsin.ToArray();
}
Clock++;
Console.WriteLine();
var MsgAmount = Array.Empty
foreach (var n in nodes.Keys)
{
var message = new Message(Clock, Message.CodeSend, 0, n, Message.TokSnap, 0, 0);
var outMessages = Node(n).Process(new[] { message });
MsgAmount = MsgAmount.Append(outMessages[0]).ToArray();
}
MsgAmount = Common.SortToArray(MsgAmount);
MsgAmount.ToList().ForEach(m => Common.Print(m));
int total_snap_amount = MsgAmount.Select(m => m.pay).Sum();
Console.WriteLine();
Console.WriteLine(
$” {nodes.Count,3} {amount,3} {fwd_pay,3} {ret_pay,3} {amount * nodes.Count,3} {total_snap_amount,3} {snap_lamport,3}”);
// stop
}
catch (Exception ex)
{
Console.WriteLine($”*** {ex.Message}”);
}
}
}