Last active
December 6, 2018 17:03
-
-
Save stipebosnjak/0eb84e0131fad2d155b96439108f84e1 to your computer and use it in GitHub Desktop.
Advent 6.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Runtime.InteropServices; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace Aoc.Puzzles | |
{ | |
class _6 | |
{ | |
public void Run() | |
{ | |
var inputLines = File.ReadAllLines("Puzzles/6.txt"); | |
char letter = 'A'; | |
var points = new List<Point>(); | |
foreach (var inputLine in inputLines) | |
{ | |
var split = inputLine.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries); | |
var x = Convert.ToInt32(split[0]); | |
var y = Convert.ToInt32(split[1]); | |
points.Add(new Point() {X = x, Y = y, Char = letter}); | |
letter++; | |
} | |
Grow(points); | |
// var test = new int[points.Max(x => x.X), points.Max(x => x.Y)]; | |
//var distances = calculate(test); | |
} | |
private void Grow(List<Point> points) | |
{ | |
var sb = new StringBuilder(); | |
var allPoints = new List<Point>(); | |
var expandedPoints = points.SelectMany(x => x.Expand()); | |
for (int i = 0; i < 10; i++) | |
{ | |
var dupes = expandedPoints | |
.GroupBy(x => x.ToString()) | |
.Select(x => new | |
{ | |
Point = x.Key, | |
Count = x.Count() | |
}) | |
.Where(x => x.Count > 1) | |
.Select(x => x.Point); | |
var bla = expandedPoints.Where(x => dupes.Contains(x.ToString())); | |
allPoints.AddRange(bla); | |
expandedPoints = bla.SelectMany(x => x.Expand()); | |
PrintPoints(allPoints, points.Max(x => x.X), points.Max(x => x.Y)); | |
} | |
// PrintPoints(allPoints, points.Max(x => x.X), points.Max(x => x.Y)); | |
} | |
private void PrintPoints(List<Point> points, int maxX, int maxY) | |
{ | |
var sb = new StringBuilder(); | |
for (int y = 0; y < maxY; y++) | |
{ | |
for (int x = 0; x < maxX; x++) | |
{ | |
var point = points.FirstOrDefault(p => p.X == x && p.Y == y); | |
if (point != null) | |
{ | |
sb.Append(point.Char); | |
} | |
else | |
{ | |
sb.Append("0"); | |
} | |
} | |
sb.AppendLine(); | |
} | |
File.WriteAllText("Puzzles/6_gen.txt",sb.ToString()); | |
Console.WriteLine(sb.ToString()); | |
} | |
} | |
public class Point : IEqualityComparer<Point> | |
{ | |
public int X { get; set; } | |
public int Y { get; set; } | |
public bool Expanded { get; set; } | |
public List<Point> AffectedArea { get; set; } | |
public Point(int x, int y, char letter) | |
{ | |
X = x; | |
Y = y; | |
Char = letter; | |
} | |
public IEnumerable<Point> Expand() | |
{ | |
Expanded = true; | |
yield return new Point(X, Y + 1, char.ToLower(Char)); | |
yield return new Point(X + 1, Y + 1, char.ToLower(Char)); | |
yield return new Point(X + 1, Y, char.ToLower(Char)); | |
yield return new Point(X + 1, Y - 1, char.ToLower(Char)); | |
yield return new Point(X, Y - 1, char.ToLower(Char)); | |
yield return new Point(X - 1, Y - 1, char.ToLower(Char)); | |
yield return new Point(X - 1, Y, char.ToLower(Char)); | |
yield return new Point(X - 1, Y + 1, char.ToLower(Char)); | |
} | |
public char Char { get; set; } | |
public Point() | |
{ | |
AffectedArea = new List<Point>(); | |
} | |
public bool Equals(Point a, Point b) | |
{ | |
return a.X == b.X && a.Y == b.Y; | |
} | |
public override string ToString() | |
{ | |
return X + "," + Y; | |
} | |
public int GetHashCode(Point obj) | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment