Last active
November 21, 2015 14:34
-
-
Save mkulak/545e68b74abf3f802ae3 to your computer and use it in GitHub Desktop.
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
fn lucky_tickets_count() -> i32 { | |
fn digits(i:&i32) -> [i32; 6] { | |
[i % 10, | |
(i / 10) % 10, | |
(i / 100) % 10, | |
(i / 1000) % 10, | |
(i / 10000) % 10, | |
(i / 100000) % 10] | |
} | |
fn sum(a:&[i32]) -> i32 { | |
return a.iter().fold(0, |acc, len| acc + len) | |
} | |
fn is_lucky(i:&i32) -> bool { | |
let d = digits(i); | |
return sum(&d[0..3]) == sum(&d[3..6]) | |
} | |
return (0..1000000).filter(&is_lucky).count() as i32 | |
} |
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
fun luckyTicketsCount():Int { | |
fun digits(i:Int) = arrayOf( | |
i % 10, | |
(i / 10) % 10, | |
(i / 100) % 10, | |
(i / 1000) % 10, | |
(i / 10000) % 10, | |
(i / 100000) % 10 | |
) | |
fun isLucky(i:Int):Boolean { | |
return digits(i).take(3).sum() == digits(i).drop(3).sum() | |
} | |
return (0..999999).count(::isLucky) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment