Skip to content

Instantly share code, notes, and snippets.

@jyarbro
Created December 3, 2018 20:29
Show Gist options
  • Save jyarbro/666399b339f09df5352d97948c379031 to your computer and use it in GitHub Desktop.
Save jyarbro/666399b339f09df5352d97948c379031 to your computer and use it in GitHub Desktop.
Advent of Code 2018 Day 3
class Day3_2018 {
const string INPUTFILE = @"2018\day3.txt";
public void Part1() {
var input = File.ReadAllLines(INPUTFILE);
var dimension = 1000;
var area = dimension * dimension;
var fabric = new int[area];
foreach (var line in input) {
var match = Regex.Match(line, @"#(\d+)? @ (\d+)?,(\d+)?: (\d+)?x(\d+)?");
var id = Convert.ToInt32(match.Groups[1].Value);
var x = Convert.ToInt32(match.Groups[2].Value);
var y = Convert.ToInt32(match.Groups[3].Value);
var w = Convert.ToInt32(match.Groups[4].Value);
var h = Convert.ToInt32(match.Groups[5].Value);
for (var row = y; row < y + h; row++) {
for (var col = x; col < x + w; col++) {
var i = (row * 1000) + col;
fabric[i]++;
}
}
}
var total = 0;
for (var i = 0; i < area; i++) {
if (fabric[i] > 1) {
total++;
}
}
Console.WriteLine(total);
}
public void Part2() {
var input = File.ReadAllLines(INPUTFILE);
var claims = new Dictionary<int, Claim>();
var dimension = 1000;
var area = dimension * dimension;
var fabric = new int[area];
foreach (var line in input) {
var match = Regex.Match(line, @"#(\d+)? @ (\d+)?,(\d+)?: (\d+)?x(\d+)?");
var id = Convert.ToInt32(match.Groups[1].Value);
var x = Convert.ToInt32(match.Groups[2].Value);
var y = Convert.ToInt32(match.Groups[3].Value);
var w = Convert.ToInt32(match.Groups[4].Value);
var h = Convert.ToInt32(match.Groups[5].Value);
claims.Add(id, new Claim(x, y, w, h));
}
var claimed = new List<int>();
foreach (var claim in claims) {
for (var row = claim.Value.Y; row < claim.Value.Y + claim.Value.H; row++) {
for (var col = claim.Value.X; col < claim.Value.X + claim.Value.W; col++) {
var i = (row * 1000) + col;
// If this space was already claimed, make sure we track the existing claim ID and the new claim ID.
if (fabric[i] > 0) {
claimed.Add(fabric[i]);
claimed.Add(claim.Key);
}
fabric[i] = claim.Key;
}
}
}
claimed = claimed.Distinct().ToList();
var found = new List<int>();
for (var i = 0; i < area; i++) {
if (fabric[i] > 0) {
if (!claimed.Contains(fabric[i])) {
found.Add(fabric[i]);
}
}
}
found = found.Distinct().ToList();
// Ideally this prints out only one number.
Console.WriteLine("Found: " + string.Join(", ", found));
}
class Claim {
public int X { get; set; }
public int Y { get; set; }
public int W { get; set; }
public int H { get; set; }
public Claim(int x, int y, int w, int h) {
X = x;
Y = y;
W = w;
H = h;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment