CSharp · Console
// very very spaceship - 04.19
// You must be the newest comms operator! Thanks for relieving me of duty. I can't wait to visit the ocean again.
// Anyway, your job is pretty straight forward. Each day we connect to a satellite back home.
// The problem is, the sat we use for our downlink changes all the time and each one has a little different way of talking to us.
// We need you to write a routine to scan for the best frequency to listen in on.
// Don't worry about all the antenna code outside of the ScanSignal function. No one knows how it works and we might be switching to a new antenna soon anyway.
// The antenna has a pretty basic API:
// float Tune(int) - Set the antenna to a specific frequency. Frequencies are whole numbers that represent MHz. Returns a float representing the signal strength from 0-1.
// When you tune to the correct frequency, the strength will be 1.
// Takes 1 space minute to tune to a frequency.
// char Listen() - Listens to the tuned frequency, downloads and analyzes the data and attempts to render it as a human readable character.
// Takes 1 space minute to listen for a character.
// int GetMinFrequency() and int GetMaxFrequency() - Returns the whole number frequency in MHz.
// The satellites we use to communicate are kinda old, so they sometimes advertise frequencies that are no longer available.
// You'll want to monitor the result of tuning, otherwise you might pickup garbage.
// string GetMessageDelimiter() - Returns the delimiter that is at the start and end of every message.
// int ValidateMessage(string) - Don't worry about calling this one, our antenna will automatically run some magical checksum to validate the message.
// Returns a positive, non-zero integer representing the number of time steps it took to get the mesage or zero if the message was incorrect.
// Space is weird. There's a lot of background noise. I've been operating this antenna for over a decade and I've noticed space always seems happier nearer to radio bands.
// You'll probably start to hear a hum build up towards an active comms band and silence when you're way off.
// Our gravity-bound peers back on Earth always start and end a message with special characters. I think they've been using .-. lately.
// Lastly, time is money out here...and uh..data is also money. Yea, basically everything in space is kinda expensive.
// Every time you Tune the antenna or Listen, time is passing us by. You'll want to get to the correct frequency and get our message out as fast as you can because we've got other experiments to do while we're up here.
// Since time is always passing us by, we might tune to the correct frequency in the middle of the message. Be careful to make sure the message you validate is prefixed and postfixed with the delimiter!
// When the message is validated, the system will return the total time in space minutes it took to decode the message. I think a space minute is around 6 deciseconds, but I was never too good at math.
// Good luck!
using System;
using System.Text;
public class Program
{
static string ScanSignal( Antenna a )
{
// Write your routine here
return "";
}
// Don't touch the code below here. The comms operator who wrote it left 30 years ago and no one knows what anything does...
public static void Main()
{
Antenna a = new Antenna();
string message = ScanSignal( a );
int validMessage = a.ValidateMessage( message );
if ( validMessage > 0 )
{
Console.WriteLine( string.Format( "Message received in {0} time steps.", validMessage ) );
Console.WriteLine( message );
}
else
{
Console.WriteLine( "Unable to parse message" );
Console.WriteLine( message );
}
}
}
public class Antenna { private const int mpf = 1; private const int Mpf = 4000; private int cq = 0; private int ty = 0; private int amf = 1; private int aMf = 4000; private int mq = 1; private int Mq = 4000; private float fr = .05f; private float sr = .1f; private float tr = .15f; private float ftr = .2f; private int sps = 0; private bool vq = false; private string mfm = "{0}{1}{2}"; private string fm; private string _ = "RVJUSC0+QkFDRTogVHJhbnNtaXR0aW5nIG9uIEJBQ0UgZnJlcS4="; private string __ = "QkFDRQ=="; private string ___ = "Q29udGFjdCBsaWdodA=="; private string ____ = "Xy5f"; private bool _____; private string ______; private Random random; public Antenna() { random = new Random( 7201969 ); _____ = true; ______ = d( "Ll8u" ); mq = random.Next( mpf, mpf + 5 ); Mq = random.Next( Mpf - 5, Mpf ); ty = random.Next( mq, Mq ); fm = string.Format( mfm, ______, d( _ ), ______ ); } public int GetMinFrequency() { return amf; } public int GetMaxFrequency() { return aMf; } public float Tune( int newFrequency ) { sps++; if ( newFrequency >= mq && newFrequency <= Mq ) { cq = newFrequency; vq = true; } else { if ( newFrequency == Convert.ToUInt32( d( __ ), 16 ) ) { _____ = false; ty = cq = newFrequency; sps = 0; ______ = d( ____ ); fm = string.Format( mfm, ______, d( ___ ), ______ ); vq = true; } vq = false; } return SignalStrength(); } public char Listen() { sps++; if ( !vq ) { return _[random.Next( 0, _.Length - 1 )]; } float delta = Math.Abs( 1 - SignalStrength() ); if ( delta == 0 ) { return GetNextMessageCharacter(); } else if ( delta <= fr ) { return 'M'; } else if ( delta <= sr ) { return 'm'; } else if ( delta <= tr ) { return '-'; } else if ( delta <= ftr ) { return '.'; } else { return ' '; } } public int ValidateMessage( string msg ) { if ( msg == d( _ ) ) { return sps; } else { return 0; } } public string GetMessageDelimiter() { return ______; } private char GetNextMessageCharacter() { return fm[sps % fm.Length]; } private float SignalStrength() { return 1 - ( Math.Abs( cq - ty ) / ( float )( Mq - mq ) ); } private string e( string str ) { return Convert.ToBase64String( Encoding.UTF8.GetBytes( str ) ); } private string d( string str ) { return Encoding.UTF8.GetString( Convert.FromBase64String( str ) ); } }