-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.groovy
46 lines (33 loc) · 1.15 KB
/
day1.groovy
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
#!/usr/bin/env groovy
import org.paukov.combinatorics3.Generator
@Grab('com.github.dpaukov:combinatoricslib3:3.3.0')
def sumOfElementsEquals(list, value) {
list.sum { it } == value
}
def readIntsFromFile(file) {
(file as File).readLines().collect { it.toInteger() }
}
def findCombinationSummingToValue(list, combolength, value) {
Generator.combination(list)
.multi(combolength)
.find {sumOfElementsEquals(it, value)}
}
def findProduct(list) {
list.inject(1) { result, i -> result * i }
}
def part1(file) {
findProduct(findCombinationSummingToValue(readIntsFromFile(file), 2, 2020))
}
def part2(file) {
findProduct(findCombinationSummingToValue(readIntsFromFile(file), 3, 2020))
}
assert sumOfElementsEquals([2019, 1], 2020)
def testCombination = findCombinationSummingToValue(readIntsFromFile('testinput.txt'), 2, 2020)
assert testCombination.size() == 2
assert testCombination.contains(1721)
assert testCombination.contains(299)
assert findProduct(testCombination) == 514579
assert part1('testinput.txt') == 514579
assert part2('testinput.txt') == 241861950
println part1('input.txt')
println part2('input.txt')