Beginner Gremlin Questions

Hello - I am trying to do an Advent of Code challenge as a graph problem to learn some Gremlin, and am running into some walls. I'm working on Part 2 of Day 4. The conceit is that there are scratchcards, and that for the number of matches you have on a given card you win copies of other cards (e.g. a card with ID 3 that has two matches would win copies of the next two cards with ID 4 and 5, recursively generating more copies of cards if those two cards also had matches), and you're trying to count the total number of cards won for a given input of cards. I've attached the graph that I'm working with (the sample input from the challenge, visible at the link above). I've parsed the input into Card vertices with a cardId property, and CardNumber and WinningNumber vertices each with a value property and a fromCard edge pointing to the card they are from.
2 Replies
frazer
frazer10mo ago
My solution would take three passes over all Cards. First, something like:
.match(
as("c").in('fromCard').hasLabel("CardNumber").as("n"),
as("c").in('fromCard').hasLabel("WinningNumber").as("w"),
where("n", eq("w")).by('value')
)
.select('n').groupCount('matchCount').by(out('fromCard'))
.match(
as("c").in('fromCard').hasLabel("CardNumber").as("n"),
as("c").in('fromCard').hasLabel("WinningNumber").as("w"),
where("n", eq("w")).by('value')
)
.select('n').groupCount('matchCount').by(out('fromCard'))
... which creates a hashmap of Card -> # Matches. This is so far all I have that works. Then, I'm imagining something like:
.match(
as("c").hasLabel("Card").properties("cardId").value().as("cId"),
// ... some way of retrieving the value for c from matchCount map and storing it as "matches"
as("cId).math("_ + matches").as("limit")
as("d").hasLabel("Card").properties("cardId").value()
.and(
is(gt("cId")),
is(lte("limit"))
)
)
.select('c', 'd').addE('winsCard').from('c').to('d')
.match(
as("c").hasLabel("Card").properties("cardId").value().as("cId"),
// ... some way of retrieving the value for c from matchCount map and storing it as "matches"
as("cId).math("_ + matches").as("limit")
as("d").hasLabel("Card").properties("cardId").value()
.and(
is(gt("cId")),
is(lte("limit"))
)
)
.select('c', 'd').addE('winsCard').from('c').to('d')
... which builds up the chain of which cards win other cards. Then one final iteration adding a count property to each Card, which is 1 + the sum of count for each Card with a winsCard edge to this Card (and so on recursively up the chain of winsCard edges). Bonus points if I can calculate a full running sum of count while I'm doing this (in a sack maybe?), if not one more iteration to get the sum of all count. The two problems I'm facing so far trying to accomplish the second step are: 1) How can I keep my hashmap but "reset" the traversal back to the set of all vertices? 2) How do I refer to the values from my hashmap (which is as I understand it stored in a variable called matchCount) in a Match step?
kelvinl2816
kelvinl281610mo ago
Just a note to say that I will try to take a look at this one ASAP. Somehow I did not notice it until now. FYI @triggan as someone else who enjoys the Advent of Code puzzles!
Want results from more Discord servers?
Add your server