mirror of
https://github.com/jcreek/advent-of-code.git
synced 2026-07-13 03:03:46 +00:00
chore(*): Move old solutions into a separate folder
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Day_03
|
||||
{
|
||||
public class Claim
|
||||
{
|
||||
public int ClaimId { get; set; }
|
||||
public bool CellsUsedMoreThanOnce { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
||||
<RootNamespace>Day_03</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,128 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Day_03
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// Declare an empty 2D array with 1000x1000 cells, material[x,y] from left and top respectively
|
||||
int[,] material = new int[1000, 1000];
|
||||
List<Claim> claims = new List<Claim>();
|
||||
|
||||
var lines = File.ReadAllLines("input.txt");
|
||||
// For each line of the input file
|
||||
foreach (var line in lines)
|
||||
{
|
||||
// A claim like #123 @ 3,2: 5x4 means that claim ID 123 specifies a rectangle 3 inches from the left edge, 2 inches from the top edge, 5 inches wide, and 4 inches tall.
|
||||
|
||||
// Remove the id number and everything prior to the left edge measurement
|
||||
int startIndex = line.IndexOf("@") + 1;
|
||||
int endIndex = line.Length;
|
||||
var lineData = line.Substring(startIndex, endIndex - startIndex);
|
||||
|
||||
// Store all the data
|
||||
Regex re1 = new Regex(@"(@ [0-9]+,)");
|
||||
var leftPositionList = re1.Matches(line);
|
||||
string leftPosition = leftPositionList[0].Value.Substring(2, leftPositionList[0].Value.Length -1 - 2);
|
||||
|
||||
Regex re2 = new Regex(@"(,[0-9]+:)");
|
||||
var topPositionList = re2.Matches(line);
|
||||
string topPosition = topPositionList[0].Value.Substring(1, topPositionList[0].Value.Length -1 - 1);
|
||||
|
||||
Regex re3 = new Regex(@"(: [0-9]+x)");
|
||||
var widthList = re3.Matches(line);
|
||||
int width = Int32.Parse(widthList[0].Value.Substring(1, widthList[0].Value.Length -1 - 1));
|
||||
|
||||
Regex re4 = new Regex(@"(x[0-9]+)");
|
||||
var lengthList = re4.Matches(line);
|
||||
int length = Int32.Parse(lengthList[0].Value.Substring(1));
|
||||
|
||||
// Store data in all cells horizontally and vertically
|
||||
for (int i = 0; i < width; i++) {
|
||||
for (int j = 0; j < length; j++) {
|
||||
// Store a count of how many times the cell is used at the appropriate co-ordinates
|
||||
material[(Int32.Parse(leftPosition) + i), (Int32.Parse(topPosition) + j)] += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Part 1
|
||||
|
||||
// Loop through both arrays and count how many cells have a recorded overlap
|
||||
int overlapCounter = 0;
|
||||
for(int i = 0; i < 1000; i++){
|
||||
for(int j = 0; j < 1000; j++){
|
||||
if(material[i, j] > 1){
|
||||
overlapCounter += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Console.WriteLine(material[0, 0]);
|
||||
Console.WriteLine(material.Length);
|
||||
Console.WriteLine("The number of overlapping square inches is " + overlapCounter);
|
||||
|
||||
|
||||
// Part 2
|
||||
|
||||
// Loop through all claims (lines in the file) and find the one where the material is only used once in every cell
|
||||
|
||||
// For each line of the input file
|
||||
foreach (var line in lines)
|
||||
{
|
||||
var claim = new Claim();
|
||||
// A claim like #123 @ 3,2: 5x4 means that claim ID 123 specifies a rectangle 3 inches from the left edge, 2 inches from the top edge, 5 inches wide, and 4 inches tall.
|
||||
|
||||
// Remove the id number and everything prior to the left edge measurement
|
||||
int startIndex = line.IndexOf("@") + 1;
|
||||
int endIndex = line.Length;
|
||||
var lineData = line.Substring(startIndex, endIndex - startIndex);
|
||||
|
||||
// Store all the data
|
||||
Regex re0 = new Regex(@"(#(\d+))");
|
||||
var claimIdList = re0.Matches(line);
|
||||
string claimId = claimIdList[0].Value.Substring(1);
|
||||
claim.ClaimId = Int32.Parse(claimId);
|
||||
|
||||
Regex re1 = new Regex(@"(@ [0-9]+,)");
|
||||
var leftPositionList = re1.Matches(line);
|
||||
string leftPosition = leftPositionList[0].Value.Substring(2, leftPositionList[0].Value.Length -1 - 2);
|
||||
|
||||
Regex re2 = new Regex(@"(,[0-9]+:)");
|
||||
var topPositionList = re2.Matches(line);
|
||||
string topPosition = topPositionList[0].Value.Substring(1, topPositionList[0].Value.Length -1 - 1);
|
||||
|
||||
Regex re3 = new Regex(@"(: [0-9]+x)");
|
||||
var widthList = re3.Matches(line);
|
||||
int width = Int32.Parse(widthList[0].Value.Substring(1, widthList[0].Value.Length -1 - 1));
|
||||
|
||||
Regex re4 = new Regex(@"(x[0-9]+)");
|
||||
var lengthList = re4.Matches(line);
|
||||
int length = Int32.Parse(lengthList[0].Value.Substring(1));
|
||||
|
||||
// Store data in all cells horizontally and vertically
|
||||
for (int i = 0; i < width; i++) {
|
||||
for (int j = 0; j < length; j++) {
|
||||
// Check how many times the cell has been used at the appropriate co-ordinates
|
||||
// If it has been used more than once, set CellsUsedMoreThanOnce to true
|
||||
if (material[(Int32.Parse(leftPosition) + i), (Int32.Parse(topPosition) + j)] > 1)
|
||||
{
|
||||
claim.CellsUsedMoreThanOnce = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
claims.Add(claim);
|
||||
}
|
||||
|
||||
// Find the only claim that has no overlap
|
||||
|
||||
int finalClaimId = claims.First(c => !c.CellsUsedMoreThanOnce).ClaimId;
|
||||
Console.WriteLine("The ID of the only claim that doesn't overlap is: " + finalClaimId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
--- Day 3: No Matter How You Slice It ---
|
||||
The Elves managed to locate the chimney-squeeze prototype fabric for Santa's suit (thanks to someone who helpfully wrote its box IDs on the wall of the warehouse in the middle of the night). Unfortunately, anomalies are still affecting them - nobody can even agree on how to cut the fabric.
|
||||
|
||||
The whole piece of fabric they're working on is a very large square - at least 1000 inches on each side.
|
||||
|
||||
Each Elf has made a claim about which area of fabric would be ideal for Santa's suit. All claims have an ID and consist of a single rectangle with edges parallel to the edges of the fabric. Each claim's rectangle is defined as follows:
|
||||
|
||||
The number of inches between the left edge of the fabric and the left edge of the rectangle.
|
||||
The number of inches between the top edge of the fabric and the top edge of the rectangle.
|
||||
The width of the rectangle in inches.
|
||||
The height of the rectangle in inches.
|
||||
A claim like #123 @ 3,2: 5x4 means that claim ID 123 specifies a rectangle 3 inches from the left edge, 2 inches from the top edge, 5 inches wide, and 4 inches tall. Visually, it claims the square inches of fabric represented by # (and ignores the square inches of fabric represented by .) in the diagram below:
|
||||
|
||||
...........
|
||||
...........
|
||||
...#####...
|
||||
...#####...
|
||||
...#####...
|
||||
...#####...
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
The problem is that many of the claims overlap, causing two or more claims to cover part of the same areas. For example, consider the following claims:
|
||||
|
||||
#1 @ 1,3: 4x4
|
||||
#2 @ 3,1: 4x4
|
||||
#3 @ 5,5: 2x2
|
||||
Visually, these claim the following areas:
|
||||
|
||||
........
|
||||
...2222.
|
||||
...2222.
|
||||
.11XX22.
|
||||
.11XX22.
|
||||
.111133.
|
||||
.111133.
|
||||
........
|
||||
The four square inches marked with X are claimed by both 1 and 2. (Claim 3, while adjacent to the others, does not overlap either of them.)
|
||||
|
||||
If the Elves all proceed with their own plans, none of them will have enough fabric. How many square inches of fabric are within two or more claims?
|
||||
@@ -0,0 +1,6 @@
|
||||
--- Part Two ---
|
||||
Amidst the chaos, you notice that exactly one claim doesn't overlap by even a single square inch of fabric with any other claim. If you can somehow draw attention to it, maybe the Elves will be able to make Santa's suit after all!
|
||||
|
||||
For example, in the claims above, only claim 3 is intact after all claims are made.
|
||||
|
||||
What is the ID of the only claim that doesn't overlap?
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user