From 1aeb80d0eba8e122c0e2991161370c0b039a2bfe Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Wed, 8 Dec 2021 19:29:44 -0800 Subject: [PATCH] [2021][ruby][8.2] minizinc --- 2021/ruby/day_08.mzn | 40 ++++++++++++++++++++++++++++++++++++++++ 2021/ruby/day_08.rb | 37 +++++++++++++++++++++++-------------- 2 files changed, 63 insertions(+), 14 deletions(-) create mode 100644 2021/ruby/day_08.mzn diff --git a/2021/ruby/day_08.mzn b/2021/ruby/day_08.mzn new file mode 100644 index 0000000..e9ce265 --- /dev/null +++ b/2021/ruby/day_08.mzn @@ -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)]; \ No newline at end of file diff --git a/2021/ruby/day_08.rb b/2021/ruby/day_08.rb index c173e03..9c47cd0 100644 --- a/2021/ruby/day_08.rb +++ b/2021/ruby/day_08.rb @@ -18,24 +18,33 @@ MAP = { # 8 6 8 7 4 9 7 }.transform_keys(&:to_set) -def solve(signals, output) - all = signals + output - one = all.find {|x| x.length == 2 } - four = all.find {|x| x.length == 4 } - seven = all.find {|x| x.length == 3 } - eight = all.find {|x| x.length == 7 } +# # My original solution to part two +# def solve(signals, output) +# all = signals + output +# one = all.find {|x| x.length == 2 } +# four = all.find {|x| x.length == 4 } +# 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 } - e = (?a..?g).find {|x| signals.count {|s| s.include?(x) } == 4 } - f = (?a..?g).find {|x| signals.count {|s| s.include?(x) } == 9 } +# a = (seven.chars - one.chars)[0] +# c = (one.chars - [f])[0] +# d = (four.chars - [b, c, f])[0] +# g = (eight.chars - [a, b, c, d, e, f])[0] - a = (seven.chars - one.chars)[0] - 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 - 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 end