Created
December 8, 2019 11:40
-
-
Save Happsson/5dd5c92481c69a602f4d9f489c21a93d to your computer and use it in GitHub Desktop.
Day 6 of AoC 2019 in unity.
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.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using System.Linq; | |
public class Day6 : MonoBehaviour | |
{ | |
public TextAsset input; | |
public class Node | |
{ | |
public Node parent; | |
public string name; | |
public int depth; | |
public List<Node> children = new List<Node>(); | |
public GameObject gameObj; | |
public void SetChild(Node child) | |
{ | |
children.Add(child); | |
child.gameObj.transform.SetParent(gameObj.transform); | |
child.gameObj.transform.position = gameObj.transform.position + ((new Vector3(Random.Range(-1, 1f), Random.Range(-1, 1f), 0).normalized * Random.Range(5,30))); | |
child.parent = this; | |
} | |
} | |
public GameObject nodePref; | |
public Node root; | |
private List<Node> allNodes = new List<Node>(); | |
// Start is called before the first frame update | |
void Start() | |
{ | |
string[] orbs = input.text.Split('\n'); | |
StartCoroutine("Part1", orbs); | |
} | |
void Part2() | |
{ | |
Node you = GetNode("YOU"); | |
Node san = GetNode("SAN"); | |
List<Node> path1 = new List<Node>(); | |
Node n = you.parent; | |
while (n.parent != null) | |
{ | |
path1.Add(n); | |
n = n.parent; | |
} | |
List<Node> path2 = new List<Node>(); | |
Node j = san.parent; | |
while (j.parent != null) | |
{ | |
path2.Add(j); | |
j = j.parent; | |
} | |
Node lowestParent = you; | |
for(int i = 0; i <path1.Count; i++) | |
{ | |
if (path2.Contains(path1[i])) | |
{ | |
Debug.Log("Highest common parent: " + path1[i].name); | |
lowestParent = path1[i]; | |
break; | |
} | |
} | |
n = you.parent; | |
int chain = 0; | |
while(n != lowestParent) | |
{ | |
n.gameObj.GetComponent<SphereRotate>().SetColor(Color.blue); | |
chain++; | |
n = n.parent; | |
} | |
n = san.parent; | |
while (n != lowestParent) | |
{ | |
n.gameObj.GetComponent<SphereRotate>().SetColor(Color.blue); | |
chain++; | |
n = n.parent; | |
} | |
Debug.Log(chain); | |
} | |
IEnumerator Part1(string[] orbs) | |
{ | |
foreach (string o in orbs) | |
{ | |
string[] objs = o.Split(')'); | |
Node center = GetNode(objs[0]); | |
Node child = GetNode(objs[1]); | |
if(center.name == "YOU") | |
{ | |
center.gameObj.GetComponent<SphereRotate>().SetColor(Color.green); | |
} | |
if (child.name == "YOU") | |
{ | |
child.gameObj.GetComponent<SphereRotate>().SetColor(Color.green); | |
} | |
if (center.name == "SAN") | |
{ | |
center.gameObj.GetComponent<SphereRotate>().SetColor(Color.red); | |
} | |
if (child.name == "SAN") | |
{ | |
child.gameObj.GetComponent<SphereRotate>().SetColor(Color.red); | |
} | |
center.SetChild(child); | |
yield return null; | |
Debug.Log("Tick"); | |
} | |
int totalOrbs = 0; | |
foreach(Node n in allNodes) | |
{ | |
int depth = 0; | |
Node p = n; | |
while (p.parent != null) | |
{ | |
p = p.parent; | |
depth++; | |
} | |
n.depth = depth; | |
totalOrbs += depth; | |
} | |
Debug.Log(totalOrbs); | |
Part2(); | |
} | |
Node GetNode(string n) | |
{ | |
if (root == null) | |
{ | |
GameObject g = Instantiate(nodePref); | |
g.name = n; | |
root = new Node() { depth = 0, name = n, gameObj = g }; | |
allNodes.Add(root); | |
return root; | |
} | |
else | |
{ | |
Node search = (from Node nn in allNodes where nn.name == n select nn).FirstOrDefault(); | |
if (search == null) | |
{ | |
GameObject g = Instantiate(nodePref); | |
g.name = n; | |
Node nn = new Node() { depth = 0, name = n, gameObj = g }; | |
allNodes.Add(nn); | |
return nn; | |
} | |
else | |
{ | |
return search; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment