Skip to content

Commit a0c23a0

Browse files
committed
2023.13
1 parent a0c2393 commit a0c23a0

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed

php/2023/13/01.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Year2023\Day13;
4+
5+
$start = microtime(true);
6+
7+
#$in = file_get_contents('example.txt');
8+
$in = file_get_contents('input.txt');
9+
10+
$blocks = explode("\n\n", $in);
11+
12+
class Map {
13+
private array $map = [];
14+
15+
private array $byCols = [];
16+
17+
public function __construct(string $raw)
18+
{
19+
foreach (explode("\n", $raw) as $line) {
20+
$this->map[] = str_split($line);
21+
}
22+
23+
for ($i = 0, $max = count($this->map[0]); $i < $max; $i++) {
24+
$this->byCols[] = array_column($this->map, $i);
25+
}
26+
}
27+
28+
private function findFold(array $in): ?int
29+
{
30+
for ($i = 0, $max=count($in)-1; $i<$max; $i++) {
31+
if ($this->isFold($in, $i)) {
32+
# echo $i+1, "\n";
33+
return $i+1;
34+
}
35+
}
36+
return null;
37+
}
38+
39+
private function isFold(array $in, int $lineNumber): bool
40+
{
41+
$a = $lineNumber;
42+
$b = $lineNumber+1;
43+
while (isset($in[$a], $in[$b])) {
44+
if ($in[$a] !== $in[$b]) {
45+
return false;
46+
}
47+
--$a;
48+
++$b;
49+
}
50+
51+
return true;
52+
}
53+
54+
public function getScore(): int
55+
{
56+
if (null !== ($score = $this->findFold($this->map))) {
57+
return $score * 100;
58+
}
59+
60+
if (null !== ($score = $this->findFold($this->byCols))) {
61+
return $score;
62+
}
63+
64+
throw new \RuntimeException('it should reflect but does not? :(');
65+
}
66+
67+
}
68+
69+
$sum = 0;
70+
foreach ($blocks as $block) {
71+
$map = new Map($block);
72+
$sum += $map->getScore();
73+
}
74+
75+
76+
echo "\n", $sum, "\n";
77+
78+
79+
echo microtime(true) - $start;
80+
echo "\n";
81+

php/2023/13/02.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Year2023\Day13;
4+
5+
6+
$start = microtime(true);
7+
8+
#$in = file_get_contents('example.txt');
9+
$in = file_get_contents('input.txt');
10+
11+
$blocks = explode("\n\n", $in);
12+
13+
class Map {
14+
private array $map = [];
15+
16+
private array $byCols = [];
17+
18+
public function __construct(string $raw)
19+
{
20+
foreach (explode("\n", $raw) as $line) {
21+
$this->map[] = str_split($line);
22+
}
23+
24+
for ($i = 0, $max = count($this->map[0]); $i < $max; $i++) {
25+
$this->byCols[] = array_column($this->map, $i);
26+
}
27+
}
28+
29+
private function findFold(array $in): ?int
30+
{
31+
for ($i = 0, $max=count($in)-1; $i<$max; $i++) {
32+
if ($this->isFold($in, $i)) {
33+
# echo $i+1, "\n";
34+
return $i+1;
35+
}
36+
}
37+
return null;
38+
}
39+
40+
private function isFold(array $in, int $lineNumber): bool
41+
{
42+
$diff = 0;
43+
$a = $lineNumber;
44+
$b = $lineNumber+1;
45+
while (isset($in[$a], $in[$b])) {
46+
$diff += count(array_diff_assoc($in[$a], $in[$b]));
47+
if ($diff > 1) {
48+
return false;
49+
}
50+
--$a;
51+
++$b;
52+
}
53+
54+
return $diff === 1;
55+
}
56+
57+
public function getScore(): int
58+
{
59+
if (null !== ($score = $this->findFold($this->map))) {
60+
return $score * 100;
61+
}
62+
63+
if (null !== ($score = $this->findFold($this->byCols))) {
64+
return $score;
65+
}
66+
67+
throw new \RuntimeException('it should reflect but does not? :(');
68+
}
69+
70+
}
71+
72+
$sum = 0;
73+
foreach ($blocks as $block) {
74+
$map = new Map($block);
75+
$sum += $map->getScore();
76+
}
77+
78+
79+
echo "\n", $sum, "\n";
80+
81+
82+
echo microtime(true) - $start;
83+
echo "\n";
84+

php/2023/13/example.txt

162 Bytes
Binary file not shown.

php/2023/13/input.txt

17.1 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