aboutsummaryrefslogtreecommitdiff
path: root/challenge-115/paulo-custodio/d/ch_2.d
blob: db9dc060a05668fb3dad76d5aac65c6361560719 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
Challenge 115

TASK #2 - Largest Multiple
Submitted by: Mohammad S Anwar
You are given a list of positive integers (0-9), single digit.

Write a script to find the largest multiple of 2 that can be formed from the
list.

Examples
Input: @N = (1, 0, 2, 6)
Output: 6210

Input: @N = (1, 4, 2, 8)
Output: 8412

Input: @N = (4, 1, 7, 6)
Output: 7614
*/

import std.stdio;
import std.conv;
import std.algorithm.mutation : remove;
import std.algorithm.sorting : sort;

int largest_even(int[] nums) {
    // smallest even number is last digit
    int last_digit = 10;
    foreach (int num; nums) {
        if ((num % 2) == 0 && num < last_digit)
            last_digit = num;
    }
    if (last_digit == 10) return 0;

    // remove last_digit from list
    nums = remove!(a => a == last_digit)(nums);

    // sort remaining digits in descending order
    nums.sort!("a > b");

    int result = 0;
    foreach (int num; nums)
        result = result * 10 + num;
    result = result * 10 + last_digit;

    return result;
}

void main(string[] args) {
    int[] nums;
    foreach (string arg; args[1..$])
        nums ~= to!int(arg);
    writeln(largest_even(nums));
}