Last active
December 20, 2015 09:29
-
-
Save desrtfx/b753c8859151c1280ffa 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
public class Day20 { | |
private final static int TARGET = 29000000; | |
private final static int MAX = 1000000; | |
static int[] presents = new int[MAX]; | |
public static void main(String[] args) { | |
// Part 1 | |
solve(false); | |
int sought = findHouse(); | |
System.out.println("Part 1: House number: " + sought); | |
// Part 2 | |
solve(true); | |
sought = findHouse(); | |
System.out.println("Part 2: House number: " + sought); | |
} | |
public static void init() { | |
for (int i = 0; i < MAX; i++) { | |
presents[i] = 0; | |
} | |
} | |
public static int findHouse() { | |
for (int house = 1; house < MAX; house++) { | |
if (presents[house] >= TARGET) { | |
return house; | |
} | |
} | |
return -1; | |
} | |
public static void solve(boolean part2) { | |
init(); | |
int multiplier = (part2 ? 11 : 10); | |
for (int elf = 1; elf < MAX; elf++) { | |
for (int house = elf; (house < MAX && (!part2 || (house <= elf * 50))); house += elf) { | |
presents[house] += (elf * multiplier); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment