예제 #1
0
                            $this->grid[$key] = 0;
                        }
                        break;
                    case 'toggle':
                        $this->grid[$key] += 2;
                        break;
                }
            }
        }
    }
    /**
     * Return the total brightness of all the ligts
     *
     * @return int
     */
    public function getTotalBrightness()
    {
        return array_sum($this->grid);
    }
}
$handle = fopen("input.txt", "r");
$grid = new lightGrid();
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        if (preg_match('/^(?P<action>turn (on|off)|toggle)\\s(?P<x1>[0-9]+),(?P<y1>[0-9]+)\\sthrough\\s(?P<x2>[0-9]+),(?P<y2>[0-9]+)/i', $line, $matches)) {
            $grid->run($matches['action'], (int) $matches['x1'], (int) $matches['y1'], (int) $matches['x2'], (int) $matches['y2']);
        }
    }
    fclose($handle);
}
echo 'Total brightness: ' . $grid->getTotalBrightness() . PHP_EOL;
예제 #2
0
                }
            }
        }
    }
    /**
     * Count the number of lights that are switched on
     * 
     * @return int
     */
    public function countLightsOn()
    {
        $ret = 0;
        foreach ($this->grid as $value) {
            if (true === $value) {
                $ret++;
            }
        }
        return $ret;
    }
}
$handle = fopen("input.txt", "r");
$grid = new lightGrid();
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        if (preg_match('/^(?P<action>turn (on|off)|toggle)\\s(?P<x1>[0-9]+),(?P<y1>[0-9]+)\\sthrough\\s(?P<x2>[0-9]+),(?P<y2>[0-9]+)/i', $line, $matches)) {
            $grid->run($matches['action'], (int) $matches['x1'], (int) $matches['y1'], (int) $matches['x2'], (int) $matches['y2']);
        }
    }
    fclose($handle);
}
echo 'Number of lights that are on: ' . $grid->countLightsOn() . PHP_EOL;