Skip to content

Commit a0c0bd4

Browse files
committed
2023.09
1 parent a0cf85e commit a0c0bd4

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

php/2023/09/01.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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('input.txt', FILE_IGNORE_NEW_LINES);
9+
10+
$seqs = [];
11+
foreach ($lines as $line) {
12+
$seqs[] = explode(" ", $line);
13+
}
14+
15+
16+
17+
function findNextInSequence (array $in): int
18+
{
19+
$dims[] = $newLine = $in;
20+
while (!isAllZeros($newLine)) {
21+
$dims[] = $newLine = getNextRow($newLine);
22+
}
23+
24+
for ($i = count($dims)-1; $i > 0; $i--) {
25+
$dims[$i-1][] = $dims[$i-1][array_key_last($dims[$i-1])] + $dims[$i][array_key_last($dims[$i])];
26+
}
27+
28+
# print_r($dims);
29+
30+
return $dims[0][array_key_last($dims[0])];
31+
}
32+
33+
function getNextRow(array $in): array
34+
{
35+
$next = [];
36+
for ($i = 0, $count = count($in); $i < $count-1; $i++) {
37+
$next[] = $in[$i+1] - $in[$i];
38+
}
39+
return $next;
40+
}
41+
42+
function isAllZeros(array $in): bool
43+
{
44+
foreach ($in as $i) {
45+
if ($i !== 0) {
46+
return false;
47+
}
48+
}
49+
return true;
50+
}
51+
52+
$sum = 0;
53+
foreach ($seqs as $seq) {
54+
$sum += findNextInSequence($seq);
55+
}
56+
57+
echo "\n", $sum, "\n";
58+
59+
echo microtime(true) - $start;
60+
echo "\n";
61+

php/2023/09/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+
$start = microtime(true);
6+
7+
#$lines = file('example.txt', FILE_IGNORE_NEW_LINES);
8+
$lines = file('input.txt', FILE_IGNORE_NEW_LINES);
9+
10+
$seqs = [];
11+
foreach ($lines as $line) {
12+
$seqs[] = explode(" ", $line);
13+
}
14+
15+
16+
17+
function findPreviousInSequence (array $in): int
18+
{
19+
$dims[] = $newLine = $in;
20+
while (!isAllZeros($newLine)) {
21+
$dims[] = $newLine = getNextRow($newLine);
22+
}
23+
24+
for ($i = count($dims)-1; $i > 0; $i--) {
25+
$dims[$i-1][] = $dims[$i-1][array_key_last($dims[$i-1])] + $dims[$i][array_key_last($dims[$i])]; // part1
26+
array_unshift($dims[$i-1], $dims[$i-1][0] - $dims[$i][0]); // part2
27+
}
28+
29+
# print_r($dims);
30+
31+
return $dims[0][0];
32+
}
33+
34+
function getNextRow(array $in): array
35+
{
36+
$next = [];
37+
for ($i = 0, $count = count($in); $i < $count-1; $i++) {
38+
$next[] = $in[$i+1] - $in[$i];
39+
}
40+
return $next;
41+
}
42+
43+
function isAllZeros(array $in): bool
44+
{
45+
foreach ($in as $i) {
46+
if ($i !== 0) {
47+
return false;
48+
}
49+
}
50+
return true;
51+
}
52+
53+
$sum = 0;
54+
foreach ($seqs as $seq) {
55+
$sum += findPreviousInSequence($seq);
56+
}
57+
58+
echo "\n", $sum, "\n";
59+
60+
echo microtime(true) - $start;
61+
echo "\n";
62+

php/2023/09/example.txt

68 Bytes
Binary file not shown.

php/2023/09/input.txt

20.9 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