-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
140 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
ini_set("memory_limit", "-1"); | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use adventofcode\Year2015\LookAndSay; | ||
|
||
/** | ||
* --- Day 10: Elves Look, Elves Say --- | ||
* | ||
* Today, the Elves are playing a game called look-and-say. They take turns making sequences by reading aloud the | ||
* previous sequence and using that reading as the next sequence. For example, 211 is read as "one two, two ones", | ||
* which becomes 1221 (1 2, 2 1s). | ||
* | ||
* Look-and-say sequences are generated iteratively, using the previous value as input for the next step. For each | ||
* step, take the previous value, and replace each run of digits (like 111) with the number of digits (3) followed | ||
* by the digit itself (1). | ||
* | ||
* For example: | ||
* | ||
* - 1 becomes 11 (1 copy of digit 1). | ||
* - 11 becomes 21 (2 copies of digit 1). | ||
* - 21 becomes 1211 (one 2 followed by one 1). | ||
* - 1211 becomes 111221 (one 1, one 2, and two 1s). | ||
* - 111221 becomes 312211 (three 1s, two 2s, and one 1). | ||
* | ||
* Starting with the digits in your puzzle input, apply this process 40 times. What is the length of the result? | ||
*/ | ||
|
||
$lookAndSay = new LookAndSay(); | ||
$result = $lookAndSay->play('3113322113', 40); | ||
print("The length of the result after 40 rounds is " . strlen($result) . ".\n"); | ||
|
||
/** | ||
* Neat, right? You might also enjoy hearing John Conway talking about this sequence (that's Conway of Conway's | ||
* Game of Life fame). | ||
* | ||
* Now, starting again with the digits in your puzzle input, apply this process 50 times. What is the length of | ||
* the new result? | ||
*/ | ||
|
||
$lookAndSay = new LookAndSay(); | ||
$result = $lookAndSay->play('3113322113', 50); | ||
print("The length of the result after 50 rounds is " . strlen($result) . ".\n"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace adventofcode\Year2015; | ||
|
||
class LookAndSay | ||
{ | ||
/** | ||
* Play a game of Look and Say. | ||
* | ||
* @param string $input | ||
* @param int $rounds Number of rounds to play | ||
* @return string | ||
*/ | ||
public function play(string $input, int $rounds): string | ||
{ | ||
$finalResult = $input; | ||
|
||
for ($i = 0; $i < $rounds; $i++) { | ||
$characters = str_split($finalResult); | ||
|
||
if (count($characters) === 1) { | ||
$finalResult = '1' . $characters[0]; | ||
continue; | ||
} | ||
|
||
$previousCharacter = $characters[0]; | ||
$characterCount = 0; | ||
$roundResult = ''; | ||
|
||
foreach ($characters as $index => $character) { | ||
if ($character === $previousCharacter) { | ||
$characterCount++; | ||
|
||
if ($index === count($characters) - 1) { | ||
$roundResult .= $characterCount . $previousCharacter; | ||
} | ||
continue; | ||
} | ||
|
||
$roundResult .= $characterCount . $previousCharacter; | ||
$previousCharacter = $character; | ||
$characterCount = 1; | ||
|
||
if ($index === count($characters) - 1) { | ||
$roundResult .= $characterCount . $previousCharacter; | ||
} | ||
} | ||
|
||
$finalResult = $roundResult; | ||
} | ||
|
||
return $finalResult; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace adventofcode\Year2015\Test; | ||
|
||
use adventofcode\Year2015\LookAndSay; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class LookAndSayTest extends TestCase | ||
{ | ||
protected $lookAndSay; | ||
|
||
protected function setup(): void | ||
{ | ||
$this->lookAndSay = new LookAndSay(); | ||
} | ||
|
||
public function testPlay() | ||
{ | ||
ini_set("memory_limit", "-1"); | ||
|
||
// Day 10 Part 1 example input | ||
$result = $this->lookAndSay->play('1', 5); | ||
$this->assertEquals('312211', $result); | ||
$this->assertEquals(6, strlen($result)); | ||
|
||
// Day 10 Part 1 actual input | ||
$result = $this->lookAndSay->play('3113322113', 40); | ||
$this->assertEquals(329356, strlen($result)); | ||
|
||
// Day 10 Part 2 actual input | ||
$result = $this->lookAndSay->play('3113322113', 50); | ||
$this->assertEquals(4666278, strlen($result)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters