DataWeave scripts used in the adventofcode.com site for 2015.
Script
%dw 2.0
---
payload
replace "(" with "1,"
replace ")" with "-1,"
splitBy ","
map $ as Number
then sum($)
Script
%dw 2.0
fun getResult(data, index=0, sum=0) =
if (!isEmpty(data)) do {
var newSum = data[0] as Number + sum
var currentIndex = index+1
---
if (newSum == -1)
currentIndex
else
getResult(data[1 to -1], currentIndex, newSum)
}
else index
---
payload
replace "(" with "1,"
replace ")" with "-1,"
splitBy ","
then getResult($)
Script
%dw 2.0
fun getSingleResult(data) = do {
var d = data splitBy "x" map $ as Number orderBy $
var slack = d[0] * d[1]
---
(2*d[0]*d[1]) + (2*d[1]*d[2]) + (2*d[2]*d[0]) + slack
}
---
sum(payload splitBy "\n" map getSingleResult($))
Script
%dw 2.0
fun getSingleResult(data) = do {
var d = data splitBy "x" map $ as Number orderBy $
var ribbon = d[0] + d[0] + d[1] + d[1]
---
d[0]*d[1]*d[2] + ribbon
}
---
sum(payload splitBy "\n" map getSingleResult($))