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 pathDay3.php
More file actions
194 lines (164 loc) · 3.76 KB
/
Day3.php
File metadata and controls
194 lines (164 loc) · 3.76 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
183
184
185
186
187
188
189
190
191
192
193
194
<?php
class Layer {
/** @var int */
public $count;
/** @var int */
public $start;
/** @var int */
public $end;
/** @var int */
public $lineWidth;
/**
* Layer constructor.
*
* @param int $start
* @param int $lineWidth
*
* @internal param int $end
*/
public function __construct($start, $lineWidth)
{
$this->count = $lineWidth * 4 - 4;
$this->start = $start;
$this->lineWidth = $lineWidth;
$this->end = $start + $this->count - 1;
}
/**
* @return Layer
*/
public function newLayer()
{
return new self($this->end + 1, $this->lineWidth + 2);
}
}
class Field {
/** @var int */
public $id;
/** @var int */
public $value;
/** @var Layer */
public $layer;
/** @var int */
public $layerPosition;
/** @var int */
public $linePosition;
/**
* Field constructor.
*
* @param int $id
* @param Layer $layer
*/
public function __construct($id, Layer $layer)
{
$this->id = $id;
$this->layer = $layer;
$this->layerPosition = $this->id - $this->layer->start;
$this->linePosition = $this->layerPosition % $this->layer->lineWidth;
}
/**
* @return Field
*/
public function newField()
{
$layer = $this->layer;
if ($this->id == $this->layer->end) {
$layer = $layer->newLayer();
}
return new self($this->id+1, $layer);
}
/**
* @return bool
*/
public function isEndOfLine()
{
return $this->linePosition == $this->layer->lineWidth;
}
/**
* @return bool
*/
public function isStartOfLine()
{
return $this->linePosition == 1;
}
/**
* @return bool
*/
public function isEndOfLayer()
{
return $this->id == $this->layer->end;
}
/**
* @return int
*/
public function stepIn()
{
if ($this->isEndOfLayer()) {
return $this->layer->start;
}
if ($this->isEndOfLine() || $this->isStartOfLine()) {
return $this->id -1;
}
return 0;
}
}
function calcDistance($lineWidth, $posInLayer)
{
$half = ($lineWidth) / 2;
$posInLine = $posInLayer % $lineWidth;
$step1 = $posInLine > $half ? $posInLine - $half : $half - $posInLine;
$step2 = $half;
return $step1 + $step2;
}
$input = 265149;
$input = 12;
$input = 2;
//$input = 23;
//puzzel 1
$layer = new Layer(1, 1);
while ($layer->end < $input) {
$layer = $layer->newLayer();
}
$field = new Field($input, $layer);
echo "start {$field->layer->start} - end {$field->layer->end} - linewidth {$field->layer->lineWidth}, count {$field->layer->count}, position in layer: {$field->layerPosition}\n";
echo "steps " . calcDistance($field->layer->lineWidth - 1, $field->layerPosition) . "\n";
//return;
//puzzel 2
/** USE https://oeis.org/A141481 */
//$grid = [];
//
//$layer = new Layer(1, 1);
//$field = new Field(1, $layer);
//$field->value = 1;
//$grid[$field->id] = $field;
//
//$field = $field->newField();
//$field->value = 1;
//$grid[$field->id] = $field;
//
//$sum = 1;
//$last = $field;
//while ($sum < $input) {
// /** @var Field $last */
// /** @var Field $field */
// $field = $last->newField();
// $toSum = [];
// $toSum[] = $last;
//
// /** @var Field $stepIn */
//// if (!$field->isStartOfLine() && !$field->isEndOfLine()) {
//// $stepIn = $grid[$field->stepIn()];
//// $toSum[] = $stepIn;
//// if (!$stepIn->isEndOfLine()) {
//// $toSum[] = $grid[$stepIn->id + 1];
//// }
////
//// }
//
// $sum = 266330;
//
//// $toSum[] = $grid[$last->stepIn()];
//
// $last = $field;
//}
//
//echo $sum . "\n";