-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09.cs
100 lines (90 loc) · 3.42 KB
/
day09.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
namespace Adventofcode.Tag._9
{
class Program
{
static Point head = new Point(0,0); // [X-Achse (L/R), Y-Achse (U/D)]
static Point tail = new Point(0, 0);
static List<Point> tails = new List<Point>();
static List<Point> visited = new List<Point>();
const int taillenght = 9;
static void Main(string[] args)
{
var input = File.ReadAllText("../../input.txt").Split('\n').Where(s => s.Length > 0).ToList();
// Teil 1
foreach (var line in input)
moveHead(line.Split(' ')[0], Convert.ToInt32(line.Split(' ')[1]), false);
Console.WriteLine(visited.Count); // 5683
// Teil 2
visited = new List<Point>();
for (int i = 0; i < taillenght; i++)
tails.Add(new Point());
foreach (var line in input)
moveHead(line.Split(' ')[0], Convert.ToInt32(line.Split(' ')[1]), true);
Console.WriteLine(visited.Count); // 2372
}
static void moveHead(string direction, int distance, bool longTail)
{
for (int i = 0; i < distance; i++)
{
switch (direction)
{
case "U":
head.Y++;
break;
case "D":
head.Y--;
break;
case "R":
head.X++;
break;
case "L":
head.X--;
break;
default:
throw new NotImplementedException();
}
if (!longTail)
tail = moveTail(head, tail, true);
else
tails = moveTails(tails);
}
}
static Point moveTail(Point headPoint, Point tailPoint, bool isLastTail)
{
var offset = new Point(headPoint.X - tailPoint.X, headPoint.Y - tailPoint.Y);
if ((Math.Abs(offset.Y) == 2 && Math.Abs(offset.X) >= 1) || (Math.Abs(offset.X) == 2 && Math.Abs(offset.Y) >= 1))
{
tailPoint.Y = offset.Y > 0 ? tailPoint.Y + 1 : tailPoint.Y - 1;
tailPoint.X = offset.X > 0 ? tailPoint.X + 1 : tailPoint.X - 1;
}
else if (offset.X > 1 && offset.Y == 0)
tailPoint.X++;
else if (offset.X < -1 && offset.Y == 0)
tailPoint.X--;
else if (offset.Y > 1 && offset.X == 0)
tailPoint.Y++;
else if (offset.Y < -1 && offset.X == 0)
tailPoint.Y--;
if (isLastTail && !visited.Contains(tailPoint))
visited.Add(tailPoint);
return tailPoint;
}
private static List<Point> moveTails(List<Point> points)
{
for (int i = 0; i < taillenght; i++)
{
if (i == 0)
points[i] = moveTail(head, points[i], false);
else
points[i] = moveTail(points[i - 1], points[i], i == taillenght - 1);
}
return points;
}
}
}