This repository was archived by the owner on Jul 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay7.php
More file actions
182 lines (148 loc) · 3.96 KB
/
Day7.php
File metadata and controls
182 lines (148 loc) · 3.96 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
class Program {
private $name;
private $weight;
/** @var Program */
private $parent;
/** @var array|Program[] */
private $children = [];
private $childString = '';
private $totalWeight;
/**
* Program constructor.
* @param $name
*/
public function __construct($name, $weight)
{
$this->name = $name;
$this->weight = $weight;
}
public static function fromNameString($nameString)
{
$exp = explode(" ", $nameString);
$name = $exp[0];
$weight = trim($exp[1], "()");
return new self($name, $weight);
}
public function addChild(Program $newChild)
{
if (!in_array($newChild, $this->children)) {
$this->children[] = $newChild;
$newChild->setParent($this);
}
}
/**
* @return array|Program[]
*/
public function getChildren()
{
return $this->children;
}
public function setParent(Program $newParent) {
$this->parent = $newParent;
$newParent->addChild($this);
}
public function hasParent()
{
return $this->parent instanceof Program;
}
public function getName()
{
return $this->name;
}
public function getWeight()
{
return $this->weight;
}
public function getTotalWeight()
{
if (is_null($this->totalWeight)) {
foreach ($this->children as $child) {
$this->totalWeight += $child->getTotalWeight();
}
$this->totalWeight += $this->weight;
}
return $this->totalWeight;
}
public function isBalanced() {
$lastWeight = 0;
foreach ($this->children as $child) {
if ($lastWeight == 0) {
$lastWeight = $child->getTotalWeight();
}
if ($lastWeight <> $child->getTotalWeight()) {
return false;
}
}
return true;
}
public function setChildString($chldString)
{
$this->childString = trim($chldString);
}
public function getChildString()
{
return $this->childString;
}
public function getChildNames()
{
return explode(', ', $this->childString);
}
public function hasChildString()
{
return strlen($this->childString) > 0;
}
/**
* @return $this|Program
*/
public function findUnbalancedNode() {
foreach ($this->children as $child) {
if (!$child->isBalanced()) {
return $child->findUnbalancedNode();
}
}
return $this;
}
}
/** @var Program[] $programs */
$programs = [];
$input = explode("\n", file_get_contents('input7'));
foreach ($input as $line) {
$lineParts = explode("->", $line);
$program = Program::fromNameString($lineParts[0]);
$programs[$program->getName()] = $program;
if (isset($lineParts[1])) {
$program->setChildString($lineParts[1]);
}
}
foreach ($programs as $program) {
if ($program->hasChildString()) {
foreach ($program->getChildNames() as $childName) {
$program->addChild($programs[$childName]);
}
}
}
foreach ($programs as $program) {
if (!$program->hasParent()) {
$parent = $program;
}
}
echo "Parent = " . $parent->getName() . "\n";
$unbalancedNode = $parent->findUnbalancedNode();
echo "Unbalanced node = " . $unbalancedNode->getName() . " -> {$unbalancedNode->getWeight()}\n";
$weights = [];
foreach ($unbalancedNode->getChildren() as $child) {
@$weights[$child->getTotalWeight()]++;
}
foreach ($weights as $weight => $count) {
if ($count > 1) {
$normalWeight = $weight;
}
}
foreach ($unbalancedNode->getChildren() as $child) {
if ($child->getTotalWeight() <> $normalWeight) {
$diff = $normalWeight - $child->getTotalWeight();
$weight = $child->getWeight() + $diff;
echo "{$child->getName()} weight should be {$weight}\n" ;
}
}