|
1 | 1 | package com.sbaars.adventofcode.year15.days; |
2 | 2 |
|
3 | 3 | import com.sbaars.adventofcode.year15.Day2015; |
| 4 | +import java.util.*; |
| 5 | +import java.util.stream.IntStream; |
4 | 6 |
|
5 | 7 | public class Day20 extends Day2015 { |
6 | | - public Day20() { |
7 | | - super(20); |
8 | | - } |
9 | | - |
10 | | - public static void main(String[] args) { |
11 | | - new Day20().printParts(); |
12 | | - } |
13 | | - |
14 | | - @Override |
15 | | - public Object part1() { |
16 | | - return ""; |
17 | | - } |
18 | | - |
19 | | - @Override |
20 | | - public Object part2() { |
21 | | - return ""; |
22 | | - } |
| 8 | + |
| 9 | + private static final int TARGET_PRESENTS = 36000000; |
| 10 | + private static final int PRESENTS_PER_ELF = 10; |
| 11 | + |
| 12 | + public Day20() { |
| 13 | + super(20); |
| 14 | + } |
| 15 | + |
| 16 | + public static void main(String[] args) { |
| 17 | + Day20 day = new Day20(); |
| 18 | + day.printParts(); |
| 19 | + new com.sbaars.adventofcode.network.Submit().submit(day.part1(), 2015, 20, 1); |
| 20 | + } |
| 21 | + |
| 22 | + @Override |
| 23 | + public Object part1() { |
| 24 | + int houseNumber = 1; |
| 25 | + while (getPresentsForHouse(houseNumber) < TARGET_PRESENTS) { |
| 26 | + houseNumber++; |
| 27 | + } |
| 28 | + return houseNumber; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public Object part2() { |
| 33 | + return 0; // Implement in next part |
| 34 | + } |
| 35 | + |
| 36 | + private int getPresentsForHouse(int houseNumber) { |
| 37 | + return getDivisors(houseNumber).stream() |
| 38 | + .mapToInt(divisor -> divisor * PRESENTS_PER_ELF) |
| 39 | + .sum(); |
| 40 | + } |
| 41 | + |
| 42 | + private Set<Integer> getDivisors(int number) { |
| 43 | + Set<Integer> divisors = new HashSet<>(); |
| 44 | + int sqrt = (int) Math.sqrt(number); |
| 45 | + |
| 46 | + for (int i = 1; i <= sqrt; i++) { |
| 47 | + if (number % i == 0) { |
| 48 | + divisors.add(i); |
| 49 | + divisors.add(number / i); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return divisors; |
| 54 | + } |
23 | 55 | } |
0 commit comments