Day 04
This commit is contained in:
parent
5261410cbf
commit
b96e0a4c32
@ -1,4 +1,3 @@
|
||||
using System.IO;
|
||||
namespace AdventOfCode2022.Handlers
|
||||
{
|
||||
public class Day03
|
||||
|
79
Handlers/Day04.cs
Normal file
79
Handlers/Day04.cs
Normal file
@ -0,0 +1,79 @@
|
||||
namespace AdventOfCode2022.Handlers
|
||||
{
|
||||
public class Day04
|
||||
{
|
||||
string[] pairs = new string[] {};
|
||||
public Day04()
|
||||
{
|
||||
pairs = File.ReadAllText(@"./days/04/input").Trim().Split("\n");
|
||||
}
|
||||
|
||||
public void PartA()
|
||||
{
|
||||
var count = 0;
|
||||
foreach (var pair in pairs)
|
||||
{
|
||||
var split = pair.Split(",");
|
||||
var first = split[0];
|
||||
var second = split[1];
|
||||
|
||||
var firstMin = int.Parse(first.Split("-")[0]);
|
||||
var firstMax = int.Parse(first.Split("-")[1]);
|
||||
|
||||
var secondMin = int.Parse(second.Split("-")[0]);
|
||||
var secondMax = int.Parse(second.Split("-")[1]);
|
||||
|
||||
if (firstMin <= secondMin && firstMax >= secondMax)
|
||||
{
|
||||
count++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (secondMin <= firstMin && secondMax >= firstMax)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine($"Part A: {count}");
|
||||
}
|
||||
|
||||
public void PartB()
|
||||
{
|
||||
var count = 0;
|
||||
foreach (var pair in pairs)
|
||||
{
|
||||
var split = pair.Split(",");
|
||||
var first = split[0];
|
||||
var second = split[1];
|
||||
|
||||
var firstMin = int.Parse(first.Split("-")[0]);
|
||||
var firstMax = int.Parse(first.Split("-")[1]);
|
||||
|
||||
var secondMin = int.Parse(second.Split("-")[0]);
|
||||
var secondMax = int.Parse(second.Split("-")[1]);
|
||||
|
||||
for (var i = firstMin; i <= firstMax; i++)
|
||||
{
|
||||
var shouldLoop = true;
|
||||
for (var j = secondMin; j <= secondMax; j++)
|
||||
{
|
||||
if (i == j)
|
||||
{
|
||||
count++;
|
||||
shouldLoop = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!shouldLoop)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine($"Part B: {count}");
|
||||
}
|
||||
}
|
||||
}
|
10
Program.cs
10
Program.cs
@ -9,6 +9,7 @@ namespace AdventOfCode2022
|
||||
dayOne();
|
||||
dayTwo();
|
||||
dayThree();
|
||||
dayFour();
|
||||
}
|
||||
catch (Exception e) {
|
||||
Console.WriteLine(e.Message);
|
||||
@ -46,5 +47,14 @@ namespace AdventOfCode2022
|
||||
|
||||
PrintSeparator();
|
||||
}
|
||||
|
||||
static void dayFour() {
|
||||
Console.WriteLine("Day 04");
|
||||
var day = new Day04();
|
||||
day.PartA();
|
||||
day.PartB();
|
||||
|
||||
PrintSeparator();
|
||||
}
|
||||
}
|
||||
}
|
47
days/04/index.md
Normal file
47
days/04/index.md
Normal file
@ -0,0 +1,47 @@
|
||||
\--- 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?_
|
1000
days/04/input
Normal file
1000
days/04/input
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user