Skip to content

Commit

Permalink
Add solutions for all days
Browse files Browse the repository at this point in the history
  • Loading branch information
TornOne authored Feb 9, 2021
0 parents commit f1395e5
Show file tree
Hide file tree
Showing 27 changed files with 2,095 additions and 0 deletions.
68 changes: 68 additions & 0 deletions Advent of Code 2020.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{D017CB45-4695-4C39-9312-9A1599E5EF7D}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Advent_of_Code_2020</RootNamespace>
<AssemblyName>Advent of Code 2020</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>none</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\</OutputPath>
<DefineConstants>
</DefineConstants>
<Optimize>true</Optimize>
<DebugType>none</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>none</ErrorReport>
</PropertyGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Compile Include="Day21.cs" />
<Compile Include="Day20.cs" />
<Compile Include="Day19.cs" />
<Compile Include="Day18.cs" />
<Compile Include="Day17.cs" />
<Compile Include="Day16.cs" />
<Compile Include="Day15.cs" />
<Compile Include="Day14.cs" />
<Compile Include="Day13.cs" />
<Compile Include="Day12.cs" />
<Compile Include="Day11.cs" />
<Compile Include="Day10.cs" />
<Compile Include="Day25.cs" />
<Compile Include="Day24.cs" />
<Compile Include="Day23.cs" />
<Compile Include="Day22.cs" />
<Compile Include="Day9.cs" />
<Compile Include="Day8.cs" />
<Compile Include="Day7.cs" />
<Compile Include="Day6.cs" />
<Compile Include="Day5.cs" />
<Compile Include="Day4.cs" />
<Compile Include="Day3.cs" />
<Compile Include="Day2.cs" />
<Compile Include="Day1.cs" />
<Compile Include="Picker.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
44 changes: 44 additions & 0 deletions Day1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.IO;

class Day1 {
public static int Solution1() {
HashSet<int> solutions = new HashSet<int>();

foreach (int number in Array.ConvertAll(File.ReadAllLines("input1.txt"), int.Parse)) {
int other = 2020 - number;

if (solutions.Contains(number)) {
return other * number;
}
solutions.Add(other);
}

return -1;
}

public static int Solution2() {
Dictionary<int, int> solutions = new Dictionary<int, int>();
int[] numbers = Array.ConvertAll(File.ReadAllLines("input1.txt"), int.Parse);

foreach (int number1 in numbers) {
if (solutions.TryGetValue(number1, out int product)) {
return product * number1;
}

foreach (int number2 in numbers) {
if (number1 == number2) {
continue;
}

int sum = number1 + number2;
if (sum < 2020) {
solutions[2020 - sum] = number1 * number2;
}
}
}

return -1;
}
}
46 changes: 46 additions & 0 deletions Day10.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.IO;

class Day10 {
public static int Solution1() {
int[] adapters = Array.ConvertAll(File.ReadAllLines("input10.txt"), int.Parse);
Array.Sort(adapters);

int diff1 = adapters[0] == 1 ? 1 : 0;
int diff3 = adapters[0] == 3 ? 2 : 1;
for (int i = adapters.Length - 1; i > 0;) {
int diff = adapters[i--] - adapters[i];
if (diff == 1) {
diff1++;
} else if (diff == 3) {
diff3++;
}
}

return diff1 * diff3;
}

public static int Solution2() {
int[] adapters = Array.ConvertAll(File.ReadAllLines("input10.txt"), int.Parse);
Array.Sort(adapters);
long[] arrangements = new long[adapters.Length];

//Count the ways from each adapter to your phone
arrangements[arrangements.Length - 1] = 1;
arrangements[arrangements.Length - 2] = 1;
arrangements[arrangements.Length - 3] = 2;
for (int i = adapters.Length - 4; i >= 0; i--) {
arrangements[i] = arrangements[i + 1];

if (adapters[i + 2] - adapters[i] <= 3) {
arrangements[i] += arrangements[i + 2];
if (adapters[i + 3] - adapters[i] <= 3) {
arrangements[i] += arrangements[i + 3];
}
}
}

Console.WriteLine(arrangements[0] + arrangements[1] + arrangements[2]);
return 0;
}
}
192 changes: 192 additions & 0 deletions Day11.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
using System.Collections.Generic;
using System.IO;

class Day11 {
public static int Solution1() {
string[] input = File.ReadAllLines("input11.txt");
int width = input[0].Length + 2;
int height = input.Length + 2;
int[] seats1 = new int[width * height];
for (int y = 0; y < input.Length; y++) {
int i = (y + 1) * width + 1;
for (int x = 0; x < input[0].Length; x++) {
if (input[y][x] == 'L') {
seats1[i + x] = 1;
}
}
}
int[] seats2 = (int[])seats1.Clone();

int[] offsets = new int[8] { -width - 1, -width, -width + 1, -1, 1, width - 1, width, width + 1 };
for (int round = 1;; round++) {
bool change = false;
int[] from = round % 2 == 0 ? seats1 : seats2;
int[] to = round % 2 == 0 ? seats2 : seats1;

for (int i = 0; i < seats1.Length; i++) {
if (from[i] == 1) {
int result = 2;
foreach (int offset in offsets) {
if (from[i + offset] == 2) {
result = 1;
break;
}
}
change = change || result == 2;
to[i] = result;
} else if (from[i] == 2) {
int count = 0;
foreach (int offset in offsets) {
if (from[i + offset] == 2) {
count++;
}
}
change = change || count >= 4;
to[i] = count >= 4 ? 1 : 2;
}
}

if (!change) {
int count = 0;
foreach (int seat in to) {
if (seat == 2) {
count++;
}
}
return count;
}
}
}

public static int Solution2() {
string[] map = File.ReadAllLines("input11.txt");
int width = map[0].Length;
int height = map.Length;

int count = 0;
int[,] indexMap = new int[map.Length, map[0].Length];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
indexMap[y, x] = map[y][x] == 'L' ? count++ : -1;
}
}

int[][] adjacencyMap = new int[count][];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (indexMap[y, x] == -1) {
continue;
}
List<int> adjacent = new List<int>(8);
//Top left
for (int y2 = y - 1, x2 = x - 1; y2 >= 0 && x2 >= 0; y2--, x2--) {
int i = indexMap[y2, x2];
if (i != -1) {
adjacent.Add(i);
break;
}
}
//Top
for (int y2 = y - 1; y2 >= 0; y2--) {
int i = indexMap[y2, x];
if (i != -1) {
adjacent.Add(i);
break;
}
}
//Top right
for (int y2 = y - 1, x2 = x + 1; y2 >= 0 && x2 < width; y2--, x2++) {
int i = indexMap[y2, x2];
if (i != -1) {
adjacent.Add(i);
break;
}
}
//Left
for (int x2 = x - 1; x2 >= 0; x2--) {
int i = indexMap[y, x2];
if (i != -1) {
adjacent.Add(i);
break;
}
}
//Right
for (int x2 = x + 1; x2 < width; x2++) {
int i = indexMap[y, x2];
if (i != -1) {
adjacent.Add(i);
break;
}
}
//Bottom left
for (int y2 = y + 1, x2 = x - 1; y2 < height && x2 >= 0; y2++, x2--) {
int i = indexMap[y2, x2];
if (i != -1) {
adjacent.Add(i);
break;
}
}
//Bottom
for (int y2 = y + 1; y2 < height; y2++) {
int i = indexMap[y2, x];
if (i != -1) {
adjacent.Add(i);
break;
}
}
//Bottom right
for (int y2 = y + 1, x2 = x + 1; y2 < height && x2 < width; y2++, x2++) {
int i = indexMap[y2, x2];
if (i != -1) {
adjacent.Add(i);
break;
}
}

adjacencyMap[indexMap[y, x]] = adjacent.ToArray();
}
}

bool[] seats1 = new bool[count];
bool[] seats2 = new bool[count];

for (int round = 1; ; round++) {
bool change = false;
bool[] from = round % 2 == 0 ? seats1 : seats2;
bool[] to = round % 2 == 0 ? seats2 : seats1;

for (int i = 0; i < seats1.Length; i++) {
if (from[i]) {
int seen = 0;
foreach (int adjacent in adjacencyMap[i]) {
if (from[adjacent]) {
seen++;
}
}
change = change || seen >= 5;
to[i] = seen < 5;
} else {
bool result = true;
foreach (int adjacent in adjacencyMap[i]) {
if (from[adjacent]) {
result = false;
break;
}
}
change = change || result;
to[i] = result;
}
}

if (!change) {
int total = 0;
foreach (bool seat in to) {
if (seat) {
total++;
}
}
return total;
}
}
}
}
Loading

0 comments on commit f1395e5

Please sign in to comment.