diff --git a/2015/day-10.php b/2015/day-10.php new file mode 100644 index 0000000..245bd9d --- /dev/null +++ b/2015/day-10.php @@ -0,0 +1,45 @@ +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"); diff --git a/2015/src/LookAndSay.php b/2015/src/LookAndSay.php new file mode 100644 index 0000000..0bb7c64 --- /dev/null +++ b/2015/src/LookAndSay.php @@ -0,0 +1,54 @@ + $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; + } +} diff --git a/2015/src/NiceString.php b/2015/src/NiceString.php index 6bc3082..98a4d12 100644 --- a/2015/src/NiceString.php +++ b/2015/src/NiceString.php @@ -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. @@ -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; } diff --git a/2015/tests/LookAndSayTest.php b/2015/tests/LookAndSayTest.php new file mode 100644 index 0000000..4cd2d68 --- /dev/null +++ b/2015/tests/LookAndSayTest.php @@ -0,0 +1,34 @@ +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)); + } +} diff --git a/README.md b/README.md index 1891e69..40a64d3 100644 --- a/README.md +++ b/README.md @@ -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 | | | | | | | | | |