Skip to content

Commit 11f865f

Browse files
authored
Day 10 part 1 & 2 (#23)
1 parent f16a65d commit 11f865f

File tree

5 files changed

+140
-5
lines changed

5 files changed

+140
-5
lines changed

2015/day-10.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
ini_set("memory_limit", "-1");
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
use adventofcode\Year2015\LookAndSay;
8+
9+
/**
10+
* --- Day 10: Elves Look, Elves Say ---
11+
*
12+
* Today, the Elves are playing a game called look-and-say. They take turns making sequences by reading aloud the
13+
* previous sequence and using that reading as the next sequence. For example, 211 is read as "one two, two ones",
14+
* which becomes 1221 (1 2, 2 1s).
15+
*
16+
* Look-and-say sequences are generated iteratively, using the previous value as input for the next step. For each
17+
* step, take the previous value, and replace each run of digits (like 111) with the number of digits (3) followed
18+
* by the digit itself (1).
19+
*
20+
* For example:
21+
*
22+
* - 1 becomes 11 (1 copy of digit 1).
23+
* - 11 becomes 21 (2 copies of digit 1).
24+
* - 21 becomes 1211 (one 2 followed by one 1).
25+
* - 1211 becomes 111221 (one 1, one 2, and two 1s).
26+
* - 111221 becomes 312211 (three 1s, two 2s, and one 1).
27+
*
28+
* Starting with the digits in your puzzle input, apply this process 40 times. What is the length of the result?
29+
*/
30+
31+
$lookAndSay = new LookAndSay();
32+
$result = $lookAndSay->play('3113322113', 40);
33+
print("The length of the result after 40 rounds is " . strlen($result) . ".\n");
34+
35+
/**
36+
* Neat, right? You might also enjoy hearing John Conway talking about this sequence (that's Conway of Conway's
37+
* Game of Life fame).
38+
*
39+
* Now, starting again with the digits in your puzzle input, apply this process 50 times. What is the length of
40+
* the new result?
41+
*/
42+
43+
$lookAndSay = new LookAndSay();
44+
$result = $lookAndSay->play('3113322113', 50);
45+
print("The length of the result after 50 rounds is " . strlen($result) . ".\n");

2015/src/LookAndSay.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace adventofcode\Year2015;
4+
5+
class LookAndSay
6+
{
7+
/**
8+
* Play a game of Look and Say.
9+
*
10+
* @param string $input
11+
* @param int $rounds Number of rounds to play
12+
* @return string
13+
*/
14+
public function play(string $input, int $rounds): string
15+
{
16+
$finalResult = $input;
17+
18+
for ($i = 0; $i < $rounds; $i++) {
19+
$characters = str_split($finalResult);
20+
21+
if (count($characters) === 1) {
22+
$finalResult = '1' . $characters[0];
23+
continue;
24+
}
25+
26+
$previousCharacter = $characters[0];
27+
$characterCount = 0;
28+
$roundResult = '';
29+
30+
foreach ($characters as $index => $character) {
31+
if ($character === $previousCharacter) {
32+
$characterCount++;
33+
34+
if ($index === count($characters) - 1) {
35+
$roundResult .= $characterCount . $previousCharacter;
36+
}
37+
continue;
38+
}
39+
40+
$roundResult .= $characterCount . $previousCharacter;
41+
$previousCharacter = $character;
42+
$characterCount = 1;
43+
44+
if ($index === count($characters) - 1) {
45+
$roundResult .= $characterCount . $previousCharacter;
46+
}
47+
}
48+
49+
$finalResult = $roundResult;
50+
}
51+
52+
return $finalResult;
53+
}
54+
}

2015/src/NiceString.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
class NiceString
66
{
7-
const MODE_V1 = 'v1';
8-
const MODE_V2 = 'v2';
7+
public const MODE_V1 = 'v1';
8+
public const MODE_V2 = 'v2';
99

1010
/**
1111
* Determines if the input contains a naughty string.
@@ -104,8 +104,10 @@ public function isNice(string $input, string $mode = self::MODE_V1): bool
104104
{
105105
switch ($mode) {
106106
case self::MODE_V2:
107-
if ($this->containsMultipleLetterPairs($input) &&
108-
$this->containsDuplicateLetterWithOneCharacterBetween($input)) {
107+
if (
108+
$this->containsMultipleLetterPairs($input) &&
109+
$this->containsDuplicateLetterWithOneCharacterBetween($input)
110+
) {
109111
return true;
110112
}
111113

2015/tests/LookAndSayTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace adventofcode\Year2015\Test;
4+
5+
use adventofcode\Year2015\LookAndSay;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class LookAndSayTest extends TestCase
9+
{
10+
protected $lookAndSay;
11+
12+
protected function setup(): void
13+
{
14+
$this->lookAndSay = new LookAndSay();
15+
}
16+
17+
public function testPlay()
18+
{
19+
ini_set("memory_limit", "-1");
20+
21+
// Day 10 Part 1 example input
22+
$result = $this->lookAndSay->play('1', 5);
23+
$this->assertEquals('312211', $result);
24+
$this->assertEquals(6, strlen($result));
25+
26+
// Day 10 Part 1 actual input
27+
$result = $this->lookAndSay->play('3113322113', 40);
28+
$this->assertEquals(329356, strlen($result));
29+
30+
// Day 10 Part 2 actual input
31+
$result = $this->lookAndSay->play('3113322113', 50);
32+
$this->assertEquals(4666278, strlen($result));
33+
}
34+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ of changes and trends in that community.
2020
| Day 07 | | | | | | | | | |
2121
| Day 08 | | | | | | | | | |
2222
| Day 09 | | | | | | | | | |
23-
| Day 10 | | | | | | | | | |
23+
| Day 10 | | | | | | | | | :star: :star: |
2424
| Day 11 | | | | | | | | | |
2525
| Day 12 | | | | | | | | | |
2626
| Day 13 | | | | | | | | | |

0 commit comments

Comments
 (0)