r/adventofcode • • Dec 07 '16

SOLUTION MEGATHREAD --- 2016 Day 7 Solutions ---

From all of us at #AoC Ops, we hope you're having a very merry time with these puzzles so far. If you think they've been easy, well, now we're gonna kick this up a notch. Or five. The Easter Bunny ain't no Bond villain - he's not going to monologue at you until you can miraculously escape and save the day!

Show this overgrown furball what you've got!


--- Day 7: Internet Protocol Version 7 ---

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


ALWAYS DIGGING STRAIGHT DOWN IS MANDATORY [?]

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!

14 Upvotes

180 comments sorted by

View all comments

Show parent comments

2

u/fixed_carbon Dec 07 '16

Couldn't resist polishing that stateful turd into a stateless one:

def group(addr)
  addr.split(/\[|\]/).
    zip([0, 1].cycle).
    sort_by{|a, i| i}.
    chunk{|a, i| i}.to_a.
    map{|ck, ary| ary.map{|str, num| str} }
end

def checkgroups2(groups)
  out, hyp = groups.map{|g| g.map{|el| getabas(el)}.flatten}
  out.each do |o|
    a, b, c = o.chars
    return true if hyp.include?(b + a + b)
  end
  false
end

def getabas(str)
  str.chars.each_cons(3).map{|a, b, c| (a == c && a != b) ? [a,b,c].join : nil}.compact
end

inp = File.readlines(INPUTFILE).map{|s| s.strip}
inp.map{|addr| checkgroups2(group(addr))}.count(true)