Skip to content

Instantly share code, notes, and snippets.

@llupRisinglll
Created October 15, 2022 05:20
Show Gist options
  • Save llupRisinglll/dd2f3b6785f91a9bebc19f3ccb33d1cb to your computer and use it in GitHub Desktop.
Save llupRisinglll/dd2f3b6785f91a9bebc19f3ccb33d1cb to your computer and use it in GitHub Desktop.
HackerRank: Day of Programmer; PHP algorithm
<?php
function dayOfProgrammer($year) {
$targetDays = 256;
$targetCalendar = CAL_GREGORIAN;
if ($year < 1919){
$targetCalendar = CAL_JULIAN;
}
// In 1918, February 14th was the 32nd day of the year
$totalDays = 0;
$startMonth = ($year == 1918) ? 2 : 1;
for ($i = $startMonth; $i < 9; $i++){
$d = cal_days_in_month($targetCalendar, $i, $year);
if ($i == 2 && $year == 1918){
$remainingDaysAfterFeb14 = $d - 14;
$totalDays = (32 + $remainingDaysAfterFeb14);
continue;
}
$totalDays += $d;
}
$remainingDays = $targetDays - $totalDays;
return "$remainingDays.09.$year";
}
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$year = intval(trim(fgets(STDIN)));
$result = dayOfProgrammer($year);
fwrite($fptr, $result . "\n");
fclose($fptr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment