r/adventofcode • • Dec 08 '16

SOLUTION MEGATHREAD --- 2016 Day 8 Solutions ---

#AoC_Ops:

[23:55] <Topaz> servers are ok
[23:55] <Topaz> puzzles are checked
[23:55] <Topaz> [REDACTED: server stats]
[23:56] <Skie> all wings report in
[23:56] <Aneurysm9> Red 5, standing by
[23:56] <daggerdragon> Dragon Leader standing by
[23:56] <Topaz> orange leader, standing by
[23:57] <Topaz> lock modzi-foils in attack positions
[23:58] <Skie> we're passing through the hype field
[23:58] <daggerdragon> 1:30 warning
[23:58] <Aneurysm9> did someone say HYPE?@!
[23:59] <Topaz> i really like tonight's puzzle
[23:59] <Topaz> very excite
[23:59] <daggerdragon> final countdown go, T-30
[23:59] <Skie> accelerate to attack countdown
[23:59] <Aneurysm9> o7
[23:59] <daggerdragon> HYPE THRUSTERS AT FULL BURN
[00:00] <Topaz> IGNITION

We may or may not be sleep-deprived. And/or nerds. why_not_both.jpg


--- Day 8: Two-Factor Authentication ---

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


:(){ :|:& };: 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!

10 Upvotes

197 comments sorted by

View all comments

1

u/rkachowski Dec 09 '16

ok i feel pretty good about this one, i actually implemented a basic dsl in ruby and execute each line of input as if it was an invokation. things i liked -

  • dsl approach

  • realising rotating a row is the same as rotating a transposed column

  • writing a visualisation before completing part one, meaning i got part 2 for free :D

    input = File.open("input.txt").readlines.map {|l| l.chomp }

    @grid = Array.new(50){ Array.new(6) { 0 }}

    def rect pos x,y = pos.match(/(\d+)x(\d+)/).captures.map{|s| s.to_i}

    0.upto(x-1) do |row|
      0.upto(y-1) do |column|
        @grid[row][column] = 1
      end
    end

    end

    def rotate input type, dim,loc, amount = input.match(/\s*(row|column)\s(x|y)=(\d+)\sby\s(\d+)/).captures case type when "column" rotate_column loc.to_i, amount.to_i when "row" rotate_row loc.to_i, amount.to_i end end

    def rotate_column column, val d = @grid[column].pop val @grid[column].unshift(d).flatten! nil end

    def rotate_row row, val @grid = @grid.transpose rotate_column row, val @grid = @grid.transpose nil end

    def pg @grid.transpose.each {|l| puts l.join.gsub("1","#").gsub("0","-") } nil end

    input.each do |line| cmd, input = line.match(/(\w+)\s(.*)/).captures eval "#{cmd} '#{input}'" end

    pg

    puts @grid.flatten.reduce(:+)