Skip to content

Instantly share code, notes, and snippets.

@packrat386
Last active June 4, 2017 02:20
Show Gist options
  • Save packrat386/39c58cee63fe58d84e86e6de21a0a4a9 to your computer and use it in GitHub Desktop.
Save packrat386/39c58cee63fe58d84e86e6de21a0a4a9 to your computer and use it in GitHub Desktop.
Doing day 7 in a single pass
STATE_MAP = {
outside_intial: 'Outside a bracket. Nothing Interesting',
outside_a: 'Outside a bracket. We found a character <a>',
outside_ab: 'Outside a bracket. After finding <a>, we found a <b> which was not the same as <a>',
outside_abb: 'Outside a bracket. After finding a sequence <a><b>, we found <b>',
solved_initial: 'Outside a bracket. We found a sequence <a><b><b><a>',
inside_intial: 'Inside a bracket. Nothing Interesting',
inside_a: 'Inside a bracket. We found a character <a>',
inside_ab: 'Inside a bracket. After finding <a>, we found a <b> which was not the same as <a>',
inside_abb: 'Inside a bracket. After finding a sequence <a><b>, we found <b>',
inside_abba: 'Inside a bracket. We found a sequence <a><b><b><a>',
solved_inside_intial: 'Found an earlier ABBA outside. Inside a bracket. Nothing Interesting',
solved_inside_a: 'Found an earlier ABBA outside. Inside a bracket. We found a character <a>',
solved_inside_ab: 'Found an earlier ABBA outside. Inside a bracket. After finding <a>, we found a <b> which was not the same as <a>',
solved_inside_abb: 'Found an earlier ABBA outside. Inside a bracket. After finding a sequence <a><b>, we found <b>',
solved_inside_abba: 'Found an earlier ABBA outside. Inside a bracket. We found a sequence <a><b><b><a>',
}
def supports?(str)
# The only working memory we need
$char_a = nil
$char_b = nil
$state = :outside_initial
# For output purposes
$currstr = ''
str.each_char do |char|
update_state(char)
$currstr << char
puts "#{$currstr} #{$state} #{$char_a.inspect} #{$char_b.inspect} #{STATE_MAP[$state]}"
return false if $state == :solved_inside_abba
return false if $state == :inside_abba
end
return $state.to_s.include?('solved')
end
def update_state(char)
case $state
when :outside_initial
case char
when '['
$state = :inside_initial
$char_a = nil
$char_b = nil
when ']'
raise 'Unbalanced brackets'
else
$state = :outside_a
$char_a = char
$char_b = nil
end
when :outside_a
case char
when '['
$state = :inside_initial
$char_a = nil
$char_b = nil
when ']'
raise 'Unbalanced brackets'
when $char_a
$state = :outside_a
$char_a = char
$char_b = nil
else
$state = :outside_ab
$char_a = $char_a
$char_b = char
end
when :outside_ab
case char
when '['
$state = :inside_initial
$char_a = nil
$char_b = nil
when ']'
raise 'Unbalanced brackets'
when $char_b
$state = :outside_abb
$char_a = $char_a
$char_b = $char_b
else
$state = :outside_ab
$char_a = $char_b
$char_b = char
end
when :outside_abb
case char
when '['
$state = :inside_initial
$char_a = nil
$char_b = nil
when ']'
raise 'Unbalanced brackets'
when $char_a
$state = :solved_initial
$char_a = nil
$char_b = nil
when $char_b
$state = :outside_a
$char_a = $char_b
$char_b = nil
else
$state = :outside_ab
$char_a = $char_b
$char_b = char
end
when :solved_initial
case char
when '['
$state = :solved_inside_initial
$char_a = nil
$char_b = nil
else
$state = :solved_initial
$char_a = nil
$char_b = nil
end
when :inside_initial
case char
when ']'
$state = :outside_initial
$char_a = nil
$char_b = nil
when '['
raise 'Unbalanced brackets'
else
$state = :inside_a
$char_a = char
$char_b = nil
end
when :inside_a
case char
when ']'
$state = :outside_initial
$char_a = nil
$char_b = nil
when '['
raise 'Unbalanced brackets'
when $char_a
$state = :inside_a
$char_a = char
$char_b = nil
else
$state = :inside_ab
$char_a = $char_a
$char_b = char
end
when :inside_ab
case char
when ']'
$state = :outside_initial
$char_a = nil
$char_b = nil
when '['
raise 'Unbalanced brackets'
when $char_b
$state = :inside_abb
$char_a = $char_a
$char_b = $char_b
else
$state = :inside_ab
$char_a = $char_b
$char_b = char
end
when :inside_abb
case char
when ']'
$state = :outside_initial
$char_a = nil
$char_b = nil
when '['
raise 'Unbalanced brackets'
when $char_a
$state = :inside_abba
$char_a = $char_a
$char_b = $char_b
when $char_b
$state = :inside_a
$char_a = $char_b
$char_b = nil
else
$state = :inside_ab
$char_a = $char_b
$char_b = char
end
when :solved_inside_initial
case char
when ']'
$state = :solved_initial
$char_a = nil
$char_b = nil
when '['
raise 'Unbalanced brackets'
else
$state = :solved_inside_a
$char_a = char
$char_b = nil
end
when :solved_inside_a
case char
when ']'
$state = :solved_initial
$char_a = nil
$char_b = nil
when '['
raise 'Unbalanced brackets'
when $char_a
$state = :solved_inside_a
$char_a = char
$char_b = nil
else
$state = :solved_inside_ab
$char_a = $char_a
$char_b = char
end
when :solved_inside_ab
case char
when ']'
$state = :solved_initial
$char_a = nil
$char_b = nil
when '['
raise 'Unbalanced brackets'
when $char_b
$state = :solved_inside_abb
$char_a = $char_a
$char_b = $char_b
else
$state = :solved_inside_ab
$char_a = $char_b
$char_b = char
end
when :solved_inside_abb
case char
when ']'
$state = :solved_initial
$char_a = nil
$char_b = nil
when '['
raise 'Unbalanced brackets'
when $char_a
$state = :solved_inside_abba
$char_a = $char_a
$char_b = $char_b
when $char_b
$state = :solved_inside_a
$char_a = $char_b
$char_b = nil
else
$state = :solved_inside_ab
$char_a = $char_b
$char_b = char
end
else
raise "Unsupported state #{$state}"
end
end
TESTS = {
'abcd' => false,
'abba' => true,
'ddabba' => true,
'abbccb' => true,
'ab[ba]'=> false,
'[abba]'=> false,
'[abcd]abba' => true,
'abccb[abba]' => false,
'[abba]cbbc' => false
}
TESTS.each do |test, expected|
puts "\n\n- #{test} - #{expected} -"
puts supports?(test)
end
ABBA = /(?<a>\w)(?!\k<a>)(?<b>\w)(\k<b>)(\k<a>)/
NOT_ABBA = /(?:(?<a>\w)(?!(?<b>\w)\k<b>\k<a>))*/
BENIGN_EXTRA = /(\w*|\[#{NOT_ABBA}\])/
SUPPORTS_SSL = /^#{BENIGN_EXTRA}*#{ABBA}#{BENIGN_EXTRA}*$/
def supports?(str)
matchdata = SUPPORTS_SSL.match(str)
puts matchdata.inspect
!!matchdata
end
TESTS = {
'abcd' => false,
'abba' => true,
'ddabba' => true,
'abbccb' => true,
'ab[ba]'=> false,
'[abba]'=> false,
'[abcd]abba' => true,
'abccb[abba]' => false,
'[abba]cbbc' => false
}
TESTS.each do |test, expected|
puts "\n\n- #{test} - #{expected} -"
puts supports?(test)
end
SUPPORTS_SSL.match('xdsqxnovprgovwzkus[fmadbfsbqwzzrzrgdg]aeqornszgvbizdm')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment