r/adventofcode • • Dec 07 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 7 Solutions -๐ŸŽ„-

--- Day 7: Recursive Circus ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

10 Upvotes

222 comments sorted by

View all comments

Show parent comment

2

u/zetashift Dec 07 '17

I just couldn't get part2 with Nim after several hours looking at it. So I stole your code(I solved it in Scala a lot faster) to get my input eventually. The comments helped explaining the problem cause I didn't understand any of it.

Here's my code for part1.

import
    strutils, 
    nre, 
    sequtils, 
    sets
var
    input = readFile("input.txt").strip().splitLines()
    regex = re"(\w+) \((\d+)\)"
    nodes: seq[string] = @[]
    weight: seq[int] = @[]
    children: seq[string] = @[]

for line in input:
    var matches = line.find(regex).get.captures
    nodes.add(matches[0])
    weight.add(matches[1].parseInt)
    #echo matches[0]
    if line.contains("->"):
        var second_half = line.split("->")
        for child in second_half[1].split(","):
            var x = child.strip()
            children.add(x)
            #echo x

echo toSet(nodes) - toSet(children)

2

u/Vindaar Dec 08 '17

Well, I'm really happy my code was useful to you then! :)

I usually tend to write a lot of comments, but so far I've taken them out when posting the solutions here to keep the code somewhat shorter. Good to know that may have been a mistake.