Skip to content

Instantly share code, notes, and snippets.

@dingo-d
Created December 3, 2017 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dingo-d/9432fc42ec1da83e029cdcc48d803d4c to your computer and use it in GitHub Desktop.
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
<?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