chore(*): Move old solutions into a separate folder

This commit is contained in:
Josh Creek
2023-11-30 12:51:45 +00:00
parent ed3eb9cb54
commit b78d79a3af
117 changed files with 141 additions and 141 deletions
+21
View File
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_09</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Creek.HelpfulExtensions" Version="1.3.0" />
</ItemGroup>
<ItemGroup>
<None Update="input.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
+235
View File
@@ -0,0 +1,235 @@
using Creek.HelpfulExtensions;
namespace Day09
{
class Program
{
public static int arraySizeX;
public static int arraySizeY;
static void Main(string[] args)
{
string[] lines = File.ReadAllLines("input.txt");
//Part1(lines);
Part2(lines);
}
static void Part1(string[] lines)
{
arraySizeX = lines[0].Length;
arraySizeY = lines.Count();
int[,] array = new int[arraySizeX, arraySizeY];
foreach (var (line, index) in lines.WithIndex())
{
for (int i = 0; i < line.Length; i++)
{
array[i,index] = int.Parse(line[i].ToString());
}
}
List<int> lowPoints = new List<int>();
// Check each number in the array
for (int i = 0; i < arraySizeX; i++)
{
for (int j = 0; j < arraySizeY; j++)
{
if (IsLowPoint(array, i, j))
{
lowPoints.Add(array[i,j]);
}
}
}
// Risk level of a low point is 1 plus height
int totalRiskLevel = lowPoints.Sum() + lowPoints.Count();
Console.WriteLine($"Total Risk Level: {totalRiskLevel}");
}
static void Part2(string[] lines)
{
arraySizeX = lines[0].Length;
arraySizeY = lines.Count();
int[,] array = new int[arraySizeX, arraySizeY];
foreach (var (line, index) in lines.WithIndex())
{
for (int i = 0; i < line.Length; i++)
{
array[i, index] = int.Parse(line[i].ToString());
}
}
List<(int x, int y)> lowPointLocations = new List<(int x, int y)>();
// Check each number in the array
for (int i = 0; i < arraySizeX; i++)
{
for (int j = 0; j < arraySizeY; j++)
{
if (IsLowPoint(array, i, j))
{
lowPointLocations.Add((i, j));
}
}
}
// There is a basin for every low point, bounded by 9s and the edge of the array
// Find these basins, along with the count of how many locations are in each basin
List<int> basinLocationCount = new List<int>();
foreach ((int x, int y) location in lowPointLocations)
{
// For each low point, find all the locations within its basin
Console.WriteLine("Starting new basin");
List<(int x, int y)> locationsInBasin = new List<(int x, int y)>();
FindBasinLocations(array, ref locationsInBasin, location);
basinLocationCount.Add(locationsInBasin.Count());
}
Console.WriteLine("==============");
foreach (var item in basinLocationCount)
{
Console.WriteLine(item);
}
List<int> threeHighest = basinLocationCount.OrderByDescending(x => x).Take(3).ToList();
int mult = threeHighest.Aggregate((x, y) => x * y);
Console.WriteLine($"Total Basin Locations: {basinLocationCount.Count()} Multiplying their sizes together: {mult}");
}
static void FindBasinLocations(int[,] array, ref List<(int x, int y)> locationsInBasin, (int x, int y) location)
{
List<(int x, int y)> relativeLocations = new List<(int x, int y)>()
{
//(location.x - 1, location.y - 1),
//(location.x, location.y - 1),
//(location.x + 1, location.y - 1),
//(location.x - 1, location.y),
//(location.x + 1, location.y),
//(location.x - 1, location.y + 1),
//(location.x, location.y + 1),
//(location.x + 1, location.y + 1),
};
// REMEMBER THIS DOESN'T TAKE INTO ACCOUNT DIAGONALS
try
{
//relativeLocations.Add((location.x - 1, location.y - 1));
relativeLocations.Add((location.x, location.y - 1));
//relativeLocations.Add((location.x + 1, location.y - 1));
relativeLocations.Add((location.x - 1, location.y));
relativeLocations.Add((location.x + 1, location.y));
//relativeLocations.Add((location.x - 1, location.y + 1));
relativeLocations.Add((location.x, location.y + 1));
//relativeLocations.Add((location.x + 1, location.y + 1));
}
catch (Exception ex)
{
Console.WriteLine($"{location.x},{location.y} errored");
}
// For every direction, if there's a 9 or no element stop, otherwise, go again from that location, excluding locations we've already considered
foreach ((int x, int y) relativeLocation in relativeLocations)
{
int newX = relativeLocation.x;
int newY = relativeLocation.y;
bool condition1 = IsNotHighPointOrOutOfArray(array, newX, newY);
bool condition2 = condition1 ? array[newX, newY] < 9 : false;
bool condition3 = condition1 ? !locationsInBasin.Contains((newX, newY)) : false;
//Console.WriteLine($"{condition1} - {condition2} - {condition3}");
if (condition1 && condition2 && condition3)
{
locationsInBasin.Add((newX, newY));
Console.WriteLine($"{newX},{newY} added with value {array[newX, newY]}");
FindBasinLocations(array, ref locationsInBasin, relativeLocation);
}
}
}
static bool IsNotHighPointOrOutOfArray(int[,] array, int x, int y)
{
if (IsInArray(x, y) && array[x, y] < 9)
{
return true;
}
else
{
return false;
}
}
static bool IsLowPoint(int[,] array, int x, int y)
{
// top left x-1, y-1
// top middle x, y-1
// top right x+1, y-1
// left x-1, y
// right x+1, y
// bottom left x-1, y+1
// bottom middle x, y+1
// bottom right x+1, y+1
// Compare all EXISTING positions around the current position and if any of them are lowest than the value in the current position return false
int currentPosition = array[x, y];
if (IsInArray(x-1, y-1) && array[x-1, y-1] < currentPosition)
{
return false;
}
else if (IsInArray(x, y - 1) && array[x, y - 1] < currentPosition)
{
return false;
}
else if (IsInArray(x + 1, y - 1) && array[x + 1, y - 1] < currentPosition)
{
return false;
}
else if (IsInArray(x - 1, y) && array[x - 1, y] < currentPosition)
{
return false;
}
else if (IsInArray(x + 1, y) && array[x + 1, y] < currentPosition)
{
return false;
}
else if (IsInArray(x - 1, y + 1) && array[x - 1, y + 1] < currentPosition)
{
return false;
}
else if (IsInArray(x, y + 1) && array[x, y + 1] < currentPosition)
{
return false;
}
else if (IsInArray(x + 1, y + 1) && array[x + 1, y + 1] < currentPosition)
{
return false;
}
else
{
Console.WriteLine($"{x},{y}: {currentPosition}");
return true;
}
}
static bool IsInArray(int x, int y)
{
if (x >= 0 && x < arraySizeX && y >= 0 && y < arraySizeY)
{
return true;
}
else
{
return false;
}
}
}
}
+21
View File
@@ -0,0 +1,21 @@
--- Day 9: Smoke Basin ---
These caves seem to be lava tubes. Parts are even still volcanically active; small hydrothermal vents release smoke into the caves that slowly settles like rain.
If you can model how the smoke flows through the caves, you might be able to avoid it and be that much safer. The submarine generates a heightmap of the floor of the nearby caves for you (your puzzle input).
Smoke flows to the lowest point of the area it's in. For example, consider the following heightmap:
2199943210
3987894921
9856789892
8767896789
9899965678
Each number corresponds to the height of a particular location, where 9 is the highest and 0 is the lowest a location can be.
Your first goal is to find the low points - the locations that are lower than any of its adjacent locations. Most locations have four adjacent locations (up, down, left, and right); locations on the edge or corner of the map have three or two adjacent locations, respectively. (Diagonal locations do not count as adjacent.)
In the above example, there are four low points, all highlighted: two are in the first row (a 1 and a 0), one is in the third row (a 5), and one is in the bottom row (also a 5). All other locations on the heightmap have some lower adjacent location, and so are not low points.
The risk level of a low point is 1 plus its height. In the above example, the risk levels of the low points are 2, 1, 6, and 6. The sum of the risk levels of all low points in the heightmap is therefore 15.
Find all of the low points on your heightmap. What is the sum of the risk levels of all low points on your heightmap?
+38
View File
@@ -0,0 +1,38 @@
--- Part Two ---
Next, you need to find the largest basins so you know what areas are most important to avoid.
A basin is all locations that eventually flow downward to a single low point. Therefore, every low point has a basin, although some basins are very small. Locations of height 9 do not count as being in any basin, and all other locations will always be part of exactly one basin.
The size of a basin is the number of locations within the basin, including the low point. The example above has four basins.
The top-left basin, size 3:
2199943210
3987894921
9856789892
8767896789
9899965678
The top-right basin, size 9:
2199943210
3987894921
9856789892
8767896789
9899965678
The middle basin, size 14:
2199943210
3987894921
9856789892
8767896789
9899965678
The bottom-right basin, size 9:
2199943210
3987894921
9856789892
8767896789
9899965678
Find the three largest basins and multiply their sizes together. In the above example, this is 9 * 14 * 9 = 1134.
What do you get if you multiply together the sizes of the three largest basins?
+100
View File
@@ -0,0 +1,100 @@
4567894301299921298789654345689439843295436789543298765432345678986789756901239998765634567895986555
3458963212989890989678943234678998764989945678932198764321456799215678949892398999854525658934895434
2348954329876799876567899195789469999767896989949019965432567897434569998789987898743212345695679423
1237895498765698765479998989890359878956789999898929876545678996545698987678976987654563456789798912
3456797987854569954398987678921298766545678998777899987766789987656987676569895498765676567899987893
4567989896543498765987876567892987655434567897656798799897894598767899565458789329876787678999876789
5679878789432579879876743458989876542123456996545987678998923459878998433347678910987898789998865678
8798765678921456989865432365678987678012345989439878569899012398989987321234567899998949898987654568
9987654688932368998765421234567898632125499878998767476789323987399876510126789998769434967896543456
2398543567893458999876510195978976543236989656989654345895439876210965432345678989754323456987432345
1987652356954567899985431989899987654549878949878943234976545985431986583556899679943212569898541234
0198621237899778999996569878789698765698765436567890125987689987642398874567976567894323498789210123
9239432348998989998987698765679549878789876323456789239898992398543498765678965456789434999654321234
8998543459987899987898798754678932989899985434567894398789943987659569898789654329899949898765432345
7987674569876789876789987643767891296929876765678965987656894998998979979898763210169898769896543456
6599795678965699965679876321458942965439987878989879898545899899987898965919874323456789656987654578
5459898789654569874589765432349899876598998989894998765434789789876567894301986545678998734598787689
4345979896543498763459896565456789987987989998763219896545697698765456965212397696989654321239898793
3254567965432987654567987886767891299876767899854323987676789569876345894343498989898975434345999892
0123979879521098975678998997878910198965656789967456798789893498985256799454569978797896545656789991
1239899998632129996789989798989992987654547899996567899893912987654345678965698765686789656767899989
4398789876543234989899976659999989998743234598989699989932102398965457789898789654345898787878999878
6987654997654399878999865545898678999654123987678989876545293459878968898769899983236789898989998767
9876563498969989769899854236789567898763234976567878989676989598989879997656999874345899949899989756
9765432349898876658789743145678978999854569865453569998789878987899989789743298765456789234789876545
9897561298767435345697653234567899989967698774312478999898767856789997678932109876567892125678987634
8999990129854321234589754745678999879878987685101387899997656345678954567893212987678943034599299846
7898889298743210125678965678989998767989999543215456999898743237567893478954563499789956745989398767
6787678987654321367889876989999887656799898654323567898765432123456789589765679599899877659878989878
5674569898765432456789987898998786545698798765434678979878741012367998678978798989924989798767878989
4323498789878543567897698967899654323987679878555789567997652323458998789989987678912398987856567899
3212347678989697689976569458789765219886587989676894456989543456767899893499876567893987996543456789
2101234567898798797895435365698954398775456898789932349879656567878954901298775456789896789612345678
3232345678979899896789423234567965699674345679894321298968987678989543212987654345698765678923458989
4563656789765932945678910123679899986545234569965432987857899799898954329898765257789674567934567897
5674767899854321234789543234598798997632123678996949876546978998767899998789954348998543456895688976
6789898998765430123897654345987687889875012399989899985434569987658998875657895767987432388996789765
7895919987654321234998765499876576778964323989878789876323568998899987764546789879876321276989899876
8954323498765434345699878987655485567895439876567678987212567899999876543534995989865410125678999987
9765434679876595456789989987643213458996545985434569874323456789998765432323894399986423234789998998
7976765789987876567994399876543201246789759886323498765434569899879894321012789459876534345689987989
6989876894598987878973234997875212345678998765434999876545679998767987652345678967987649876798765678
5492987923679598989862125987654345456789329876565899987786789987656399543458789978998956987899654568
6321098912395439799654434598765456567893212987876789699898899876543297654567893989879987898998743489
5499999101987325678969547679976787998999543498989896545999932999632198775878932398767898969997632378
6987893245896534567898956789987898949698956569193987659899891098949019896989991987654329459876541267
9876789356789645678987897996598989434567897692012798998789789987898923987899989998763212345998765459
8765678969898756789256789543459876323456789789125679989678679876767994598999878989854301456789876567
5874789998979867894345678932398765614347899899434599876534598765656789679998767678965212367899987678
4343467897767998965456789321459654301256789998765987988323989854345678999987654589654343479999898789
3212989926456999987567895432396543212367899899899876543219876543236799989876543458965494989698769894
4343499212347894398878989693987665323456789789989987695499965432125678978985432367896989896569654923
5454578901456943219989878989998786734569897679878798986989876743234599769876921456999976789696543219
6567689212567899324599967678999897895878944598765659987976987654345678956989890967898765698987654998
7678998343456898935679756567899998976989432129654745698965498765458789545698789899987654567898769897
8789997654597987899798743456789879987896545098743434899876569896567890124987698768894323458999998786
9897898765989876568987654567898765698989652196542123987987678989698921239876567656789214567899989645
9956789879876543456798765678989854349678943987663039876498789878999632345985454347898996789979878434
9745698998767432345679896789876543234567894598754198765339899867996543459876321238957889895659762123
8634567899654321238789997898987654349978965679865299873210998759889654569985432349546778954349843234
6525898998765432345893298967898795998899876789876789954321989645678965678996543489435568893234956785
5436789129887643458932139456789989876789987895987899977439876434599876789987687678923456789125987996
6545679012998786567891012345899876765898998934598999876567965323689987895398798789212345699934598987
9876789199129987698972357896789995854567899325679999987679873212347898943219999892103479789895679298
3987899988997598789765456789899984323456789212389989898989932101236789992103498943212567998789792149
2198979876789459899896987999939876434598994323459878799999876512345699983212987654323456789678989234
3239567965679345999989898998929987545989987654598765634789965423457999874323498767654567898567878945
4999459854567956789878789987898797659876998765965834323679876567568998765454999878765678997434569896
9878398765699897898765679976545698998965789899894321014567987698989549878969891989889789986545789789
9765129876986789989874798765434569987654891998789543223478998789794323989998710195999897987696897678
8654234989875679876743459983125689998785992989678965354567899895699456799987623234599976798789977567
9766545698764568995432369894012378999876789878567896765678932923988998899876544395989897899898765456
9878656799853656789321298765623567894987898765456989896789671019977789921987895989878789978987632345
9989867987942345693210129898794678923498949987345678987896532198866678930298999876765678954596543456
9896979876543456789521345999898789213999239876239789498986545987755567891979998765874567893987656567
8755699987654567897432567899989892109878998765458991349997679876643456789767987654323456912998767778
5444598798767678996543456789876989298767879986767890145989998965432387893458998765534567899889898989
9323497679989789987894568998765678987656767897878921239876897896521298932347899876765678965678999496
8939986545698993498989979349654349876545856998989432398965986789430989549456910997876889954567894325
7898965434577892109879893298743212965432147899996545987654875678949878998967891298989997892349993214
6587894323456943998765789349892109876543236789987659876543534567898767897898932999292356793498989323
5456789434567899899854578998765412987656745893299767985452123679987656786789999899101236789987879434
4345678946678998789943469549894329898767856794999879876321014989699542345999987678912345699996768965
3235789987889997678912568932985498769878967989899989965432127894598431259899976567893456789865657896
2124899998999876589893467899876987654989879876789999876543236893987640198788893478999567998754346789
4235998989998765498794878945987896543296999765678998987664345992987651987677789567898979899893235678
5345987878999654346689989234598987654345989654567897898785456789298769876545678978957898789762124568
5459876567898953234579890123499998785459876543678965439976567892129898987634389899545987689654335679
6798765456587892145656789294989899896567985432349876521989878921019987896521256789434596578965445799
7999874343456921012345999989876789987679876545678965439997989532198756965432345694323987459878556789
9899983212457892123456789876765695498789987658789997698976596545349867896545696789109876367989769899
9789874343468943234567899985634789219898998969899989987897697655459878987856789895414995499999878998
8676965656578998745678999894323478901987899878999879876789798798767989498979899954323986989899989987
6565698767889109656789698765434567899876789989898765765678999899878995349989998765456899876789899886
5454569878999998767895549898745679978987897696789654344567899945989965210995439876787898965678789765
4323456989769899878934135987656789567898976545698743213456789434597894329874321987898987654565678954
5434567893656789989321014598967993456799895434997654302345678923456789459765210298959996543534567893
6547978932345678995432323459978942367898754329876543212456789014567896598654354349543985432123489932
7656889321256789876543434567899431234789843212987655433469894323478987698765565458932976543234567891
8798995432367899989656657678976532345678932101298766545678965435689998799876676567891098764546678910