Skip to content

Instantly share code, notes, and snippets.

@Lakshanz
Created August 21, 2019 10:12
Show Gist options
  • Save Lakshanz/99ddf911ccf64fc0c391b90dbe6d8652 to your computer and use it in GitHub Desktop.
Save Lakshanz/99ddf911ccf64fc0c391b90dbe6d8652 to your computer and use it in GitHub Desktop.
Google Codejam Problem A. Even Digits Answer in PHP | https://code.google.com/codejam/contest/9234486/dashboard
<?php
function getFirstOddNumPosition($num)
{
$digits = str_split($num);
$i = 0;
foreach ($digits as $digit) {
if ($digit % 2 != 0) {
return $i;
}
$i++;
}
return -1;
}
function getPlusCount($num, $digits, $first_odd_position)
{
foreach ($digits as $index => $digit) {
if ($index == $first_odd_position) {
$digits[$index] = $digit + 1;
} elseif ($index > $first_odd_position) {
$digits[$index] = 0;
}
}
return intval(join("", $digits)) - $num;
}
function getMinusCount($num, $digits, $first_odd_position)
{
foreach ($digits as $index => $digit) {
if ($index == $first_odd_position) {
$digits[$index] = $digit - 1;
} elseif ($index > $first_odd_position) {
$digits[$index] = 8;
}
}
return $num - intval(join("", $digits));
}
function getCount($num)
{
$first_odd_position = getFirstOddNumPosition($num);
// No odds found
if ($first_odd_position === -1) {
return 0;
}
// Odd is at last position
if (($first_odd_position + 1) == strlen($num)) {
return 1;
}
$digits = str_split($num);
// If first odd digit is 9, lets skip to minus button
if ($digits[$first_odd_position] == 9) {
return getMinusCount($num, $digits, $first_odd_position);
}
$plus_count = getPlusCount($num, $digits, $first_odd_position);
$minus_count = getMinusCount($num, $digits, $first_odd_position);
if ($plus_count < $minus_count) {
return $plus_count;
}
return $minus_count;
}
$f = fopen('php://stdin', 'r');
$i = 0;
$test_cases = 0;
while ($line = fgets($f)) {
$line = ltrim($line);
if ($i == 0) {
$test_cases = intval($line);
$i++;
continue;
}
$count = getCount(intval($line));
print "Case #$i: $count";
if ($i != $cases) {
print "\n";
}
$i++;
}
fclose($f);
exit;

Supervin has a unique calculator. This calculator only has a display, a plus button, and a minus button. Currently, the integer N is displayed on the calculator display.

Pressing the plus button increases the current number displayed on the calculator display by 1. Similarly, pressing the minus button decreases the current number displayed on the calculator display by 1. The calculator does not display any leading zeros. For example, if 100 is displayed on the calculator display, pressing the minus button once will cause the calculator to display 99.

Supervin does not like odd digits, because he thinks they are "odd". Therefore, he wants to display an integer with only even digits in its decimal representation, using only the calculator buttons. Since the calculator is a bit old and the buttons are hard to press, he wants to use a minimal number of button presses.

Please help Supervin to determine the minimum number of button presses to make the calculator display an integer with no odd digits.

Input The first line of the input gives the number of test cases, T. T test cases follow. Each begins with one line containing an integer N: the integer initially displayed on Supervin's calculator.

Output For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the minimum number of button presses, as described above.

*Input *

4 42 11 1 2018

*Output * Case #1: 0 Case #2: 3 Case #3: 1 Case #4: 2

In Sample Case #1, the integer initially displayed on the calculator has no odd digits, so no button presses are needed.

In Sample Case #2, pressing the minus button three times will cause the calculator to display 8. There is no way to satisfy the requirements with fewer than three button presses.

In Sample Case #3, either pressing the minus button once (causing the calculator to display 0) or pressing the plus button once will cause the calculator to display an integer without an odd digit.

In Sample Case #4, pressing the plus button twice will cause the calculator to display 2020. There is no way to satisfy the requirements with fewer than two button presses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment