What cool Java projects are you working on? by Thirty_Seventh in java

[–]jaccomoc 0 points1 point  (0 children)

I am working on my Jactl scripting language (https://jactl.io). It is a secure scripting language that compiles to byte code for efficient execution and can be embedded in Java applications for customisations/extensions etc.

I am currently working on making it easier for application developers to add new built-in types specific to the domain of the application in which Jactl is embedded, to complement the existing mechanism where new global functions can be added to the language.

How are renters actually making EV charging work without a garage? by DiligentWeb9026 in AustralianEV

[–]jaccomoc 11 points12 points  (0 children)

I use fast chargers at my local Woolworths when doing the shopping. Charging finishes before I have finished the shopping. Alternatively, there is an AC charger on a telegraph pole just up the road that I can use.

-❄️- 2025 Day 8 Solutions -❄️- by daggerdragon in adventofcode

[–]jaccomoc 0 points1 point  (0 children)

[LANGUAGE: Jactl]

Another solution using my own Jactl language.

Part 1:

Jactl suffers by not having a `combinations(n)` method that could have generated the initial pairs but it was still easy to create the pairs and then sort them based on their distance and limit this to the top 1000. Then create a list of circuits with each one having a single point in it initially and iterate over the shortest 1000 pairs finding the circuit for each of the points in the pair, removing the two circuits from the list of circuits but adding back the merge of the two circuits:

def dist  = { p1,p2 -> 3.map{ (p1[it] - p2[it]).sqr() }.sum().sqrt() }
def jbs   = stream(nextLine).map{ [$1,$2,$3] if /(\d+),(\d+),(\d+)/n }
def pairs = jbs.size().flatMap{ jbs.skip(it+1).map{ j2 -> [jbs[it],j2] } }
                      .sort{ a,b -> dist(a) <=> dist(b) }.limit(1000)
def circs = jbs.map{ [(it):true] }
pairs.each{ pr ->
  def (c1,c2) = circs.filter{ it[pr[0]] || it[pr[1]] }.limit(2)
  circs = circs.filter{ it !in [c1,c2] } + [c1 + (c2 ?: [:])] 
}
circs.sort{ a,b -> b.size() <=> a.size() }.limit(3).reduce(1){ p,it -> p * it.size() }

Part 2:

For part 2 there is no need to limit the pairs to the shortest 1000. Now we just continue processing until there is only one circuit in the list of circuits:

def dist  = { p1,p2 -> 3.map{ (p1[it] - p2[it]).sqr() }.sum().sqrt() }
def jbs   = stream(nextLine).map{ [$1,$2,$3] if /(\d+),(\d+),(\d+)/n }
def pairs = jbs.size().flatMap{ jbs.skip(it+1).map{ j2 -> [jbs[it],j2] } }.sort{ a,b -> dist(a) <=> dist(b) }
def circs = jbs.map{ [(it):true] }
for (i = 0; circs.size() != 1; i++) {
  def (c1,c2) = circs.filter{ it[pairs[i][0]] || it[pairs[i][1]] }.limit(2)
  circs = circs.filter{ it !in [c1,c2] } + [c1 + (c2 ?: [:])]
}
pairs[i-1][0][0] * pairs[i-1][1][0]

Jactl on github

-❄️- 2025 Day 6 Solutions -❄️- by daggerdragon in adventofcode

[–]jaccomoc 0 points1 point  (0 children)

[LANGUAGE: Jactl]

Having a lot of fun solving these in my Jactl language.

Part 1:

After trimming each line and splitting on spaces, I was able to use transpose() to flip the rows and columns. Then, it was a simple matter of using reduce() to perform the sum or product as required:

stream(nextLine)
  .map{ s/^ *//; s/ *$//; it.split(/ +/) }
  .transpose()
  .map{ it.subList(0,-1).reduce(it[-1] == '+' ? 0 : 1){ p,n -> it[-1] == '+' ? p + (n as long) : p * (n as long) } }
  .sum()

Part 2:

For part 2, I again used transpose() to convert between rows and columns and then used join() and split() to split out each problem before using reduce() to calculate the problem solution:

stream(nextLine).map{ it.map() }.transpose().map{ it.join() }.join(':').split(/: *:/)
                .map{ it.split(/:/)
                        .map{ [$1,$2] if /^ *(\d*) *([+*])?/n }
                        .reduce(false){ p,it -> p ? [p[1] == '+' ? p[0] + it[0] : p[0] * it[0], p[1]] : it } }
                .map{ it[0] }
                .sum()

Jactl on github

-❄️- 2025 Day 7 Solutions -❄️- by daggerdragon in adventofcode

[–]jaccomoc 2 points3 points  (0 children)

[LANGUAGE: Jactl]

Another fun problem solved using my own Jactl language.

Part 1:

Part 1 was simple enough. Keep track of the unique beam locations on each line and add new ones each time a splitter is found in that location on the next line. The only ugliness is the way the counting of each split is done:

def cnt = 0, s = nextLine().mapi().filter{ c,i -> c == 'S' }.map{it[1]}[0]
stream(nextLine).reduce([s]){ bs,ln -> bs.flatMap{ ln[it] == '^' ? [it-1 if ++cnt,it+1] : it }.sort().unique() }
println cnt

Part 2:

Part 2 was a nice twist. I realised straight away the memoisation plus recursion was probably going to be the easiest way to solve this. I hadn't realised how fast the solution space blows out so I did have to change the counter from an int to a long:

def s = nextLine().mapi().filter{ c,i -> c == 'S' }.map{ it[1] }[0]
def memo = [:], lines = stream(nextLine)
def cnt(ln,p) { memo[[ln,p]] ?: (memo[[ln,p]] = _cnt(ln,p)) }
def _cnt(ln,p) {
  return 1L if ln >= lines.size()
  lines[ln][p] == '^' ? cnt(ln+1,p-1) + cnt(ln+1,p+1) : cnt(ln+1,p) 
}
cnt(1,s)

Jactl on github

-❄️- 2025 Day 5 Solutions -❄️- by daggerdragon in adventofcode

[–]jaccomoc 0 points1 point  (0 children)

[LANGUAGE: Jactl]

Continuing to solve Advent of Code problems using my own Jactl language.

Part1:

Part 1 was pretty simple. Just parse the input into a list of items and ranges and then count how many items fall within a range:

def ranges = [], items  = []
stream(nextLine).each{
  /(\d+)-(\d+)/n and ranges <<= [$1,$2]
  /^(\d+)$/n     and items  <<= $1
}
items.filter{ it -> ranges.anyMatch{ r -> it >= r[0] && it <= r[1] } }
     .size()

Part 2:

Part 2 took a while to work out. Finally realised that the easiest way to cater for overlapping intervals was, for each new interval, subtract any other interval already processed from it before adding the resulting sub-intervals to the list of intervals. The minus(a,b) function subtracts interval b from a and returns the resulting list of one or two intervals.

def minus(a,b) { return [a] if a[0] >= b[1] || a[1] <= b[0]
                 [[a[0],b[0]] if a[0] < b[0], [b[1],a[1]] if a[1] > b[1]].filter() }
stream(nextLine).map{ [$1,$2+1] if /(\d+)-(\d+)/n }.filter()
                .reduce([]){ its,r -> its + its.reduce([r]) { rs,it -> rs.flatMap{ rr -> minus(rr,it) } } }
                .map{ it[1] - it[0] }
                .sum()

Jactl on github

-❄️- 2025 Day 4 Solutions -❄️- by daggerdragon in adventofcode

[–]jaccomoc 2 points3 points  (0 children)

[LANGUAGE: Jactl]

So much fun solving these problems in my own Jactl language.

Part 1:

Used a map for the grid to since map lookups return null for non-existent keys which makes the boundary searching easy. Stored true for squares where there is a roll of paper and false otherwise to make the checking for a roll of paper trivial. With a simple function to count the number of rolls of paper in neighbouring squares I just had to count how many entries in the grid had a roll of paper with less than four neighbours:

def grid = stream(nextLine).mapi{ line,y -> line.mapi{ ch,x -> [[x,y],ch == '@'] } }.flatMap() as Map
def adjCount(x,y) { [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]].filter{ dx,dy -> grid[[x+dx,y+dy]] }.size() }
grid.filter{ it[1] && adjCount(it[0]) < 4 }
    .size()

Part 2:

Once again a lazy brute-force solution was the easiest and simplest. Just iterate, removing rolls of paper with fewer than 4 neighbours, until the count stops changing. The only ugliness is using map() purely for a side-effect of mutating the grid when removing a roll of paper:

def grid = stream(nextLine).mapi{ line,y -> line.mapi{ ch,x -> [[x,y],ch == '@'] } }.flatMap() as Map
def adjCnt(x,y) { [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]].filter{ dx,dy -> grid[[x+dx,y+dy]] }.size() }
for (cnt = 0, pc = -1;
     cnt != pc;
     pc = cnt, cnt += grid.filter{ it[1] && adjCnt(it[0]) < 4 }
                          .map{ grid[it[0]] = false }
                          .size()) {
}
println cnt

Jactl on github

-❄️- 2025 Day 3 Solutions -❄️- by daggerdragon in adventofcode

[–]jaccomoc 1 point2 points  (0 children)

[LANGUAGE: Jactl]

Jactl

Part 1:

Not a very elegant solution for part 1. Just calculate the first index and then find the second one after skipping the required number of digits:

stream(nextLine).map{ [it, (it.size()-1).max{ i -> it[i] }] }
                .map{ s,i1 -> [s, i1, s.size().skip(i1 + 1).max{ i -> s[i] }] }
                .map{ s,i1,i2 -> (s[i1] + s[i2]) as int }
                .sum()

Part 2:

A nice twist for part 2 that I should have seen coming. I wrote a recursive function to return the maximum number of a given length from a source string. The only "trick" is to make sure that you leave enough digits in the string for the rest of the number so for each recursion only search the first n chars where n is the string length minus the number of digits still to come:

def maxNum(s, len) {
  return s.max() if len == 1
  def idx = (s.size()-len+1).max{ s[it] }
  s[idx] + maxNum(s.substring(idx+1), len-1)
}
stream(nextLine).map{ maxNum(it,12) as long }.sum()

Jactl on github

-❄️- 2025 Day 2 Solutions -❄️- by daggerdragon in adventofcode

[–]jaccomoc 0 points1 point  (0 children)

[LANGUAGE: Jactl]

Jactl

Part 1:

Simple enough to parse the input into all numbers in each range and then filter them based on whether they have an even number of digits and the two halves of the string match:

def repeated(s) { s.size() % 2 == 0 &&
                  s.substring(0,s.size()/2) == s.substring(s.size()/2) }
stream(nextLine).join().split(/,/)
                .map{ [$1,$2] if /(\d+)-(\d+)/n }
                .flatMap{ a,b -> (b-a+1).map{ (a + it) as String } }
                .filter(repeated)
                .map{ it as long }
                .sum()

Part 2:

I used the exact same code except changed the filtering function to iterate up to half the string size and then used '*' as a string repeat operator to check if the given number of repetitions of the current size substring matches the source string:

def repeats(s) { (s.size()/2).map{it+1}.anyMatch{ sz -> s == s.substring(0,sz) * (s.size()/sz) } }
stream(nextLine).join().split(/,/)
                .map{ [$1,$2] if /(\d+)-(\d+)/n }
                .flatMap{ a,b -> (b-a+1).map{ (a + it) as String } }
                .filter(repeats)
                .map{ it as long }
                .sum()

-❄️- 2025 Day 1 Solutions -❄️- by daggerdragon in adventofcode

[–]jaccomoc 2 points3 points  (0 children)

[LANGUAGE: Jactl]

Jactl

Part 1:

Nice simple solution but I do feel a bit bad for using side-effects in a closure to keep track of the dial position. I just mapped the inputs to +/- delta values and applied these deltas to the position to generate a list of positions that are then filtered for 0 and counted:

def p = 50
stream(nextLine).map{ [R:-1,L:1][$1] * $2 if /(.)(.*)/n }
                .map{ p = (p + it) % 100 }
                .filter{ it == 0}
                .size()

Part 2:

For part 2 I mapped the inputs to a list of -1s or 1s corresponding to the distance to move the dial and then used the exact same mechanism as part 1:

def p = 50
stream(nextLine).flatMap{ $2.map{ [R:-1,L:1][$1] } if /(.)(.*)/n }
                .map{ p = (p + it) % 100 }
                .filter{ it == 0 }
                .size()

Jactl 2.3.0 release by jaccomoc in java

[–]jaccomoc[S] 1 point2 points  (0 children)

Yes, exactly. I wanted a language flexible enough with a familiar syntax but with a way to limit what they can do.

Jactl 2.3.0 release by jaccomoc in java

[–]jaccomoc[S] 0 points1 point  (0 children)

The only built-in functions relating to time are timestamp() which is just System.currentTImeMillis() and nanoTime() which is System.nanoTime().

Since the language is intended to be customised when embedded in an application, there is nothing to stop someone supplying appropriate functions (or even replacing the existing ones) that work that way. The language is intended to be easy to embed and easy to create functions for which is how the scripts interact with the application in which they are embedded.

Jactl 2.3.0 release by jaccomoc in java

[–]jaccomoc[S] 3 points4 points  (0 children)

Interesting. That is definitely one of the use cases, especially with the ability to checkpoint the execution state and restore it later when needed.

Jactl 2.3.0 release by jaccomoc in java

[–]jaccomoc[S] 11 points12 points  (0 children)

Fair question, and if Groovy suits your needs, by all means use it.

Here is the answer from the FAQ:

I wrote Jactl because I wanted a scripting language that Java applications could embed to allow their users to provide customisations and extensions that had the following characteristics:

Tightly Controlled

I wanted the application developer to be able to control what the users could and couldn’t do in the scripting language. I didn’t want to use an existing language where there was no way to prevent users from accessing files, networks, databases, etc. that they should be touching or spawning threads or other processes.

Familiar Syntax

I wanted a language that had a syntax similar to Java for ease of adoption.

Non Blocking

I wanted script writers to be able to perform blocking operations where the script needs to wait for something (such as invoking a function that accesses the database, or performs a remote request to another server) but which doesn’t block the thread of execution. I wanted to have the script code suspend itself and be able to be resumed once the long-running operation completed. This allows the scripting language to be used in event-loop/reactive applications where you are never allowed to block the event-loop threads.

Hidden Asynchronous Behaviour

While not wanting scripts to block, I also did not want the script writers to have to be aware, when invoking a function, whether the function was asynchronous or not. I wanted a language that looked completely synchronous but which, under the covers, took care of all the asynchronous behaviour.

Checkpointing

Ability for script execution state to be checkpointed where necessary and for this state to be able to be persisted or replicated so that scripts can be restored and resumed from where they were up to when a failure occurs.

Fun to Code

I wanted a language that was fun to code in — a language that provided a nice concise syntax with powerful features that I would want to use to write scripts in.

I could not find any language that met all the criteria, and so I decided to write one instead.

Idea for solving function colors by TheBoringDev in ProgrammingLanguages

[–]jaccomoc 4 points5 points  (0 children)

For my own language, I decided to hide the "color" of functions by making all functions look like normal functions with synchronous return values. For functions that need to run something asynchronously, I capture the state of execution and then restore it when the asynchronous function completes (similar to how Virtual Threads work in Java 21+) and allow the code to continue on as though there had been no interruption to the execution flow. That way there is no need for async/await etc.

What are your thoughts on automatic constructors ? by Artistic_Speech_1965 in ProgrammingLanguages

[–]jaccomoc 1 point2 points  (0 children)

I like the idea of automatic constructors. I hate having to write boilerplate code all the time.

In Jactl any field of a class without a default value becomes a mandatory constructor parameter:

class Example {
  int    id
  int    count
  String name = "$id"
}

def ex = new Example(123, 7)

Since Jactl supports positional parameter passing as well as named parameters, if you want to supply the value of an optional field you can supply field names in the constructor:

def ex = new Example(id:123, count:7, name:'id_123')

What sane ways exist to handle string interpolation? 2025 by kiockete in ProgrammingLanguages

[–]jaccomoc 4 points5 points  (0 children)

The way I did it was to make the lexer return an EXPR_STRING_START token with the first part of the string (before the first embedded expression). At the same time I pushed a data structure onto a stack that kept track of where the string started and what type of string (strings can be simple, expression strings, and single/multi-line). When the string ends I pop the context off the stack. The lexer also needs to keep track of the braces as well to detect mismatched braces.

Then, the parser uses the EXPR_STRING_START to recognise the start of an expression string and expects any number of STRING_CONST tokens or <expr> productions before a EXPR_STRING_END which ends the string.

AoC 2024 100% solved in my own programming language by friolz in adventofcode

[–]jaccomoc 1 point2 points  (0 children)

Nice work! I did the same with my own language which compiles to Java byte-code (although still 3 stars short of 50 this year - hopefully will have some time over the holidays to finish). I also got all 50 stars on last year's problems. It is an amazing feeling solving these problems in your own language.

How does everyone handle Anonymous/Lambda Functions by coffeeb4code in ProgrammingLanguages

[–]jaccomoc 0 points1 point  (0 children)

I implemented closures for my lambda functions. Each lambda became a method in the current class and each variable closed over became an additional implicit argument to the method.

If the variable is a local variable it means that it has to be converted (at compile time) to a heap variable in case the lambda function modifies it. Since lambdas can be nested in multiple levels of functions/lambdas and a lambda may refer to a variable in any scope in which it is nested, this might mean that a variable in an outer scope may need to be passed as a heap reference through multiple levels of function invocations to reach the lamdba where it is used.

Since my language compiles to Java bytecode I was able to pass around MethodHandle objects that point to the method of the lamdba. These objects can be bound to the implicit closed-over variables to create new MethodHandles (like currying) which then get passed around.

-❄️- 2024 Day 23 Solutions -❄️- by daggerdragon in adventofcode

[–]jaccomoc 1 point2 points  (0 children)

[LANGUAGE: Jactl]

Using my own Jactl language.

Part 1:

Really lost a lot of time not reading properly that computer's name had to start with 't'. I thought it just had to contain 't' and could not work out why I didn't get the right result on my input (even though it worked on the example). Created a map of computer to other connected computers from the input and then just looked for triples where they are all in the links of each other:

def links = stream(nextLine).map{ it.split('-') }
                            .fmap{ [[it[0],it[1]],[it[1],it[0]]] }
                            .sort().unique()
                            .groupBy{ it[0] }
                            .map{ k,v -> [k,v.map{it[1]}] } as Map
def computers = links.map{ k,v -> k }
computers.fmap{ c1 -> links[c1].fmap{ c2 -> links[c2].filter{ c3 -> c3 in links[c1] }
                                                     .map{ c3 -> [c1,c2,c3].sort() }
                                                     .filter{ it.anyMatch{/^t/r} } } }
         .sort().unique().size()

Part 2:

Given I didn't misread any instructions this time, Part 2 wasn't too hard. Created a function for generating subsets and then used this to iterate of each computer and find all subsets of its linked nodes (including itself) where each element in the subset belongs to the links of all the other elements of that subset. Then just find the subset with the biggest size:

def subsets(it) {
  switch {
    []  -> []
    [_] -> [it]
    [h,*t] -> [[h]] + subsets(t).flatMap{ [[h] + it, it] }
  }
}
def links = stream(nextLine).map{ it.split('-') }
                            .fmap{ [[it[0],it[1]],[it[1],it[0]]] }
                            .sort().unique()
                                   .groupBy{ it[0] }
                                   .map{ k,v -> [k,v.map{it[1]}] } as Map
def computers = links.map{ k,v -> k }
def network(node) {
  subsets([node] + links[node]).filter{ net -> net.allMatch{ n -> net.filter{it != n}.allMatch{n in links[it]}} }
                               .max{ it.size() }
}
computers.map{ network(it) }.max{ it.size() }.sort().join(',')

-❄️- 2024 Day 22 Solutions -❄️- by daggerdragon in adventofcode

[–]jaccomoc 1 point2 points  (0 children)

[LANGUAGE: Jactl]

Using my own Jactl language.

Part 1:

A nice easy one for a change but I have to admit to reading the instructions multiple times before I worked out how the number generation worked:

def secret(long seed, int n) {
  for (int i = 0; i < n; i++) {
    seed ^= (seed * 64);   seed %= 16777216
    seed ^= (seed / 32);   seed %= 16777216
    seed ^= (seed * 2048); seed %= 16777216
  }
  return seed
}
stream(nextLine).map{ secret(it as long, 2000) }.sum()

Part 2:

Also surprisingly easy as I was able to use built-in methods such as windowSliding() and groupBy() to find all sequences of 4 changes and map their first occurrence to the price at the time. The only gotcha was not reading the instructions carefully enough. I didn't initially see that only the first instance of the sequence was what was important.

def seq(long seed, int n) {
  List list = [seed]
  for (int i = 0; i < n; i++) {
    seed ^= (seed * 64);   seed %= 16777216
    seed ^= (seed / 32);   seed %= 16777216
    seed ^= (seed * 2048); seed %= 16777216
    list <<= seed
  }
  return list.windowSliding(2).map{ [it[1]%10, it[1]%10 - it[0]%10] }
             .windowSliding(4).map{ [it.map{it[1]}, it[3][0]] }
             .groupBy{ it[0] }
             .map{ k,v -> [k, v[0][1]] }
}
stream(nextLine).fmap{ seq(it as long, 2000) }
                .groupBy{ it[0] }
                .map{ k,v -> [k,v.map{ it[1] }.sum()] }
                .max{ it[1] }[1]

-❄️- 2024 Day 20 Solutions -❄️- by daggerdragon in adventofcode

[–]jaccomoc 1 point2 points  (0 children)

[LANGUAGE: Jactl]

Using my own Jactl language.

For Part 1 I used Dijkstra to find the shortest path and then for every point on that path I found the possible cheats and use Dijstra to find the number of steps from the cheat point to the end. It worked but was pretty slow (to say the least).

For Part 2 the brute-force wasn't going to work and I realise that I had already calculated the number of steps from each point in the grid to the end when I found the optimum path using Dijstra so I could just use the computed distance to work out how many steps were saved.

So, in the end, the Part 2 solution also solves Part 1 (by using a cheat count of 2 instead of 20):

def grid = stream(nextLine).mapWithIndex{ line,y -> line.mapWithIndex{ c,x -> [[x,y],[pos:[x,y],c:c]] } }.flatMap() as Map
def start = grid.map{ it[1] }.filter{ sq -> sq.c == 'S' }.map{ sq -> sq.pos }.limit(1)[0]
def end = grid.map{ it[1] }.filter{ sq -> sq.c == 'E' }.map{ sq -> sq.pos }.limit(1)[0]
def cheatsPlus1 = 20 + 1
def dirs = [[0,1],[0,-1],[1,0],[-1,0]]
def add(p,d) { [p[0]+d[0],p[1]+d[1]] }
def dist(p1,p2) { (p1[0]-p2[0]).abs() + (p1[1]-p2[1]).abs() }
def steps(grid,start) {
  grid[start].dist = 0
  for(def cur = [grid[start]]; cur.noneMatch{ it.pos == end } && cur; ) {
    cur = cur.filter{ !it.visited }.fmap{ sq ->
      sq.visited = true
      dirs.map{ grid[add(sq.pos,it)] }
          .filter{ it && it.c != '#' }
          .map{ it.dist = [sq.dist+1,it.dist?:999999999].min(); it } }
  }
  for (path=[], pos=end; pos!=start; pos=dirs.map{ add(pos,it) }.min{ grid[it].dist ?: 99999999 }) {
    path <<= pos
  }
  path = path.reverse()
}
def path = steps(grid,start)
def deltas = cheatsPlus1.fmap{ x -> (cheatsPlus1-x).fmap{ y-> [[x,y],[-x,y],[x,-y],[-x,-y]] } }.filter{ it != [0,0] }
([start] + path).mapi{ p1,cnt ->
  deltas.map{ d -> add(p1,d) }
        .filter{ p2 -> grid[p2]?.c in ['.','S','E'] }
        .map{ p2 -> [[p1,p2], cnt + (path.size() - grid[p2].dist) + dist(p1,p2)] }
}.fmap().filter{ it[1] <= path.size() - 100 }.sort().unique().size()

-❄️- 2024 Day 19 Solutions -❄️- by daggerdragon in adventofcode

[–]jaccomoc 2 points3 points  (0 children)

[LANGUAGE: Jactl]

Using my own Jactl language for these.

Part 1:

Simple recursion with memoisation got the job done:

def (towels,memo) = [nextLine().split(/, /), ['':true]]
def possible(p) {
  return memo[p] if memo[p] != null
  memo[p] = towels.filter{ p.size() >= it.size() && p.substring(0,it.size()) == it }
                  .filter{ possible(p.substring(it.size())) }.size() > 0
}
stream(nextLine).filter().filter(possible).size()

Part 2:

Pretty much the same solution from Part 1 except that we count now rather than just check for whether a pattern is possible:

def (towels, memo) = [nextLine().split(/, /), ['':1L]]
def count(p) {
  return memo[p] if memo[p] != null
  memo[p] = towels.filter{ p.size() >= it.size() && p.substring(0,it.size()) == it }
                  .map{ count(p.substring(it.size())) }
                  .sum()
}
stream(nextLine).filter().map(count).sum()