Initial Commit

main
Jack Tyrer 3 years ago
commit d96c2e5743

4
.gitignore vendored

@ -0,0 +1,4 @@
.vs
bin
obj

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.31903.286
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventOfCode", "AdventOfCode\AdventOfCode.csproj", "{16AD44AD-7D38-497A-9477-2D09B21EC955}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{16AD44AD-7D38-497A-9477-2D09B21EC955}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{16AD44AD-7D38-497A-9477-2D09B21EC955}.Debug|Any CPU.Build.0 = Debug|Any CPU
{16AD44AD-7D38-497A-9477-2D09B21EC955}.Release|Any CPU.ActiveCfg = Release|Any CPU
{16AD44AD-7D38-497A-9477-2D09B21EC955}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {35FF9F65-08BE-4ECF-80C7-3D5F7524ABFB}
EndGlobalSection
EndGlobal

@ -0,0 +1,49 @@
namespace AdventOfCode._2021;
using System.Collections;
public class Day1
{
public void Part1()
{
var input = File.ReadAllLines("2021/input/day1.txt").AsInt();
int previous = input.First();
int count = 0;
foreach (var i in input)
{
if (i > previous)
{
count += 1;
}
previous = i;
}
Console.WriteLine(count);
}
public void Part2()
{
var input = File.ReadAllLines("2021/input/day1.txt").AsInt().ToList();
int previous = input[0] + input[1] + input[2];
int count = 0;
for (int i = 1; i < input.Count; i++)
{
if (i + 2 > input.Count - 1)
{
break;
}
int sum = input[i] + input[i + 1] + input[i + 2];
if (sum > previous)
{
count += 1;
}
previous = sum;
}
Console.WriteLine(count); // first input isn't counted.
}
}

@ -0,0 +1,69 @@
namespace AdventOfCode._2021;
public class Day2
{
public void Part1()
{
var input = File.ReadAllLines("2021/input/day2.txt");
int h = 0;
int d = 0;
foreach (var s in input)
{
var split = s.Split(' ');
(string direction, int units) = (split[0], int.Parse(split[1]));
if (direction is "forward")
{
h += units;
}
else if (direction is "down")
{
d += units;
}
else
{
d -= units;
}
}
Console.WriteLine(h * d);
}
public void Part2()
{
var input = File.ReadAllLines("2021/input/day2.txt");
int h = 0;
int d = 0;
int aim = 0;
foreach (var s in input)
{
var split = s.Split(' ');
(char dir, int units) = (split[0][0], int.Parse(split[1]));
switch (dir)
{
case 'f':
h += units;
d += aim * units;
break;
case 'd':
aim += units;
break;
case 'u':
aim -= units;
break;
default:
throw new Exception(dir.ToString());
}
}
Console.WriteLine(h * d); // first input isn't counted.
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Content Include="2021\Input\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>

@ -0,0 +1,5 @@
using AdventOfCode._2021;
new Day2().Part2();
Console.ReadLine();

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode
{
internal class Utils
{
}
public static class Extensions
{
public static IEnumerable<int> AsInt(this IEnumerable<string> strings)
{
return strings.Select(x => int.Parse(x));
}
}
}
Loading…
Cancel
Save