Repository navigation
Expand file tree
/
Copy pathDay_three.cs
More file actions
101 lines (89 loc) · 3.06 KB
/
Copy pathDay_three.cs
File metadata and controls
101 lines (89 loc) · 3.06 KB
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
101
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Avent_du_code_2019
{
class Day_three : Day
{
private List<string> puzzleinput2 = new List<string>();
public Day_three()
{
file = "daythreeinput.txt";
Read_file(file);
}
public override int Puzzle1()
{
List<string> wire1 = Mapping(puzzleinput);
List<string> wire2 = Mapping(puzzleinput2);
List<string> nodes = wire1.Intersect(wire2).ToList();
nodes.RemoveAt(0);
List<int> results = new List<int>();
foreach(string node in nodes)
{
string[] d = node.Split(';');
results.Add(Math.Abs(Int32.Parse(d[0])) + Math.Abs(Int32.Parse(d[1])));
}
return results.Min();
}
public override int Puzzle2()
{
List<string> wire1 = Mapping(puzzleinput);
List<string> wire2 = Mapping(puzzleinput2);
string firstnode = wire1.Intersect(wire2).ToList().ElementAt(1);
wire1 = wire1.GetRange(0, wire1.IndexOf(firstnode));
wire2 = wire2.GetRange(0, wire2.IndexOf(firstnode));
return wire1.Count + wire2.Count;
}
public List<string> Mapping(List<string> instructions)
{
List<string> coords = new List<string> { "0;0" };
int x = 0, y = 0;
foreach(string travel in instructions)
{
char direction = travel.ElementAt(0);
if (Int32.TryParse(travel.Remove(0, 1), out int n))
{
while (n != 0)
{
if(direction == 'U')
{
x++;
n--;
}
else if (direction == 'D')
{
x--;
n--;
}
else if (direction == 'R')
{
y++;
n--;
}
else if (direction == 'L')
{
y--;
n--;
}
coords.Add(x + ";" + y);
}
}
}
return coords;
}
public override void Read_file(string filename)
{
base.Read_file(filename);
string[] text;
if (File.Exists(path))
text = File.ReadAllText(path).Split('\n');
else
return;
puzzleinput = text[0].Split(',').ToList();
puzzleinput2 = text[1].Split(',').ToList();
}
}
}