Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.064 X-Spam-Evidence: '*H*': 0.87; '*S*': 0.00; 'sys': 0.07; 'similar,': 0.09; 'cc:addr:python-list': 0.11; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'ha!': 0.16; 'simplified': 0.16; 'subject:Programming': 0.16; 'all.': 0.16; 'wrote:': 0.18; '(not': 0.18; 'bit': 0.19; 'solution.': 0.20; 'seems': 0.21; 'cc:addr:python.org': 0.22; 'fairly': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'change,': 0.30; 'dec': 0.30; 'message-id:@mail.gmail.com': 0.30; 'maybe': 0.34; 'could': 0.34; 'subject:with': 0.35; 'problem.': 0.35; 'something': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'shorter': 0.36; 'pm,': 0.38; 'bad': 0.39; 'though,': 0.39; 'even': 0.60; 'most': 0.60; 'full': 0.61; 'simple': 0.61; 'real': 0.63; 'hours': 0.66; 'six': 0.68; 'applying': 0.72; 'notable': 0.91; 'step.': 0.91; 'to:none': 0.92; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=UgNpsk31NFaTW4VoZcEgE4kaAcHfABv/BKmtJCo0cmY=; b=HwaO5gX/I/rT2xviokD2Pxo0Q/EAyuwo9pXxtDtSdY9F/wdsf9tvKVrksx1y7ucLgD PKtbqQylPmjYUXCQCbVkLGxJ9HWrotPN0A15SrmT/AjgfQRg5IAZA9wLm9/yksJmWPOM 2t5ibPyFj0RQXeYIYQRE386ecx2bXJGui7P+LXXexBP9KG4nwkOdsskrIajxXTpLyMSY mxnPiV28Rs3SrD2frS8xvo5K10uHlkgxY6gDU+v20FOprKN+hIE+JuZFoUhRcmauzhDl hw55w6KbA7WPgGCuKNOIjMzeV5t5Ybq/jHpYQZj69s2QuCiipxvjh6tiLfdXeRFTKLSw cTqw== MIME-Version: 1.0 X-Received: by 10.66.164.136 with SMTP id yq8mr26742132pab.67.1386665440958; Tue, 10 Dec 2013 00:50:40 -0800 (PST) In-Reply-To: References: Date: Tue, 10 Dec 2013 19:50:40 +1100 Subject: Re: Programming puzzle with boolean circuits From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 78 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386665450 news.xs4all.nl 2952 [2001:888:2000:d::a6]:52987 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61452 On Tue, Dec 10, 2013 at 1:21 PM, Chris Angelico wrote: > Output: (I tweaked my __repr__ functions to parenthesize for clarity) > > (((not ((($1 and $2) or ($1 and $4)) or ($2 and $4)) and not ((($1 and > $2) and $4) or (not ((($1 and $2) or ($1 and $4)) or ($2 and $4)) and > (($1 or $2) or $4)))) or ((not ((($1 and $2) or ($1 and $4)) or ($2 > and $4)) and (($1 or $2) or $4)) and ($2 or $4))) or ((not ((($1 and > $2) and $4) or (not ((($1 and $2) or ($1 and $4)) or ($2 and $4)) and > (($1 or $2) or $4))) and ((($1 and $2) or ($1 and $4)) or ($2 and > $4))) and ($2 and $4))) > (((not ((($1 and $2) or ($1 and $4)) or ($2 and $4)) and not ((($1 and > $2) and $4) or (not ((($1 and $2) or ($1 and $4)) or ($2 and $4)) and > (($1 or $2) or $4)))) or ((not ((($1 and $2) or ($1 and $4)) or ($2 > and $4)) and (($1 or $2) or $4)) and ($1 or $4))) or ((not ((($1 and > $2) and $4) or (not ((($1 and $2) or ($1 and $4)) or ($2 and $4)) and > (($1 or $2) or $4))) and ((($1 and $2) or ($1 and $4)) or ($2 and > $4))) and ($1 and $4))) > (((not ((($1 and $2) or ($1 and $4)) or ($2 and $4)) and not ((($1 and > $2) and $4) or (not ((($1 and $2) or ($1 and $4)) or ($2 and $4)) and > (($1 or $2) or $4)))) or ((not ((($1 and $2) or ($1 and $4)) or ($2 > and $4)) and (($1 or $2) or $4)) and ($2 or $1))) or ((not ((($1 and > $2) and $4) or (not ((($1 and $2) or ($1 and $4)) or ($2 and $4)) and > (($1 or $2) or $4))) and ((($1 and $2) or ($1 and $4)) or ($2 and > $4))) and ($2 and $1))) > > Okay, so maybe the brute-force-discovered version isn't so bad after all. :) I added the same parenthesizing to the brute force searcher and reran it through 'time'. ~$1 = (($4 or not (($1 and $2) or ($4 and ($1 or $2)))) and (($4 and not (($1 and $2) or ($4 and ($1 or $2)))) or (($2 or not (($1 and $2) or ($4 and ($1 or $2)))) and (($2 and not (($1 and $2) or ($4 and ($1 or $2)))) or not (($2 or ($1 or $4)) and (($2 and ($1 and $4)) or not (($1 and $2) or ($4 and ($1 or $2))))))))) ~$2 = (($4 or not (($1 and $2) or ($4 and ($1 or $2)))) and (($4 and not (($1 and $2) or ($4 and ($1 or $2)))) or (($1 or not (($1 and $2) or ($4 and ($1 or $2)))) and (($1 and not (($1 and $2) or ($4 and ($1 or $2)))) or not (($2 or ($1 or $4)) and (($2 and ($1 and $4)) or not (($1 and $2) or ($4 and ($1 or $2))))))))) ~$4 = (($2 or not (($1 and $2) or ($4 and ($1 or $2)))) and (($2 and not (($1 and $2) or ($4 and ($1 or $2)))) or (($1 or not (($1 and $2) or ($4 and ($1 or $2)))) and (($1 and not (($1 and $2) or ($4 and ($1 or $2)))) or not (($2 or ($1 or $4)) and (($2 and ($1 and $4)) or not (($1 and $2) or ($4 and ($1 or $2))))))))) real 362m12.261s user 362m7.186s sys 0m0.064s Yes, that is indeed six full hours of CPU time for a simple problem. Ha! It seems to have come up with something fairly similar, actually. A bit shorter than the algebraic solution. brute: (($4 or not (($1 and $2) or ($4 and ($1 or $2)))) and (($4 and not (($1 and $2) or ($4 and ($1 or $2)))) or (($2 or not (($1 and $2) or ($4 and ($1 or $2)))) and (($2 and not (($1 and $2) or ($4 and ($1 or $2)))) or not (($2 or ($1 or $4)) and (($2 and ($1 and $4)) or not (($1 and $2) or ($4 and ($1 or $2))))))))) algebra: (((not ((($1 and $2) or ($1 and $4)) or ($2 and $4)) and not ((($1 and $2) and $4) or (not ((($1 and $2) or ($1 and $4)) or ($2 and $4)) and (($1 or $2) or $4)))) or ((not ((($1 and $2) or ($1 and $4)) or ($2 and $4)) and (($1 or $2) or $4)) and ($2 or $4))) or ((not ((($1 and $2) and $4) or (not ((($1 and $2) or ($1 and $4)) or ($2 and $4)) and (($1 or $2) or $4))) and ((($1 and $2) or ($1 and $4)) or ($2 and $4))) and ($2 and $4))) Here's the most notable difference: two_or_three = (a & b) | (a & c) | (b & c) can be simplified to: two_or_three = (a & b) | (c & (a | b)) That's one less gate in an early step. Even applying that change, though, the brute-forced solution is still a bit shorter. Looks like the algebraic solution could have a bit of optimization done! ChrisA