Skip to content

Commit

Permalink
Migrating project structure to new Repo
Browse files Browse the repository at this point in the history
This finalises the move .NET 8 and allows the application to download
the puzzle input for each day locally to the MyDocuments folder.

This download only happens if a day's input is not present locally (to
prevent over fetching from AdventOfCode).
  • Loading branch information
tiberiushunter committed Dec 5, 2023
0 parents commit 37a52d3
Show file tree
Hide file tree
Showing 109 changed files with 5,746 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: .NET

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal --filter "FullyQualifiedName!~AdventOfCode.Solutions.Tests"
34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
*.swp
*.*~
project.lock.json
.DS_Store
*.pyc

# Visual Studio Code
.vscode

# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
input/

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
build/
bld/
[Bb]in/
[Oo]bj/
msbuild.log
msbuild.err
msbuild.wrn

# Visual Studio 2015
.vs/
appsettings.json
5 changes: 5 additions & 0 deletions .markdownlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Disable line length limit
MD013: false

# Disable inline HTML
MD033: false
13 changes: 13 additions & 0 deletions AdventOfCode.Client/AdventOfCode.Client.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
</ItemGroup>

</Project>
20 changes: 20 additions & 0 deletions AdventOfCode.Client/AdventOfCodeClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace AdventOfCode.Client;

public class AdventOfCodeClient : IAdventOfCodeClient
{
private readonly IHttpClientFactory _httpClientFactory;

public AdventOfCodeClient(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}

public async Task<byte[]> GetPuzzleInputForDay(int year, int day)
{
var httpClient = _httpClientFactory.CreateClient("AdventOfCode");

var response = await httpClient.GetByteArrayAsync($"{year}/day/{day}/input");

return response;
}
}
23 changes: 23 additions & 0 deletions AdventOfCode.Client/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using AdventOfCode.Client.Settings;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace AdventOfCode.Client;
public static class Configuration
{
public static IServiceCollection AddAdventOfCodeClient(this IServiceCollection services, IConfiguration configuration)
{
services.AddTransient<IAdventOfCodeClient, AdventOfCodeClient>();
services.AddSingleton<IAdventOfCodeConfiguration, AdventOfCodeConfiguration>();

var adventOfCodeConfiguration = new AdventOfCodeConfiguration(configuration.GetSection("AdventOfCodeClient"));

services.AddHttpClient("AdventOfCode", client =>
{
client.BaseAddress = new Uri(adventOfCodeConfiguration.Domain);
client.DefaultRequestHeaders.Add("Cookie", $"session={adventOfCodeConfiguration.SessionToken}");
});

return services;
}
}
6 changes: 6 additions & 0 deletions AdventOfCode.Client/IAdventOfCodeClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace AdventOfCode.Client;

public interface IAdventOfCodeClient
{
Task<byte[]> GetPuzzleInputForDay(int year, int day);
}
14 changes: 14 additions & 0 deletions AdventOfCode.Client/Settings/AdventOfCodeConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Extensions.Configuration;

namespace AdventOfCode.Client.Settings;
public class AdventOfCodeConfiguration : IAdventOfCodeConfiguration
{
private readonly IConfigurationSection _configuration;
public AdventOfCodeConfiguration(IConfigurationSection configuration)
{
_configuration = configuration;
}

public string Domain => $"{_configuration["Domain"]}";
public string SessionToken => $"{_configuration["SessionToken"]}";
}
6 changes: 6 additions & 0 deletions AdventOfCode.Client/Settings/IAdventOfCodeConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace AdventOfCode.Client.Settings;
public interface IAdventOfCodeConfiguration
{
string Domain { get; }
string SessionToken { get; }
}
33 changes: 33 additions & 0 deletions AdventOfCode.Console/AdventOfCode.Console.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Colorful.Console" Version="1.2.15" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AdventOfCode.Client\AdventOfCode.Client.csproj" />
<ProjectReference Include="..\AdventOfCode.Services\AdventOfCode.Services.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.template.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
155 changes: 155 additions & 0 deletions AdventOfCode.Console/App.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
using AdventOfCode.Domain.Entities;
using AdventOfCode.Domain.Interfaces;
using System.Drawing;
using static Colorful.Console;

namespace AdventOfCode.Console;

public class App
{
private readonly IPuzzleSolverService _solverService;

public App(IPuzzleSolverService solverService)
{
_solverService = solverService;
}

public void Run(string[] args)
{
WriteLine(_welcomeText, Color.Green);
SelectYear();
}

public async void SelectYear()
{
Write("Choose a year to solve (2015-2023) or press Enter to solve all years\t");

string yearInput = ReadLine();

if (int.TryParse(yearInput, out int yearSelected))
{
await SelectDay(yearSelected);
}
else
{
await SolveAll();
}
}

private async Task SelectDay(int yearSelected)
{
Write("\nChoose a day to solve (1-25) or press Enter to solve all days\t");

string dayInput = ReadLine();

if (int.TryParse(dayInput, out int daySelected))
{
await SolveDay(yearSelected, daySelected);
}
else
{
await SolveYear(yearSelected);
}
}

private async Task SolveDay(int year, int day)
{
try
{
var solution = await _solverService.SolveDay(year, day);
PrintSolution(solution);
}
catch (Exception ex)
{
WriteLine($"{ex.Message}", Color.Red);
}
}

private async Task SolveYear(int year)
{
LineBreak(Color.Green);
WriteLine($" All Days for {year} Selected", Color.Green);
LineBreak(Color.Green);

var solutions = await _solverService.SolveYear(year);

foreach (var solution in solutions)
{
try
{
PrintSolution(solution);
}
catch (Exception ex)
{
WriteLine($"{ex.Message}", Color.Red);
}
}

var totalTime = solutions.Sum(s => s.PartA.ElapsedTime + s.PartB.ElapsedTime);

LineBreak(Color.Yellow);
WriteLine($" Total Execution Time for {year}: {totalTime}ms", Color.Yellow);
LineBreak(Color.Yellow);
}

private async Task SolveAll()
{
LineBreak(Color.Green);
WriteLine(" All Days for All Years Selected", Color.Green);
LineBreak(Color.Green);

var allSolutions = await _solverService.SolveAll();

foreach (var solutionsInYear in allSolutions)
{
foreach (var solution in solutionsInYear)
{
try
{
PrintSolution(solution);
}
catch (Exception ex)
{
WriteLine($"{ex.Message}", Color.Red);
}
}
}
var totalTime = allSolutions
.Sum(solutionYear =>
solutionYear
.Sum(solution => solution.PartA.ElapsedTime + solution.PartB.ElapsedTime));

LineBreak(Color.Yellow);
WriteLine($" Total Execution Time: {totalTime}ms", Color.Yellow);
LineBreak(Color.Yellow);
}

private static void PrintSolution(DaySolution solution)
{
LineBreak(Color.MediumPurple);
WriteLine($" {solution.Year} - Day {solution.Day}", Color.HotPink);
LineBreak(Color.MediumPurple);

WriteFormatted("Part 1:\t{0}\t", Color.LightBlue, Color.Gray, solution.PartA.Solution);
WriteLineFormatted("({0})", Color.LightGreen, Color.Gray, solution.PartA.ElapsedTime + "ms");

WriteFormatted("Part 2:\t{0}\t", Color.LightBlue, Color.Gray, solution.PartB.Solution);
WriteLineFormatted("({0})", Color.LightGreen, Color.Gray, solution.PartB.ElapsedTime + "ms");
}

private static void LineBreak(Color colour, int length = 72)
{
WriteLine(new string('=', length), colour);
}

private string _welcomeText => @"
█████╗ ██████╗ ██╗ ██╗███████╗███╗ ██╗████████╗ ██████╗ ███████╗ ██████╗ ██████╗ ██████╗ ███████╗
██╔══██╗██╔══██╗██║ ██║██╔════╝████╗ ██║╚══██╔══╝ ██╔═══██╗██╔════╝ ██╔════╝██╔═══██╗██╔══██╗██╔════╝
███████║██║ ██║██║ ██║█████╗ ██╔██╗ ██║ ██║ ██║ ██║█████╗ ██║ ██║ ██║██║ ██║█████╗
██╔══██║██║ ██║╚██╗ ██╔╝██╔══╝ ██║╚██╗██║ ██║ ██║ ██║██╔══╝ ██║ ██║ ██║██║ ██║██╔══╝
██║ ██║██████╔╝ ╚████╔╝ ███████╗██║ ╚████║ ██║ ╚██████╔╝██║ ╚██████╗╚██████╔╝██████╔╝███████╗
╚═╝ ╚═╝╚═════╝ ╚═══╝ ╚══════╝╚═╝ ╚═══╝ ╚═╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚══════╝
";
}
Loading

0 comments on commit 37a52d3

Please sign in to comment.