Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int[,] canvas = null;
- void Main()
- {
- var aoc = new AdventOfCode(2018, 3);
- var input = new string[]{ "#1 @ 1,3: 4x4", "#2 @ 3,1: 4x4", "#3 @ 5,5: 2x2", }.Select(x=> new Paper(x)).ToArray();
- //test if all is working
- Debug.Assert(FillCanvas(10, input) == 4);
- Debug.Assert(FindSoloID(input) == 3);
- //get results
- input = aoc.InputLines.Select(x=> new Paper(x)).ToArray();
- //do your thing
- aoc.SubmitAnswer(FillCanvas(1000, input), Part.A);
- aoc.SubmitAnswer(FindSoloID(input), Part.B);
- }
- //class for each Paper part
- class Paper
- {
- public int ID;
- public int xStart;
- public int yStart;
- public int width;
- public int height;
- Regex regex = new Regex(@"#(\d+) @ (\d+),(\d+): (\d+)x(\d+)");
- public Paper(string line)
- {
- var match = regex.Match(line);
- this.ID = int.Parse(match.Groups[1].Value);
- this.xStart = int.Parse(match.Groups[2].Value);
- this.yStart = int.Parse(match.Groups[3].Value);
- this.width = int.Parse(match.Groups[4].Value);
- this.height = int.Parse(match.Groups[5].Value);
- }
- }
- int FillCanvas(int size, Paper[] papers)
- {
- //create canvas
- canvas = new int[size,size];
- //check each paper
- foreach (var paper in papers)
- {
- //draw each paper
- for (int x = paper.xStart; x < paper.xStart + paper.width; x++)
- {
- for (int y = paper.yStart; y < paper.yStart + paper.height; y++)
- {
- //increment for how many papers are on this position
- canvas[x, y]++;
- }
- }
- }
- //flatten and count >= 2
- return canvas.Cast<int>().Where(x=> x >= 2).Count();
- }
- int FindSoloID(Paper[] papers)
- {
- //check all papers
- foreach (var paper in papers)
- {
- for (int x = paper.xStart; x < paper.xStart + paper.width; x++)
- {
- for (int y = paper.yStart; y < paper.yStart + paper.height; y++)
- {
- //this paper has more than 1, so not it
- if (canvas[x, y] != 1) goto FAILED;
- }
- }
- //this one has all
- return paper.ID;
- FAILED:
- continue;
- }
- throw new ObjectNotFoundException();
- }
- // Define other methods and classes here
Add Comment
Please, Sign In to add comment