Skip to content

Instantly share code, notes, and snippets.

@zach2825
Created January 11, 2024 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zach2825/d893f6226e301e7de8d2f2cb98cbb2a2 to your computer and use it in GitHub Desktop.
Save zach2825/d893f6226e301e7de8d2f2cb98cbb2a2 to your computer and use it in GitHub Desktop.
Advent day 1 part 2
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
/**
* https://adventofcode.com/2023/day/1/input
* https://adventofcode.com/2023/day/1
*/
class AdventDay1 extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'advent:day1 {--t|test}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
private array $wordToDigitMap = [
'one' => 1,
'two' => 2,
'three' => 3,
'four' => 4,
'five' => 5,
'six' => 6,
'seven' => 7,
'eight' => 8,
'nine' => 9,
];
public $outputLines = [];
/**
* Execute the console command.
*/
public function handle(): int
{
if (!$this->option('test')) {
$file_path = storage_path('app/advent/day/1/input');
} else {
$file_path = storage_path('app/advent/day/1/input.test');
}
$contents = file_get_contents($file_path);
$lines = explode("\n", $contents);
$this->newLine();
$this->info("Total Calibration Value: " . $this->sumCalibrationValues($lines));
$this->newLine();
//$this->table(['found(s)', 'found(n)', 'line', 'newNumber'], $this->outputLines);
return self::SUCCESS;
}
public function wordToDigit($word)
{
return $this->wordToDigitMap[$word] ?? null;
}
public function extractCalibrationValue($line)
{
// Replace spelled-out numbers with digits
$words = array_keys($this->wordToDigitMap);
$replace = implode('|', $words);
$found_numbers = [];
$outputLine = [];
// find all the spelled out numbers and numbers in the order they are in and append to $found_numbers
preg_match_all("/$replace|\d/", strtolower($line), $matches);
$found = $matches[0];
$this->info("Found(strings): " . implode(', ', $found));
$outputLine[] = implode(', ', $found);
if (!count($found)) {
$this->error("No numbers found in line: $line");
return 0;
}
foreach ($found as $found_number) {
if (!is_numeric($found_number)) {
$found_numbers[] = $this->wordToDigit($found_number);
} else {
$found_numbers[] = (int)($found_number);
}
}
$this->info("Found(numbers): " . implode(', ', $found_numbers));
$outputLine[] = implode(', ', $found_numbers);
$this->info("Line: " . $line);
$outputLine[] = $line;
// Extract the first and last digit
$firstDigit = $found_numbers[0];
$lastDigit = end($found_numbers);
$this->info('New Number: ' . intval($firstDigit . $lastDigit));
$outputLine[] = intval($firstDigit . $lastDigit);
$this->newLine();
$this->outputLines[] = $outputLine;
// Combine them to form a two-digit number
return intval($firstDigit . $lastDigit);
}
public function sumCalibrationValues($lines)
{
$sum = 0;
foreach ($lines as $row_num => $line) {
if (!$line) {
continue;
}
$this->comment("Row: $row_num");
$sum += $this->extractCalibrationValue($line);
}
return $sum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment