{ "cells": [ { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "require 'set'\n", "require 'awesome_print'" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "digits = (1..9).to_a" ] }, { "cell_type": "code", "execution_count": 88, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ ":full_subbags" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Array\n", " def subbags(n)\n", " return enum_for(:subbags, n) unless block_given?\n", " if n == 1\n", " yield [self]\n", " else\n", " (0..self.length).each do |i|\n", " self.combination(i).each do |f|\n", " (self - f).subbags(n-1).each do |r|\n", " yield [f] + r\n", " end\n", " end\n", " end\n", " end\n", " end\n", " \n", " def full_subbags(n)\n", " return enum_for(:full_subbags, n) unless block_given?\n", " self.subbags(n).select {|bags| bags.none? {|b| b.empty?}}.each do |bs|\n", " yield bs\n", " end\n", " end\n", "end" ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1, 2, 3]]\n" ] } ], "source": [ "[1,2,3].subbags(1) {|i| puts i}" ] }, { "cell_type": "code", "execution_count": 90, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[[[1, 2, 3]]]" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1,2,3].subbags(1).to_a" ] }, { "cell_type": "code", "execution_count": 91, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[], [1, 2, 3]]\n", "[[1], [2, 3]]\n", "[[2], [1, 3]]\n", "[[3], [1, 2]]\n", "[[1, 2], [3]]\n", "[[1, 3], [2]]\n", "[[2, 3], [1]]\n", "[[1, 2, 3], []]\n" ] }, { "data": { "text/plain": [ "0..3" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1,2,3].subbags(2) {|i| puts i}" ] }, { "cell_type": "code", "execution_count": 92, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[[[], [1, 2, 3]], [[1], [2, 3]], [[2], [1, 3]], [[3], [1, 2]], [[1, 2], [3]], [[1, 3], [2]], [[2, 3], [1]], [[1, 2, 3], []]]" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1,2,3].subbags(2).to_a" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1,2,3].subbags(2).to_a.length" ] }, { "cell_type": "code", "execution_count": 94, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[[[], [], [1, 2, 3]], [[], [1], [2, 3]], [[], [2], [1, 3]], [[], [3], [1, 2]], [[], [1, 2], [3]], [[], [1, 3], [2]], [[], [2, 3], [1]], [[], [1, 2, 3], []], [[1], [], [2, 3]], [[1], [2], [3]], [[1], [3], [2]], [[1], [2, 3], []], [[2], [], [1, 3]], [[2], [1], [3]], [[2], [3], [1]], [[2], [1, 3], []], [[3], [], [1, 2]], [[3], [1], [2]], [[3], [2], [1]], [[3], [1, 2], []], [[1, 2], [], [3]], [[1, 2], [3], []], [[1, 3], [], [2]], [[1, 3], [2], []], [[2, 3], [], [1]], [[2, 3], [1], []], [[1, 2, 3], [], []]]" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1,2,3].subbags(3).to_a" ] }, { "cell_type": "code", "execution_count": 95, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "27" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1,2,3].subbags(3).to_a.length" ] }, { "cell_type": "code", "execution_count": 96, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "i: 0, f: []\n", "i: 1, f: [1]\n", "i: 1, f: [2]\n", "i: 1, f: [3]\n", "i: 2, f: [1, 2]\n", "i: 2, f: [1, 3]\n", "i: 2, f: [2, 3]\n", "i: 3, f: [1, 2, 3]\n" ] }, { "data": { "text/plain": [ "0..3" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(0..[1,2,3].length).each do |i|\n", " [1,2,3].combination(i).each do |f|\n", " puts \"i: #{i}, f: #{f}\"\n", " end\n", "end" ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[[[1], [2], [3, 4]], [[1], [3], [2, 4]], [[1], [4], [2, 3]], [[1], [2, 3], [4]], [[1], [2, 4], [3]], [[1], [3, 4], [2]], [[2], [1], [3, 4]], [[2], [3], [1, 4]], [[2], [4], [1, 3]], [[2], [1, 3], [4]], [[2], [1, 4], [3]], [[2], [3, 4], [1]], [[3], [1], [2, 4]], [[3], [2], [1, 4]], [[3], [4], [1, 2]], [[3], [1, 2], [4]], [[3], [1, 4], [2]], [[3], [2, 4], [1]], [[4], [1], [2, 3]], [[4], [2], [1, 3]], [[4], [3], [1, 2]], [[4], [1, 2], [3]], [[4], [1, 3], [2]], [[4], [2, 3], [1]], [[1, 2], [3], [4]], [[1, 2], [4], [3]], [[1, 3], [2], [4]], [[1, 3], [4], [2]], [[1, 4], [2], [3]], [[1, 4], [3], [2]], [[2, 3], [1], [4]], [[2, 3], [4], [1]], [[2, 4], [1], [3]], [[2, 4], [3], [1]], [[3, 4], [1], [2]], [[3, 4], [2], [1]]]" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1,2,3,4].full_subbags(3).to_a" ] }, { "cell_type": "code", "execution_count": 98, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "18150" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "digits.full_subbags(3).to_a.length" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ ":euler_product" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def euler_product(subbags)\n", " products = []\n", " a, b, c = subbags\n", " csort = c.sort\n", " a.permutation.each do |ap|\n", " an = ap.map {|d| d.to_s}.join.to_i\n", " b.permutation.each do |bp|\n", " bn = bp.map {|d| d.to_s}.join.to_i\n", " cn = (an * bn)\n", " cp = cn.to_s.split('').map {|d| d.to_i}\n", " if cp.sort == csort\n", " products << cn\n", " end\n", " end\n", " end\n", " products\n", "end" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "123" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1,2,3].map {|d| d.to_s}.join.to_i" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "123.to_s.split('').map {|d| d.to_i}" ] }, { "cell_type": "code", "execution_count": 85, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1,2,3,4].full_subbags(3).flat_map {|bs| euler_product bs}.to_set" ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[7254]" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "euler_product([[3, 9], [1, 8, 6], [7, 2, 5, 4]])" ] }, { "cell_type": "code", "execution_count": 87, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "45228" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "digits.full_subbags(3).flat_map {|bs| euler_product bs}.to_set.sum" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Ruby 2.4.0", "language": "ruby", "name": "ruby" }, "language_info": { "file_extension": ".rb", "mimetype": "application/x-ruby", "name": "ruby", "version": "2.4.0" } }, "nbformat": 4, "nbformat_minor": 0 }