tidy up, refactor program.cs by day
This commit is contained in:
parent
b0c565618b
commit
5b4353d91f
14
Day.cs
14
Day.cs
|
@ -1,13 +1,17 @@
|
||||||
namespace aoc2019
|
using System;
|
||||||
|
|
||||||
|
namespace aoc2019
|
||||||
{
|
{
|
||||||
public abstract class Day
|
public abstract class Day
|
||||||
{
|
{
|
||||||
|
public abstract int DayNumber { get; }
|
||||||
public virtual void AllParts()
|
public virtual void AllParts()
|
||||||
{
|
{
|
||||||
Part1();
|
Console.WriteLine($"Day {DayNumber}:");
|
||||||
Part2();
|
Console.WriteLine(Part1());
|
||||||
|
Console.WriteLine(Part2());
|
||||||
}
|
}
|
||||||
public abstract void Part1();
|
public abstract string Part1();
|
||||||
public abstract void Part2();
|
public abstract string Part2();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
21
Day1.cs
21
Day1.cs
|
@ -1,4 +1,3 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
@ -7,16 +6,12 @@ namespace aoc2019
|
||||||
{
|
{
|
||||||
public class Day1 : Day
|
public class Day1 : Day
|
||||||
{
|
{
|
||||||
private static readonly IEnumerable<int> lines =
|
public override int DayNumber => 1;
|
||||||
File.ReadLines("input/day1.in").Select(line => int.Parse(line));
|
|
||||||
|
private static readonly IEnumerable<int> masses =
|
||||||
|
File.ReadLines("input/day1.in").Select(int.Parse);
|
||||||
|
|
||||||
private static int FuelCost(int weight) => weight / 3 - 2;
|
private static int FuelCost(int weight) => weight / 3 - 2;
|
||||||
|
|
||||||
public override void Part1()
|
|
||||||
{
|
|
||||||
Console.WriteLine(lines.Select(num => FuelCost(num)).Sum());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int FullCost(int cost)
|
private static int FullCost(int cost)
|
||||||
{
|
{
|
||||||
int total = 0, newcost, tmp = cost;
|
int total = 0, newcost, tmp = cost;
|
||||||
|
@ -30,10 +25,8 @@ namespace aoc2019
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Part2()
|
public override string Part1() => $"{masses.Select(FuelCost).Sum()}";
|
||||||
{
|
|
||||||
Console.WriteLine(lines.Select(cost => FullCost(cost)).Sum());
|
public override string Part2() => $"{masses.Select(FullCost).Sum()}";
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
46
Day2.cs
46
Day2.cs
|
@ -1,4 +1,3 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
@ -7,54 +6,37 @@ namespace aoc2019
|
||||||
{
|
{
|
||||||
public class Day2 : Day
|
public class Day2 : Day
|
||||||
{
|
{
|
||||||
private static readonly IEnumerable<int> input =
|
public override int DayNumber => 2;
|
||||||
File.ReadLines("input/day2.in")
|
|
||||||
.First()
|
|
||||||
.Split(',')
|
|
||||||
.Select(num => int.Parse(num));
|
|
||||||
|
|
||||||
public static void RunIntCode(ref List<int> v)
|
private static readonly IEnumerable<int> input =
|
||||||
|
File.ReadLines("input/day2.in").First().Split(',').Select(int.Parse);
|
||||||
|
|
||||||
|
public static List<int> RunIntCode(int noun, int verb, List<int> v)
|
||||||
{
|
{
|
||||||
|
v[1] = noun; v[2] = verb;
|
||||||
|
|
||||||
for (var i = 0; v[i] != 99; i += 4)
|
for (var i = 0; v[i] != 99; i += 4)
|
||||||
switch (v[i])
|
switch (v[i])
|
||||||
{
|
{
|
||||||
case 1: v[v[i + 3]] = v[v[i + 1]] + v[v[i + 2]]; break;
|
case 1: v[v[i + 3]] = v[v[i + 1]] + v[v[i + 2]]; break;
|
||||||
case 2: v[v[i + 3]] = v[v[i + 1]] * v[v[i + 2]]; break;
|
case 2: v[v[i + 3]] = v[v[i + 1]] * v[v[i + 2]]; break;
|
||||||
}
|
}
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Part1()
|
public override string Part1()
|
||||||
{
|
{
|
||||||
var output = input.ToList();
|
return $"{RunIntCode(12, 2, input.ToList())[0]}";
|
||||||
output[1] = 12;
|
|
||||||
output[2] = 2;
|
|
||||||
|
|
||||||
RunIntCode(ref output);
|
|
||||||
|
|
||||||
Console.WriteLine($"{output[0]}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Part2()
|
public override string Part2()
|
||||||
{
|
{
|
||||||
List<int> output;
|
|
||||||
|
|
||||||
for (var i = 0; i < 100; i++)
|
for (var i = 0; i < 100; i++)
|
||||||
{
|
|
||||||
for (var j = 0; j < 100; j++)
|
for (var j = 0; j < 100; j++)
|
||||||
{
|
if (RunIntCode(i, j, input.ToList())[0] == 19690720)
|
||||||
output = input.ToList();
|
return $"{100 * i + j}";
|
||||||
output[1] = i;
|
|
||||||
output[2] = j;
|
|
||||||
|
|
||||||
RunIntCode(ref output);
|
return string.Empty;
|
||||||
|
|
||||||
if (output[0] == 19690720)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"{100 * i + j}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
12
Day3.cs
12
Day3.cs
|
@ -7,24 +7,26 @@ namespace aoc2019
|
||||||
{
|
{
|
||||||
internal class Day3 : Day
|
internal class Day3 : Day
|
||||||
{
|
{
|
||||||
|
public override int DayNumber => 3;
|
||||||
|
|
||||||
private readonly IEnumerable<(int, int)> intersections;
|
private readonly IEnumerable<(int, int)> intersections;
|
||||||
private readonly List<Dictionary<(int, int), int>> wires;
|
private readonly List<Dictionary<(int, int), int>> wires;
|
||||||
|
|
||||||
public Day3()
|
public Day3()
|
||||||
{
|
{
|
||||||
wires = File.ReadAllLines("input/day3.in").Select(line => ParseWire(line)).ToList();
|
wires = File.ReadAllLines("input/day3.in").Select(ParseWire).ToList();
|
||||||
intersections = wires[0].Keys.Intersect(wires[1].Keys);
|
intersections = wires[0].Keys.Intersect(wires[1].Keys);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Part1()
|
public override string Part1()
|
||||||
{
|
{
|
||||||
Console.WriteLine(intersections.Min(x => Math.Abs(x.Item1) + Math.Abs(x.Item2)));
|
return $"{intersections.Min(x => Math.Abs(x.Item1) + Math.Abs(x.Item2))}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Part2()
|
public override string Part2()
|
||||||
{
|
{
|
||||||
// add 2 to count (0, 0) on both lines
|
// add 2 to count (0, 0) on both lines
|
||||||
Console.WriteLine(intersections.Min(x => wires[0][x] + wires[1][x]) + 2);
|
return $"{intersections.Min(x => wires[0][x] + wires[1][x]) + 2}";
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Dictionary<(int, int), int> ParseWire(string line)
|
private static Dictionary<(int, int), int> ParseWire(string line)
|
||||||
|
|
24
Day4.cs
24
Day4.cs
|
@ -1,21 +1,19 @@
|
||||||
using System;
|
using System.IO;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace aoc2019
|
namespace aoc2019
|
||||||
{
|
{
|
||||||
internal class Day4 : Day
|
internal class Day4 : Day
|
||||||
{
|
{
|
||||||
int start, end;
|
public override int DayNumber => 4;
|
||||||
|
|
||||||
|
private readonly int start;
|
||||||
|
private readonly int end;
|
||||||
|
|
||||||
public Day4()
|
public Day4()
|
||||||
{
|
{
|
||||||
var range = File.ReadLines("input/day4.in")
|
var range = File.ReadLines("input/day4.in")
|
||||||
.First()
|
.First().Split('-').Select(int.Parse).ToList();
|
||||||
.Split('-')
|
|
||||||
.Select(i => int.Parse(i))
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
start = range[0]; end = range[1];
|
start = range[0]; end = range[1];
|
||||||
}
|
}
|
||||||
|
@ -34,20 +32,20 @@ namespace aoc2019
|
||||||
return i >= start && i <= end && hasDup;
|
return i >= start && i <= end && hasDup;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Part1()
|
public override string Part1()
|
||||||
{
|
{
|
||||||
Console.WriteLine(Enumerable.Range(start, end).Count(i => IsValid(i)));
|
return $"{Enumerable.Range(start, end).Count(IsValid)}";
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool HasOnePair(int i)
|
private bool HasOnePair(int i)
|
||||||
{
|
{
|
||||||
var s = i.ToString();
|
var s = i.ToString();
|
||||||
return s.Select(c => s.Count(j => j == c)).Any(c => c == 2);
|
return IsValid(i) && s.Select(c => s.Count(j => j == c)).Any(c => c == 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Part2()
|
public override string Part2()
|
||||||
{
|
{
|
||||||
Console.WriteLine(Enumerable.Range(start,end).Count(i => IsValid(i) && HasOnePair(i)));
|
return $"{Enumerable.Range(start,end).Count(HasOnePair)}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +0,0 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace aoc2019
|
|
||||||
{
|
|
||||||
internal class DayFactory
|
|
||||||
{
|
|
||||||
internal static Day GetDay(int daynum)
|
|
||||||
{
|
|
||||||
switch (daynum)
|
|
||||||
{
|
|
||||||
case 1: return new Day1();
|
|
||||||
case 2: return new Day2();
|
|
||||||
case 3: return new Day3();
|
|
||||||
case 4: return new Day4();
|
|
||||||
default: return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
30
Program.cs
30
Program.cs
|
@ -1,4 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
namespace aoc2019
|
namespace aoc2019
|
||||||
{
|
{
|
||||||
|
@ -6,29 +9,28 @@ namespace aoc2019
|
||||||
{
|
{
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
var days = GetDays();
|
||||||
|
|
||||||
if (args.Length == 1 && int.TryParse(args[0], out int daynum))
|
if (args.Length == 1 && int.TryParse(args[0], out int daynum))
|
||||||
{
|
{
|
||||||
if (daynum >= 0 && daynum <= 25)
|
var d = days.Where(d => d.DayNumber == daynum);
|
||||||
{
|
if (d.Any())
|
||||||
Day day = DayFactory.GetDay(daynum);
|
d.First().AllParts();
|
||||||
day.AllParts();
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
Console.WriteLine($"{daynum} invalid or not yet implemented");
|
||||||
Console.WriteLine($"{daynum} is an invalid day");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (var i = 1; i <= 25; ++i)
|
foreach (var d in days)
|
||||||
{
|
{
|
||||||
var day = DayFactory.GetDay(i);
|
d.AllParts();
|
||||||
if (day == null) continue;
|
|
||||||
Console.WriteLine($"Day {i}:");
|
|
||||||
day.AllParts();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static IEnumerable<Day> GetDays() =>
|
||||||
|
Assembly.GetExecutingAssembly().GetTypes()
|
||||||
|
.Where(t => t.BaseType == typeof(Day))
|
||||||
|
.Select(t => (Day)Activator.CreateInstance(t));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue