Created
December 17, 2015 06:56
-
-
Save desrtfx/b3d3ec678f564dc22a1e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Day17 { | |
private static final int TARGET = 150; | |
private int[] buckets = { 11, 30, 47, 31, 32, 36, 3, 1, 5, 3, 32, 36, 15, | |
11, 46, 26, 28, 1, 19, 3 }; | |
private int minBuckets; | |
public static void main(String[] args) { | |
Day17 day17 = new Day17(); | |
System.out.println("Part 1: " + day17.solvePart1()); | |
System.out.println("Part 2: " + day17.solvePart2()); | |
} | |
public int solvePart1() { | |
int count = 0; | |
minBuckets = Integer.MAX_VALUE; | |
for (int bitmap = 1; bitmap < 1 << buckets.length; bitmap++) { | |
if (checkValid(bitmap)) { | |
count++; | |
int bitCount = countBits(bitmap); | |
if (minBuckets > bitCount) { | |
minBuckets = bitCount; | |
} | |
} | |
} | |
return count; | |
} | |
public int solvePart2() { | |
int count = 0; | |
for (int bitmap = 1; bitmap < 1 << buckets.length; bitmap++) { | |
if (checkValid(bitmap) && (countBits(bitmap) == minBuckets)) { | |
count++; | |
} | |
} | |
return count; | |
} | |
private boolean checkValid(int bitmap) { | |
int sum = 0; | |
for (int i = 0; i < buckets.length; i++) { | |
if ((bitmap & (1 << i)) == (1 << i)) { | |
sum += buckets[i]; | |
if (sum > Day17.TARGET) { | |
return false; | |
} | |
} | |
} | |
if (sum == Day17.TARGET) { | |
return true; | |
} | |
return false; | |
} | |
private int countBits(int bitmap) { | |
int count = 0; | |
for (int i = 0; i < buckets.length; i++) { | |
if ((bitmap & (1 << i)) == (1 << i)) { | |
count++; | |
} | |
} | |
return count; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment