Skip to content

Commit b792273

Browse files
committed
2024/12
1 parent 854e5f3 commit b792273

File tree

11 files changed

+376
-8
lines changed

11 files changed

+376
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![php v8.4](https://shields.io/badge/php-8.4-blue?logo=php)
44
![build](https://img.shields.io/github/actions/workflow/status/tbali0524/advent-of-code-solutions/qa.yml)
5-
![AoC stars](https://img.shields.io/badge/total%20AoC%20⭐-472-green)
5+
![AoC stars](https://img.shields.io/badge/total%20AoC%20⭐-474-green)
66
![license](https://img.shields.io/github/license/tbali0524/advent-of-code-solutions)
77

88
* [AoC website](https://adventofcode.com/)

input/2024/Aoc2024Day12.txt

Lines changed: 140 additions & 0 deletions
Large diffs are not rendered by default.

input/2024/Aoc2024Day12ex1.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
AAAA
2+
BBCD
3+
BBCC
4+
EEEC

input/2024/Aoc2024Day12ex2.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
OOOOO
2+
OXOXO
3+
OOOOO
4+
OXOXO
5+
OOOOO

input/2024/Aoc2024Day12ex3.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
RRRRIICCFF
2+
RRRRIICCCF
3+
VVRRRCCFFF
4+
VVRCCCJFFF
5+
VVVVCJJCFE
6+
VVIVCCJJEE
7+
VVIIICJJEE
8+
MIIIIIJJEE
9+
MIIISIJEEE
10+
MMMISSJEEE

input/2024/Aoc2024Day12ex4.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
EEEEE
2+
EXXXX
3+
EEEEE
4+
EXXXX
5+
EEEEE

input/2024/Aoc2024Day12ex5.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
AAAAAA
2+
AAABBA
3+
AAABBA
4+
ABBAAA
5+
ABBAAA
6+
AAAAAA

puzzles.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
|[2021](#2021)|![+](https://img.shields.io/badge/stars%20⭐-50-green) |
1414
|[2022](#2022)|![+](https://img.shields.io/badge/stars%20⭐-50-green) |
1515
|[2023](#2023)|![+](https://img.shields.io/badge/stars%20⭐-50-green) |
16-
|[2024](#2024)|![+](https://img.shields.io/badge/stars%20⭐-20-yellow) |
17-
|__Total__ |![total](https://img.shields.io/badge/stars%20⭐-472-green) |
16+
|[2024](#2024)|![+](https://img.shields.io/badge/stars%20⭐-24-yellow) |
17+
|__Total__ |![total](https://img.shields.io/badge/stars%20⭐-474-green) |
1818

1919
## Puzzles
2020

@@ -321,7 +321,7 @@ Hint from `subreddit` was used for Day 12 part 2, Day 24 part 2 and Day 25.
321321
| + |2024| 9|Disk Fragmenter | |
322322
| + |2024| 10|Hoof It |BFS |
323323
| + |2024| 11|Plutonian Pebbles |recursion, memoization |
324-
| - |2024| 12| | |
324+
| + |2024| 12|Garden Groups | |
325325
| - |2024| 13| | |
326326
| - |2024| 14| | |
327327
| - |2024| 15| | |

results.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Results for all seasons
22

3-
* Output of local run using PHP `8.4.1` with `php src/aoc.php` on `2024.12.11`.
3+
* Output of local run using PHP `8.4.1` with `php src/aoc.php` on `2024.12.12`.
44
* Total running time:
5-
* with AocRunner (PHP + JIT): `133 sec`
6-
* with PhpUnit (XDebug + coverage): `39 sec` (less puzzles, 707 tests)
5+
* with AocRunner (PHP + JIT): `141 sec`
6+
* with PhpUnit (XDebug + coverage): `39 sec` (less puzzles, 714 tests)
77

88
```txt
99
Advent of Code - batch solution runner, (c) 2024 by TBali
@@ -912,7 +912,11 @@ Advent of Code - batch solution runner, (c) 2024 by TBali
912912
[ OK ] Puzzle examples (2) passed.
913913
[ OK ] 197157
914914
[ OK ] 234430066982597
915-
======= Total: 236 solutions (12 skipped) [time: 147.633 sec; max memory: 1610 MB]
915+
=== AoC 2024 Day 12 [time: 0.068 sec; memory: 72 Mbytes] Garden Groups
916+
[ OK ] Puzzle examples (5) passed.
917+
[ OK ] 1344578
918+
[ OK ] 814302
919+
======= Total: 237 solutions (12 skipped) [time: 140.839 sec; max memory: 1610 MB]
916920
917921
[ OK ] All tests passed.
918922
```

src/Aoc2024/Aoc2024Day12.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TBali\Aoc2024;
6+
7+
use TBali\Aoc\SolutionBase;
8+
9+
/**
10+
* AoC 2024 Day 12: Garden Groups.
11+
*
12+
* @see https://adventofcode.com/2024/day/12
13+
*/
14+
final class Aoc2024Day12 extends SolutionBase
15+
{
16+
public const YEAR = 2024;
17+
public const DAY = 12;
18+
public const TITLE = 'Garden Groups';
19+
public const SOLUTIONS = [1344578, 814302];
20+
public const EXAMPLE_SOLUTIONS = [[140, 80], [772, 436], [1930, 1206], [0, 236], [0, 368]];
21+
22+
/**
23+
* Solve both parts of the puzzle for a given input, without IO.
24+
*
25+
* @param array<int, string> $input The lines of the input, without LF
26+
*
27+
* @return array<int, string> The answers for Part 1 and Part 2 (as strings)
28+
*
29+
* @phpstan-return array{string, string}
30+
*/
31+
public function solve(array $input): array
32+
{
33+
// ---------- Parse input
34+
$max_y = count($input);
35+
$max_x = strlen($input[0]);
36+
if (array_any(array_map(strlen(...), $input), static fn (int $x): bool => $x != $max_x)) {
37+
throw new \Exception('grid must be rectangular');
38+
}
39+
// ---------- Part 1 + 2
40+
$region_grid = array_fill(0, $max_y, array_fill(0, $max_x, -1));
41+
$side_grid = array_fill(0, $max_y, array_fill(0, $max_x, [-1, -1, -1, -1]));
42+
$areas = [];
43+
$perimeters = [];
44+
$count_regions = 0;
45+
for ($start_y = 0; $start_y < $max_y; ++$start_y) {
46+
for ($start_x = 0; $start_x < $max_x; ++$start_x) {
47+
if ($region_grid[$start_y][$start_x] >= 0) {
48+
continue;
49+
}
50+
++$count_regions;
51+
$region_char = $input[$start_y][$start_x];
52+
$areas[] = 0;
53+
$perimeters[] = 0;
54+
$q = [[$start_x, $start_y]];
55+
$region_grid[$start_y][$start_x] = $count_regions - 1;
56+
$idx_read = 0;
57+
while ($idx_read < count($q)) {
58+
[$x, $y] = $q[$idx_read];
59+
++$idx_read;
60+
++$areas[$count_regions - 1];
61+
foreach ([[1, 0], [0, 1], [-1, 0], [0, -1]] as $dir => [$dx, $dy]) {
62+
$x1 = $x + $dx;
63+
$y1 = $y + $dy;
64+
if (
65+
$x1 < 0
66+
|| $x1 >= $max_x
67+
|| $y1 < 0
68+
|| $y1 >= $max_y
69+
|| $input[$y1][$x1] != $region_char
70+
) {
71+
++$perimeters[$count_regions - 1];
72+
$side_grid[$y][$x][$dir] = $count_regions - 1;
73+
continue;
74+
}
75+
if ($region_grid[$y1][$x1] >= 0) {
76+
continue;
77+
}
78+
$region_grid[$y1][$x1] = $count_regions - 1;
79+
$q[] = [$x1, $y1];
80+
}
81+
}
82+
}
83+
}
84+
$ans1 = 0;
85+
for ($i = 0; $i < $count_regions; ++$i) {
86+
$ans1 += $areas[$i] * $perimeters[$i];
87+
}
88+
$sides = array_fill(0, $count_regions, 0);
89+
for ($dir = 0; $dir < 4; ++$dir) {
90+
if ($dir % 2 == 0) {
91+
for ($x = 0; $x < $max_x; ++$x) {
92+
for ($y = 0; $y < $max_y; ++$y) {
93+
$idx_region = $side_grid[$y][$x][$dir];
94+
if ($idx_region < 0) {
95+
continue;
96+
}
97+
if ($y == 0 || $side_grid[$y - 1][$x][$dir] != $idx_region) {
98+
++$sides[$idx_region];
99+
}
100+
}
101+
}
102+
} else {
103+
for ($y = 0; $y < $max_y; ++$y) {
104+
for ($x = 0; $x < $max_x; ++$x) {
105+
$idx_region = $side_grid[$y][$x][$dir];
106+
if ($idx_region < 0) {
107+
continue;
108+
}
109+
if ($x == 0 || $side_grid[$y][$x - 1][$dir] != $idx_region) {
110+
++$sides[$idx_region];
111+
}
112+
}
113+
}
114+
}
115+
}
116+
$ans2 = 0;
117+
for ($i = 0; $i < $count_regions; ++$i) {
118+
$ans2 += $areas[$i] * $sides[$i];
119+
}
120+
return [strval($ans1), strval($ans2)];
121+
}
122+
}

0 commit comments

Comments
 (0)