Part 1
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace Kata4
{
class Kata4
{
public static void Main(string[] argv)
{
FileStream fs = new FileStream("K4Weather.txt", FileMode.Open, FileAccess.Read);
StreamReader r = new StreamReader(fs);
int min_spread = Int32.MaxValue;
int spread_day = -1;
while(r.Peek() > -1)
{
string s = r.ReadLine();
Match fields = Regex.Match(s, "^ +([0-9]+) +([0-9]+)* *([0-9]+).*");
try {
int day = Convert.ToInt32(fields.Groups[1].ToString());
int min = Convert.ToInt32(fields.Groups[3].ToString());
int max = Convert.ToInt32(fields.Groups[2].ToString());
int spread = max - min;
if (spread <= min_spread)
{
spread_day = day;
min_spread = spread;
}
} catch {
}
}
Console.WriteLine(spread_day);
}
}
}
Part 2
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace Kata4
{
class Kata4_2
{
public static void Main(string[] argv)
{
FileStream fs = new FileStream("K4Soccer.txt", FileMode.Open, FileAccess.Read);
StreamReader r = new StreamReader(fs);
int min_spread = Int32.MaxValue;
string spread_day = "";
while(r.Peek() > -1)
{
string s = r.ReadLine();
Match fields = Regex.Match(s, "^ +[0-9]+\\. ([A-Za-z_]+) +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +([0-9]+) +- +([0-9]+).*");
try {
string day = fields.Groups[1].ToString();
Console.Write("Testing");
Console.WriteLine(day);
int min = Convert.ToInt32(fields.Groups[2].ToString());
int max = Convert.ToInt32(fields.Groups[3].ToString());
int spread = Math.Abs( max - min );
if (spread <= min_spread)
{
spread_day = day;
min_spread = spread;
}
} catch {
}
}
Console.WriteLine(spread_day);
}
}
}
Part 3
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace Kata4
{
class Kata4_2
{
public static void Main(string[] args)
{
Console.Write("part1: ");
DM("K4Weather.txt", "^ +([0-9]+) +([0-9]+)* *([0-9]+).*");
Console.Write("part2: ");
DM("K4Soccer.txt", "^ +[0-9]+\\. ([A-Za-z_]+) +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +([0-9]+) +- +([0-9]+).*");
}
public static void DM(string file, string pattern)
{
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
StreamReader r = new StreamReader(fs);
int min_spread = Int32.MaxValue;
string spread_day = "";
while(r.Peek() > -1)
{
string s = r.ReadLine();
Match fields = Regex.Match(s, pattern);
try {
string day = fields.Groups[1].ToString();
int min = Convert.ToInt32(fields.Groups[2].ToString());
int max = Convert.ToInt32(fields.Groups[3].ToString());
int spread = Math.Abs( max - min );
if (spread <= min_spread)
{
spread_day = day;
min_spread = spread;
}
} catch {
}
}
Console.WriteLine(spread_day);
}
}
}