Skip to content

Day 10 part 1 & 2 #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions 2015/day-10.php
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");
54 changes: 54 additions & 0 deletions 2015/src/LookAndSay.php
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;
}
}
10 changes: 6 additions & 4 deletions 2015/src/NiceString.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

class NiceString
{
const MODE_V1 = 'v1';
const MODE_V2 = 'v2';
public const MODE_V1 = 'v1';
public const MODE_V2 = 'v2';

/**
* Determines if the input contains a naughty string.
Expand Down Expand Up @@ -104,8 +104,10 @@ public function isNice(string $input, string $mode = self::MODE_V1): bool
{
switch ($mode) {
case self::MODE_V2:
if ($this->containsMultipleLetterPairs($input) &&
$this->containsDuplicateLetterWithOneCharacterBetween($input)) {
if (
$this->containsMultipleLetterPairs($input) &&
$this->containsDuplicateLetterWithOneCharacterBetween($input)
) {
return true;
}

Expand Down
34 changes: 34 additions & 0 deletions 2015/tests/LookAndSayTest.php
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));
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ of changes and trends in that community.
| Day 07 | | | | | | | | | |
| Day 08 | | | | | | | | | |
| Day 09 | | | | | | | | | |
| Day 10 | | | | | | | | | |
| Day 10 | | | | | | | | | :star: :star: |
| Day 11 | | | | | | | | | |
| Day 12 | | | | | | | | | |
| Day 13 | | | | | | | | | |
Expand Down