Created
December 3, 2017 09:12
-
-
Save dingo-d/9432fc42ec1da83e029cdcc48d803d4c to your computer and use it in GitHub Desktop.
Solution for: http://adventofcode.com/2017/day/2 in php part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$file = nl2br( file_get_contents( 'input.txt' ) ); | |
$arr = array_filter( preg_split( '/\\r\\n|\\r|\\n/', $file ) ); | |
$res = []; | |
function divison_check( $arr ) { | |
$cur_num; | |
$arr_len = count( $arr ); | |
for ( $i = 0; $i < $arr_len; $i++ ) { | |
$curr_num = $arr[ $i ]; | |
for ( $j = 0; $j < $arr_len; $j++ ) { | |
if ( $j !== $i ) { | |
if ( is_int( $curr_num / $arr[ $j ] ) ) { | |
return $curr_num / $arr[ $j ]; | |
} | |
} | |
} | |
} | |
} | |
foreach ( $arr as $a_key => $a_value ) { | |
$single_arr = preg_split( '/\t/', $a_value ); | |
$single_arr = array_map( 'intval', $single_arr ); | |
$res[] = divison_check( $single_arr ); | |
} | |
$sum = 0; | |
foreach ( $res as $r_key => $r_value ) { | |
$sum += $r_value; | |
} | |
print_r($sum); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment