diff --git a/day-7/Main.java b/day-7/Main.java new file mode 100644 index 0000000..030b87a --- /dev/null +++ b/day-7/Main.java @@ -0,0 +1,70 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; +import java.util.stream.Collectors; + +public class Main { + + static class Test { + + public long result; + public List values; + + Test(long r, List v) { + this.result = r; + this.values = v; + } + + boolean testIt() { + int possibleCombinations = (int) Math.pow(2, this.values.size()); + + for (int i = 0; i < possibleCombinations; i++) { + long newResult = this.values.get(0); + String out = "" + newResult; + for (int j = 1; j < this.values.size(); j++) { + if ((i >> j) % 2 == 0) { + newResult += this.values.get(j); + } else { + newResult *= this.values.get(j); + } + } + if (newResult == this.result) { + return true; + } + } + return false; + } + } + + static ArrayList parseInput() { + ArrayList output = new ArrayList(); + try { + File input = new File("input.txt"); + Scanner reader = new Scanner(input); + while (reader.hasNextLine()) { + String data = reader.nextLine(); + String[] parts = data.split(":"); + long result = Long.valueOf(parts[0]); + List values = Arrays.asList(parts[1].trim().split(" ")) + .stream() + .map(s -> Integer.parseInt(s)) + .collect(Collectors.toList()); + output.add(new Test(result, values)); + } + reader.close(); + } catch (FileNotFoundException e) { + System.out.println("Ups..."); + } + return output; + } + + public static void main(String args[]) { + ArrayList tests = parseInput(); + tests.removeIf(t -> !t.testIt()); + long total = tests.stream().mapToLong(t -> t.result).sum(); + System.out.println(total); + } +} diff --git a/readme.md b/readme.md index b111d81..c0518d5 100644 --- a/readme.md +++ b/readme.md @@ -8,7 +8,8 @@ Día 2: Zig Día 3: Bash Día 4: LibreOffice Calc Día 5: Julia -Día 6: Javascript +Día 6: Javascript +Día 7: Java Lenguajes por usar: @@ -18,7 +19,7 @@ Lenguajes por usar: 1. ~~Julia~~ 1. C++ 1. PHP -1. Java +1. ~~Java~~ 1. Ruby 1. ~~Javascript~~ 1. ~~LibreOffice Calc~~