Skip to content

Instantly share code, notes, and snippets.

@sarahzrf
Created December 8, 2021 07:48
Show Gist options
  • Save sarahzrf/8164e16d13ed34ebdbe07fdf4501b596 to your computer and use it in GitHub Desktop.
Save sarahzrf/8164e16d13ed34ebdbe07fdf4501b596 to your computer and use it in GitHub Desktop.
require 'set'
def group(obs_raw)
obs = Hash.new {Set.new("abcdefg".chars)}
obs_raw.each do |word|
obs[word.length] &= Set.new(word.chars)
end
obs
end
def decode(obs)
cf = obs[2]
acf = obs[3]
bcdf = obs[4]
adg = obs[5]
abfg = obs[6]
abcdefg = obs[7]
a = acf - cf
b = bcdf - cf - adg
d = bcdf - b - acf
e = abcdefg - acf - bcdf - abfg
f = abfg & cf
c = cf - f
g = adg - a - d
{a.first => 'a', b.first => 'b', c.first => 'c', d.first => 'd',
e.first => 'e', f.first => 'f', g.first => 'g'}
end
def char_set(s)
Set.new(s.chars)
end
DISPLAY = {
char_set('abcefg') => '0',
char_set('cf') => '1',
char_set('acdeg') => '2',
char_set('acdfg') => '3',
char_set('bcdf') => '4',
char_set('abdfg') => '5',
char_set('abdefg') => '6',
char_set('acf') => '7',
char_set('abcdefg') => '8',
char_set('abcdfg') => '9',
}
total = 0
$<.each do |l|
obs_raw, outs = l.chomp.split(' | ').map(&:split)
tbl = decode(group(obs_raw))
digits = outs.map do |out|
real_out = out.chars.map {|c| tbl[c]}
digit = DISPLAY[Set.new(real_out)]
end
total += digits.join.to_i
end
print total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment