[php] AOC day 14 both
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- #!/usr/local/bin/php
- <?php
- class DockingData {
- private $data;
- public function __construct() {
- $this->data = file(__DIR__.'/input.txt');
- $this->data = array_map('trim', $this->data);
- }
- public function part1() : int
- {
- $memory = [];
- $mask = '';
- foreach ($this->data as $line) {
- $l_spl = explode(' = ',$line);
- if(str_starts_with($line, 'mask')) {
- $mask = $l_spl[1];
- } elseif(str_starts_with($line, 'mem')) {
- $matches = [];
- preg_match('/\[([\d]+)\]/', $l_spl[0], $matches);
- $to_mem = decbin(intval($l_spl[1]));
- // echo ($to_mem.PHP_EOL);
- $to_mem = $this->apply_mask($mask, $to_mem);
- $memory[intval($matches[1])] = bindec($to_mem);
- }
- }
- return array_sum($memory);
- }
- private function apply_mask(string $mask, string $value) : string
- {
- // echo("value: ".str_pad($value,36,'0',STR_PAD_LEFT)."\nmask: $mask\n");
- $mask_arr = str_split($mask);
- $value_arr = str_split(str_pad($value,36,'0',STR_PAD_LEFT));
- foreach ($mask_arr as $digit => $mask_bit) {
- switch ($mask_bit) {
- case 'X':
- break;
- default: // 1 and 0
- $value_arr[$digit] = $mask_bit;
- break;
- }
- }
- // echo ("result: ".implode($value_arr).PHP_EOL);
- return implode($value_arr);
- }
- public function part2() : int
- {
- $memory = [];
- $mask = '';
- foreach ($this->data as $line) {
- $l_spl = explode(' = ',$line);
- if(str_starts_with($line, 'mask')) {
- $mask = $l_spl[1];
- } elseif(str_starts_with($line, 'mem')) {
- $matches = [];
- preg_match('/\[([\d]+)\]/', $l_spl[0], $matches);
- $to_mem = intval($l_spl[1]);
- // echo ($to_mem.PHP_EOL);
- $addresses = $this->get_mask_possibilities($mask, decbin($matches[1]));
- foreach ($addresses as $address) {
- $memory[$address] = $to_mem;
- }
- }
- }
- return array_sum($memory);
- }
- private function get_mask_possibilities(string $mask, string $value) : array
- {
- // echo("address: ".str_pad($value,36,'0',STR_PAD_LEFT)."\nmask: $mask\n");
- $mask_arr = str_split($mask);
- $value_arr = str_split(str_pad($value,36,'0',STR_PAD_LEFT));
- $num_floating = 0;
- foreach ($mask_arr as $digit => $mask_bit) {
- switch ($mask_bit) {
- case '0':
- break;
- case '1':
- $value_arr[$digit] = $mask_bit;
- break;
- case 'X':
- $value_arr[$digit] = 'X';
- $num_floating += 1;
- break;
- }
- }
- $num_possibilities = bindec(str_pad('', $num_floating, '1'));
- $result = [];
- foreach (range(0,$num_possibilities) as $digits) {
- $digits_arr = str_split(str_pad(decbin($digits),$num_floating,'0',STR_PAD_LEFT));
- $value_arr_copy_str = implode($value_arr);
- foreach ($digits_arr as $digit) {
- $pos = strpos($value_arr_copy_str, 'X');
- if ($pos !== false) {
- $value_arr_copy_str = substr_replace($value_arr_copy_str, $digit, $pos, 1);
- } else {
- die('FUCK');
- }
- }
- // echo ("result: ".$value_arr_copy_str." (decimal ".bindec($value_arr_copy_str).")".PHP_EOL);
- $result[] = bindec($value_arr_copy_str);
- }
- // echo(PHP_EOL);
- return $result;
- }
- }
- $rr = new DockingData();
- $part1 = $rr->part1();
- echo("Part 1: $part1\n");
- $part2 = $rr->part2();
- echo("Part 2: $part2\n");
Editor
You can edit this paste and save as new: