[2021][ruby][8.2] minizinc

pull/2/head
Alpha Chen 3 years ago
parent 63778c56b7
commit 1aeb80d0eb

@ -0,0 +1,40 @@
include "globals.mzn";
enum Signals = { A, B, C, D, E, F, G };
set of Signals: Zero = { A, B, C, E, F, G };
set of Signals: One = { C, F };
set of Signals: Two = { A, C, D, E, G };
set of Signals: Three = { A, C, D, F, G };
set of Signals: Four = { B, C, D, F };
set of Signals: Five = { A, B, D, F, G };
set of Signals: Six = { A, B, D, E, F, G };
set of Signals: Seven = { A, C, F };
set of Signals: Eight = { A, B, C, D, E, F, G };
set of Signals: Nine = { A, B, C, D, F, G };
array[1..10] of set of Signals: AllSignals = [ Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine ];
array[int] of set of Signals: Patterns;
% Patterns = [
% { A, C, E, D, G, F, B },
% { C, D, F, B, E },
% { G, C, D, F, A },
% { F, B, C, A, D },
% { D, A, B },
% { C, E, F, A, B, D },
% { C, D, F, G, E, B },
% { E, A, F, B },
% { C, A, G, E, D, B },
% { A, B },
% ];
array [Signals] of var Signals: Map;
constraint alldifferent(Map);
constraint forall(pattern in Patterns)(
exists(digit in AllSignals)(
{ Map[x] | x in pattern } = digit
)
);
output [show(Map)];

@ -18,24 +18,33 @@ MAP = {
# 8 6 8 7 4 9 7 # 8 6 8 7 4 9 7
}.transform_keys(&:to_set) }.transform_keys(&:to_set)
def solve(signals, output) # # My original solution to part two
all = signals + output # def solve(signals, output)
one = all.find {|x| x.length == 2 } # all = signals + output
four = all.find {|x| x.length == 4 } # one = all.find {|x| x.length == 2 }
seven = all.find {|x| x.length == 3 } # four = all.find {|x| x.length == 4 }
eight = all.find {|x| x.length == 7 } # seven = all.find {|x| x.length == 3 }
# eight = all.find {|x| x.length == 7 }
# b = (?a..?g).find {|x| signals.count {|s| s.include?(x) } == 6 }
# e = (?a..?g).find {|x| signals.count {|s| s.include?(x) } == 4 }
# f = (?a..?g).find {|x| signals.count {|s| s.include?(x) } == 9 }
b = (?a..?g).find {|x| signals.count {|s| s.include?(x) } == 6 } # a = (seven.chars - one.chars)[0]
e = (?a..?g).find {|x| signals.count {|s| s.include?(x) } == 4 } # c = (one.chars - [f])[0]
f = (?a..?g).find {|x| signals.count {|s| s.include?(x) } == 9 } # d = (four.chars - [b, c, f])[0]
# g = (eight.chars - [a, b, c, d, e, f])[0]
a = (seven.chars - one.chars)[0] # map = { a: a, b: b, c: c, d: d, e: e, f: f, g: g }.invert
c = (one.chars - [f])[0]
d = (four.chars - [b, c, f])[0]
g = (eight.chars - [a, b, c, d, e, f])[0]
map = { a: a, b: b, c: c, d: d, e: e, f: f, g: g }.invert # output.map {|x| MAP.fetch(x.chars.map {|c| map.fetch(c).to_s }.to_set) }.join.to_i
# end
# Here it is with a solver, but much, much slower!
def solve(signals, output)
data = "Patterns = [ #{signals.map { "{ #{_1.upcase.chars.join(" ,")} }" }.join(", ")} ]"
solution = `minizinc --cmdline-data "#{data}" day_08.mzn`.scan(/[A-G]/)
map = (?a..?g).zip(solution.map(&:downcase)).to_h
output.map {|x| MAP.fetch(x.chars.map {|c| map.fetch(c).to_s }.to_set) }.join.to_i output.map {|x| MAP.fetch(x.chars.map {|c| map.fetch(c).to_s }.to_set) }.join.to_i
end end

Loading…
Cancel
Save