Skip to content

Commit a0cf85e

Browse files
committed
2023.08
1 parent a0c2a34 commit a0cf85e

File tree

7 files changed

+106
-2
lines changed

7 files changed

+106
-2
lines changed

php/2023/07/02.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ public function getSortScore(): int
150150

151151
$sum = 0;
152152
foreach ($hands as $pos => $hand) {
153-
echo $hand->getSortScore(), " ", $hand->raw, ": ", ($pos + 1), " * ", $hand->getBid(), " => ", ($partScore = (($pos + 1) * $hand->getBid())), "\n";
154-
$sum += $partScore;
153+
#echo $hand->getSortScore(), " ", $hand->raw, ": ", ($pos + 1), " * ", $hand->getBid(), " => ", ($partScore = (($pos + 1) * $hand->getBid())), "\n";
154+
$sum += (($pos + 1) * $hand->getBid());
155155
}
156156

157157
echo "\n", $sum, "\n";

php/2023/08/01.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Year2023\Day08;
4+
5+
$start = microtime(true);
6+
7+
#$lines = file('example.txt', FILE_IGNORE_NEW_LINES);
8+
#$lines = file('example2.txt', FILE_IGNORE_NEW_LINES);
9+
$lines = file('input.txt', FILE_IGNORE_NEW_LINES);
10+
11+
$rl = str_split(array_shift($lines));
12+
array_shift($lines);
13+
14+
$map = [];
15+
foreach ($lines as $line) {
16+
if (!preg_match('/(\w+)\s=\s\((\w+),\s(\w+)\)/', $line, $out)) {
17+
throw new \Exception('no match: ', $line);
18+
}
19+
[, $a, $b, $c] = $out;
20+
$map[$a] = [$b, $c];
21+
}
22+
23+
$current = 'AAA';
24+
$stopAt = 'ZZZ';
25+
26+
$steps = 0;
27+
while ($current !== $stopAt) {
28+
$rightOrLeft = $rl[$steps % count($rl)];
29+
if ($rightOrLeft === 'L') {
30+
$current = $map[$current][0];
31+
} else {
32+
$current = $map[$current][1];
33+
}
34+
++$steps;
35+
}
36+
37+
38+
echo "\n", $steps, "\n";
39+
40+
echo microtime(true) - $start;
41+
echo "\n";
42+

php/2023/08/02.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Year2023\Day08;
4+
5+
use common\math;
6+
7+
$start = microtime(true);
8+
9+
require_once '../../common/math.php';
10+
11+
12+
#$lines = file('example3.txt', FILE_IGNORE_NEW_LINES);
13+
$lines = file('input.txt', FILE_IGNORE_NEW_LINES);
14+
15+
$rl = str_split(array_shift($lines));
16+
array_shift($lines);
17+
18+
$map = [];
19+
foreach ($lines as $line) {
20+
if (!preg_match('/(\w+)\s=\s\((\w+),\s(\w+)\)/', $line, $out)) {
21+
throw new \Exception('no match: ', $line);
22+
}
23+
[, $a, $b, $c] = $out;
24+
$map[$a] = [$b, $c];
25+
}
26+
27+
$currentPositions = [];
28+
29+
foreach (array_keys($map) as $mk) {
30+
if ($mk[2] === 'A') {
31+
$currentPositions[] = $mk;
32+
}
33+
}
34+
35+
36+
function stepsToZ($map, $rl, $start): int {
37+
$steps = 0;
38+
$current = $start;
39+
while ($current[2] !== 'Z') {
40+
$rightOrLeft = $rl[$steps % count($rl)];
41+
if ($rightOrLeft === 'L') {
42+
$current = $map[$current][0];
43+
} else {
44+
$current = $map[$current][1];
45+
}
46+
++$steps;
47+
}
48+
return $steps;
49+
}
50+
51+
$stepsToZ = [];
52+
foreach ($currentPositions as $current) {
53+
$stepsToZ[] = stepsToZ($map, $rl, $current);
54+
}
55+
56+
57+
58+
echo "\n", math::leastCommonMultiple(...$stepsToZ), "\n";
59+
60+
echo microtime(true) - $start;
61+
echo "\n";
62+

php/2023/08/example.txt

144 Bytes
Binary file not shown.

php/2023/08/example2.txt

77 Bytes
Binary file not shown.

php/2023/08/example3.txt

161 Bytes
Binary file not shown.

php/2023/08/input.txt

12.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy