Need Programming Assitance.

This reminds me of a problem where you can't use ANY whitespace characters (newline, spaces or even delimiters), and you have a maximum of 10 variables.

(It's a Rubik's Cube Solver)...

I think ONE person in the entire competition (I'm pretty sure this is for Ontario only??) got it, and I actually have English class with him! (LOL he's #19 in the world from the IOI 2008)
 
1. Use a language other than Perl.

You can write good code in any language and you can write bad code in any language, but some languages lend themselves to writing bad code. Perl is one of them.
 
TIMTOWTDI:
Code:
#!usr/bin/perl

use warnings FATAL => 'all';
use strict;

# UFR UF UFL UR U UL UBR UB UBL    DFR DF DFL DR D DL DBR DB DBL
# FUR FU FUL FR F FL FDR FD FDL    BUR BU BUL BR B BL BDR BD BDL
# RUF RU RUB RF R RB RDF RD RDB    LUF LU LUB LF L LB LDF LD LDB
my %perms = ( U => [ 6, 3, 0, 7, 4, 1, 8, 5, 2,   9,10,11,12,13,14,15,16,17,
                    38,37,36,21,22,23,24,25,26,  47,46,45,30,31,32,33,34,35,
                    27,28,29,39,40,41,42,43,44,  18,19,20,48,49,50,51,52,53]
            , D => [ 0, 1, 2, 3, 4, 5, 6, 7, 8,  15,12, 9,16,13,10,17,14,11,
                    18,19,20,21,22,23,44,43,42,  27,28,29,30,31,32,53,52,51,
                    36,37,38,39,40,41,33,34,35,  45,46,47,48,49,50,24,25,26]
            , F => [45,48,51, 3, 4, 5, 6, 7, 8,  36,39,42,12,13,14,15,16,17,
                    20,23,26,19,22,25,18,21,24,  27,28,29,30,31,32,33,34,35,
                     2,37,38, 1,40,41, 0,43,44,  11,46,47,10,49,50, 9,52,53]
            , B => [ 0, 1, 2, 3, 4, 5,47,50,53,   9,10,11,12,13,14,38,41,44,
                    18,19,20,21,22,23,24,25,26,  29,32,35,28,31,34,27,30,33,
                    36,37, 8,39,40, 7,42,43, 6,  45,46,17,48,49,16,51,52,15]
            , R => [24, 1, 2,21, 4, 5,18, 7, 8,  33,10,11,30,13,14,27,16,17,
                     9,19,20,12,22,23,15,25,26,   0,28,29, 3,31,32, 6,34,35,
                    42,39,36,43,40,37,44,41,38,  45,46,47,48,49,50,51,52,53]
            , L => [ 0, 1,26, 3, 4,23, 6, 7,20,   9,10,35,12,13,32,15,16,29,
                    18,19,11,21,22,14,24,25,17,  27,28, 2,30,31, 5,33,34, 8,
                    36,37,38,39,40,41,42,43,44,  51,48,45,52,49,46,53,50,47] );

#### subs

sub turn_face($@) {
    my ($face,@stickers) = @_;
    map { $stickers[$_] } @{$perms{$face}} }

sub apply_alg($@) {
    my ($alg,@stickers) = @_;
    my @out = @{[@stickers]};
    @out = turn_face $_, @out for split //, $alg;
    @out }

sub is_solved(@) { "@_" eq join " ", 0..53 }

sub count_repetitions($) {
    my $alg = shift;
    my @stickers = 0..53;
    my $count = 0;

    for (;;) {
        @stickers = apply_alg $alg, @stickers;
        $count++;
        if (is_solved @stickers) {
            return $count } } }

#### main

for (;;) {
    print "> ";
    my $alg = <STDIN>;

    $alg =~ s/[^UDFBRL'2]//g;
    $alg =~ s/(.)'/$1$1$1/g;
    $alg =~ s/(.)2/$1$1/g;

    print "Number of repetitions for $alg: " . (count_repetitions $alg) . "\n" }

Running:
Code:
$ perl temp.pl
> R
Number of repetitions for R: 4
> L D
Number of repetitions for LD: 105
> R' U' R U' R' U2 R
Number of repetitions for RRRUUURUUURRRUUR: 6
> ^C

I need to come up with some more things to program so I can practice.
http://projecteuler.net/
 
Last edited:
Back
Top