Repository navigation
Expand file tree
/
Copy pathDaySaven.java
More file actions
56 lines (50 loc) · 1.86 KB
/
Copy pathDaySaven.java
File metadata and controls
56 lines (50 loc) · 1.86 KB
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
56
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
public class DaySaven {
private static Integer solve(Long target, Long current, ArrayList<Long> n) {
long mult = current * n.get(0);
long add = current + n.get(0);
long concat = Long.parseLong(current.toString() + n.get(0).toString());
if(n.size() == 1) {
if (mult == target || concat == target || add == target) {
return 1;
} else {
return 0;
}
}
ArrayList<Long> nextList = new ArrayList<Long>(n.subList(1, n.size()));
if(mult > target && concat > target) {
return solve(target, add, nextList);
} else {
if(solve(target, mult, nextList) == 1) {
return 1;
} else if(solve(target, concat, nextList) == 1){
return 1;
} else {
return solve(target, add, nextList);
}
}
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
try {
File inputFile = new File("input.txt");
input = new Scanner(inputFile);
String line = "";
Long sum = 0L;
while (input.hasNextLine()) {
line = input.nextLine();
Long target = Long.parseLong(line.split(":")[0]);
ArrayList<Long> n = new ArrayList<Long>(Arrays.stream(line.split(": ")[1].split(" "))
.map(Long::parseLong)
.collect(Collectors.toList()));
sum += (target * solve(target, n.get(0), new ArrayList<Long>(n.subList(1, n.size()))));
}
input.close();
System.out.println(sum);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}