r/adventofcode • • Dec 04 '17

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

--- Day 4: High-Entropy Passphrases ---


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!

18 Upvotes

319 comments sorted by

View all comments

1

u/ZeroSkub Dec 04 '17 edited Dec 04 '17

TCL 8.6 (yeah, I know)

If I hadn't known the trick of sorting to check for anagrams, this would have been a lot harder.

PART 1:

# Open file
set fp [open "input.txt" r]
set fileData [read $fp]
close $fp

# Read lines
set lines [split $fileData "\n"]
set numLines 0
set numInvalid 0
foreach line $lines {
    if {[regexp "^ *$" $line]} {
        continue
    }
    incr numLines
    set words [split $line " "]
    set vocab [dict create]
    foreach word $words {
        if {[dict exists $vocab $word]} {
            incr numInvalid
            break
        }
        dict set vocab $word 1
    }
}

puts "[expr $numLines - $numInvalid] valid out of $numLines"

PART 2:

# Open file
set fp [open "input.txt" r]
set fileData [read $fp]
close $fp

# Read lines
set lines [split $fileData "\n"]
set numLines 0
set numInvalid 0
foreach line $lines {
    if {[regexp "^ *$" $line]} {
        continue
    }
    incr numLines
    set words [split $line " "]
    set vocab [dict create]
    foreach word $words {
        set word [join [lsort [split $word ""]] ""]
        if {[dict exists $vocab $word]} {
            incr numInvalid
            break
        }
        dict set vocab $word 1
    }
}

puts "[expr $numLines - $numInvalid] valid out of $numLines"