Skip to content

Instantly share code, notes, and snippets.

@wconrad
Created December 8, 2015 12:39
Show Gist options
  • Save wconrad/0b2af86c45655bcc2a91 to your computer and use it in GitHub Desktop.
Save wconrad/0b2af86c45655bcc2a91 to your computer and use it in GitHub Desktop.
Advent of Code, day 8
r/bin/env ruby
# http://adventofcode.com/day/8
class Literal
def initialize(line)
@s = line.chomp
end
def literal_size
@s.size
end
def memory_size
unescape.size
end
def escaped_size
escape.size
end
private
def unescape
eval(@s)
end
def escape
@s.inspect
end
end
lines = File.readlines("input")
literals = lines.map { |line| Literal.new(line) }
total_literal_size = literals.map(&:literal_size).reduce(&:+)
total_memory_size = literals.map(&:memory_size).reduce(&:+)
total_escaped_size = literals.map(&:escaped_size).reduce(&:+)
# part1
puts total_literal_size - total_memory_size
# part2
puts total_escaped_size - total_literal_size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment