Initial Commit
This commit is contained in:
10
2022/Day04CSharp/Day04CSharp.csproj
Normal file
10
2022/Day04CSharp/Day04CSharp.csproj
Normal file
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
121
2022/Day04CSharp/Program.cs
Normal file
121
2022/Day04CSharp/Program.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
//const string fileName = @"example-input.txt";
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Unicode;
|
||||
|
||||
const string puzzleFileName = @"puzzle-input.txt";
|
||||
const string exampleFileName = @"example-input.txt";
|
||||
|
||||
var exampleData = new LoadedData(exampleFileName);
|
||||
var puzzleData = new LoadedData(puzzleFileName);
|
||||
|
||||
Console.WriteLine(String.Format("Part01, ExampleFullyContains: {0}, PuzzleFullyContains {1}", exampleData.GetFullyContainsCount(), puzzleData.GetFullyContainsCount()));
|
||||
Console.WriteLine(String.Format("Part02, ExampleOverlaps: {0}, PuzzleOverlaps {1}", exampleData.GetOverlapsCount(), puzzleData.GetOverlapsCount()));
|
||||
|
||||
class LoadedData
|
||||
{
|
||||
public class Assignment
|
||||
{
|
||||
public struct Range
|
||||
{
|
||||
public Range() { }
|
||||
public Range(int l, int r)
|
||||
{
|
||||
Low = l; High = r;
|
||||
}
|
||||
|
||||
public int Low { get; set; }
|
||||
public int High { get; set; }
|
||||
}
|
||||
|
||||
public Range Left;
|
||||
public Range Right;
|
||||
|
||||
public bool FullyContains
|
||||
{
|
||||
get
|
||||
{
|
||||
if ( Left.Low >= Right.Low && Left.High <= Right.High
|
||||
|| Right.Low >= Left.Low && Right.High <= Left.High)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Overlaps
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Left.High < Right.Low || Right.High < Left.Low)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<Assignment> Assignments = new List<Assignment>();
|
||||
|
||||
public int GetFullyContainsCount()
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
foreach (var a in Assignments)
|
||||
{
|
||||
if (a.FullyContains) count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
public int GetOverlapsCount()
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
foreach (var a in Assignments)
|
||||
{
|
||||
if (a.Overlaps) count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
public LoadedData(string fileName)
|
||||
{
|
||||
using (StreamReader reader = File.OpenText(fileName))
|
||||
{
|
||||
while (!reader.EndOfStream)
|
||||
{
|
||||
string? line = reader.ReadLine();
|
||||
|
||||
if (line == null)
|
||||
{
|
||||
throw new InvalidDataException();
|
||||
}
|
||||
|
||||
var ranges = line.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
|
||||
if (ranges.Length != 2)
|
||||
{
|
||||
throw new InvalidDataException();
|
||||
}
|
||||
|
||||
var rangeLeft = ranges[0].Split('-');
|
||||
var rangeRight = ranges[1].Split('-');
|
||||
|
||||
Assignment a = new Assignment();
|
||||
a.Left.Low = Int32.Parse(rangeLeft[0]);
|
||||
a.Left.High = Int32.Parse(rangeLeft[1]);
|
||||
a.Right.Low = Int32.Parse(rangeRight[0]);
|
||||
a.Right.High = Int32.Parse(rangeRight[1]);
|
||||
|
||||
Assignments.Add(a);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
8
2022/Day04CSharp/Properties/launchSettings.json
Normal file
8
2022/Day04CSharp/Properties/launchSettings.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"profiles": {
|
||||
"Day04CSharp": {
|
||||
"commandName": "Project",
|
||||
"workingDirectory": "C:\\dev\\DevSandbox\\AdventOfCode\\2022\\Day04CSharp"
|
||||
}
|
||||
}
|
||||
}
|
||||
6
2022/Day04CSharp/example-input.txt
Normal file
6
2022/Day04CSharp/example-input.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
2-4,6-8
|
||||
2-3,4-5
|
||||
5-7,7-9
|
||||
2-8,3-7
|
||||
6-6,4-6
|
||||
2-6,4-8
|
||||
59
2022/Day04CSharp/problem.txt
Normal file
59
2022/Day04CSharp/problem.txt
Normal file
@@ -0,0 +1,59 @@
|
||||
--- Day 4: Camp Cleanup ---
|
||||
|
||||
Space needs to be cleared before the last supplies can be unloaded from the ships, and so several Elves have been assigned the job of cleaning up sections of the camp. Every section has a unique ID number, and each Elf is assigned a range of section IDs.
|
||||
|
||||
However, as some of the Elves compare their section assignments with each other, they've noticed that many of the assignments overlap. To try to quickly find overlaps and reduce duplicated effort, the Elves pair up and make a big list of the section assignments for each pair (your puzzle input).
|
||||
|
||||
For example, consider the following list of section assignment pairs:
|
||||
|
||||
2-4,6-8
|
||||
2-3,4-5
|
||||
5-7,7-9
|
||||
2-8,3-7
|
||||
6-6,4-6
|
||||
2-6,4-8
|
||||
|
||||
For the first few pairs, this list means:
|
||||
|
||||
Within the first pair of Elves, the first Elf was assigned sections 2-4 (sections 2, 3, and 4), while the second Elf was assigned sections 6-8 (sections 6, 7, 8).
|
||||
The Elves in the second pair were each assigned two sections.
|
||||
The Elves in the third pair were each assigned three sections: one got sections 5, 6, and 7, while the other also got 7, plus 8 and 9.
|
||||
|
||||
This example list uses single-digit section IDs to make it easier to draw; your actual list might contain larger numbers. Visually, these pairs of section assignments look like this:
|
||||
|
||||
.234..... 2-4
|
||||
.....678. 6-8
|
||||
|
||||
.23...... 2-3
|
||||
...45.... 4-5
|
||||
|
||||
....567.. 5-7
|
||||
......789 7-9
|
||||
|
||||
.2345678. 2-8
|
||||
..34567.. 3-7
|
||||
|
||||
.....6... 6-6
|
||||
...456... 4-6
|
||||
|
||||
.23456... 2-6
|
||||
...45678. 4-8
|
||||
|
||||
Some of the pairs have noticed that one of their assignments fully contains the other. For example, 2-8 fully contains 3-7, and 6-6 is fully contained by 4-6. In pairs where one assignment fully contains the other, one Elf in the pair would be exclusively cleaning sections their partner will already be cleaning, so these seem like the most in need of reconsideration. In this example, there are 2 such pairs.
|
||||
|
||||
In how many assignment pairs does one range fully contain the other?
|
||||
|
||||
--- Part Two ---
|
||||
|
||||
It seems like there is still quite a bit of duplicate work planned. Instead, the Elves would like to know the number of pairs that overlap at all.
|
||||
|
||||
In the above example, the first two pairs (2-4,6-8 and 2-3,4-5) don't overlap, while the remaining four pairs (5-7,7-9, 2-8,3-7, 6-6,4-6, and 2-6,4-8) do overlap:
|
||||
|
||||
5-7,7-9 overlaps in a single section, 7.
|
||||
2-8,3-7 overlaps all of the sections 3 through 7.
|
||||
6-6,4-6 overlaps in a single section, 6.
|
||||
2-6,4-8 overlaps in sections 4, 5, and 6.
|
||||
|
||||
So, in this example, the number of overlapping assignment pairs is 4.
|
||||
|
||||
In how many assignment pairs do the ranges overlap?
|
||||
1000
2022/Day04CSharp/puzzle-input.txt
Normal file
1000
2022/Day04CSharp/puzzle-input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user