diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-12-24 18:33:58 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-12-24 18:33:58 +0000 |
| commit | 0d9e18aca111f48c94ad0abf18a596f5c9c0ba71 (patch) | |
| tree | 221c28bc70e164d64efb7a9ddd7762faa8bebe90 /challenge-001 | |
| parent | b2bda687c42cfc1229440dfb0d392e5fbad8f1d1 (diff) | |
| download | perlweeklychallenge-club-0d9e18aca111f48c94ad0abf18a596f5c9c0ba71.tar.gz perlweeklychallenge-club-0d9e18aca111f48c94ad0abf18a596f5c9c0ba71.tar.bz2 perlweeklychallenge-club-0d9e18aca111f48c94ad0abf18a596f5c9c0ba71.zip | |
Add Brainfuck solution to challenge 001.2
Diffstat (limited to 'challenge-001')
| -rw-r--r-- | challenge-001/paulo-custodio/Makefile | 8 | ||||
| -rw-r--r-- | challenge-001/paulo-custodio/brainfuck.py | 2 | ||||
| -rw-r--r-- | challenge-001/paulo-custodio/brainfuck/ch-1.bf | 1 | ||||
| -rw-r--r-- | challenge-001/paulo-custodio/brainfuck/ch-1.pl | 22 | ||||
| -rw-r--r-- | challenge-001/paulo-custodio/brainfuck/ch-2.bf | 91 | ||||
| -rw-r--r-- | challenge-001/paulo-custodio/brainfuck/ch-2.pl | 36 |
6 files changed, 92 insertions, 68 deletions
diff --git a/challenge-001/paulo-custodio/Makefile b/challenge-001/paulo-custodio/Makefile index 24da326a16..c3c762d746 100644 --- a/challenge-001/paulo-custodio/Makefile +++ b/challenge-001/paulo-custodio/Makefile @@ -1,8 +1,2 @@ -all: brainfuck/ch-1.bf brainfuck/ch-2.bf +all: perl ../../challenge-001/paulo-custodio/test.pl - -brainfuck/ch-1.bf: brainfuck/ch-1.pl - perl brainfuck/ch-1.pl > $@ - -brainfuck/ch-2.bf: brainfuck/ch-2.pl - perl brainfuck/ch-2.pl > $@ diff --git a/challenge-001/paulo-custodio/brainfuck.py b/challenge-001/paulo-custodio/brainfuck.py index 7ef3443113..9d68e92acc 100644 --- a/challenge-001/paulo-custodio/brainfuck.py +++ b/challenge-001/paulo-custodio/brainfuck.py @@ -51,7 +51,7 @@ class TuringMachine: if do_trace: print("") elif op==",": - self.tape[self.ptr] = chr(sys.stdin.read(1)) + self.tape[self.ptr] = ord(sys.stdin.read(1)) elif op=="[": if self.tape[self.ptr]==0: self.find_close() diff --git a/challenge-001/paulo-custodio/brainfuck/ch-1.bf b/challenge-001/paulo-custodio/brainfuck/ch-1.bf deleted file mode 100644 index b1d94a8bb7..0000000000 --- a/challenge-001/paulo-custodio/brainfuck/ch-1.bf +++ /dev/null @@ -1 +0,0 @@ -[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++. diff --git a/challenge-001/paulo-custodio/brainfuck/ch-1.pl b/challenge-001/paulo-custodio/brainfuck/ch-1.pl deleted file mode 100644 index c9f6754286..0000000000 --- a/challenge-001/paulo-custodio/brainfuck/ch-1.pl +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env perl - -# Challenge 001 -# -# Challenge #1 -# Write a script to replace the character ‘e’ with ‘E’ in the string -# ‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ -# is found in the string. - -# Output brainfuck program - -use Modern::Perl; - -my $s = "Perl Weekly Challenge"; -my $text = ($s =~ tr/e/E/)." ".$s; - -for (split //, $text) { - print "[-]"; # zero cell - print "+" x ord($_); # set cell to ASCII value of character - print "."; # output character -} -print "\n"; diff --git a/challenge-001/paulo-custodio/brainfuck/ch-2.bf b/challenge-001/paulo-custodio/brainfuck/ch-2.bf index 0a252de458..507fc83b2e 100644 --- a/challenge-001/paulo-custodio/brainfuck/ch-2.bf +++ b/challenge-001/paulo-custodio/brainfuck/ch-2.bf @@ -1 +1,90 @@ -[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++. +#!/usr/bin/bf + +[ +# Challenge 001 +# +# Challenge #2 +# Write a one-liner to solve the FizzBuzz problem and print the numbers 1 +# through 20. However, any number divisible by 3 should be replaced by the word +# ‘fizz’ and any divisible by 5 by the word ‘buzz’. Those numbers that are both +# divisible by 3 and 5 become ‘fizzbuzz’. +] + +>++++[<+++++>-]< # Tape: *counter=20 +>+ # Tape: counter *index=1 +< # Tape: *counter index +[ # WHILE counter + # Tape: *counter index found_branch + + # MOVE index + >[->>+>+<<<] # Tape: counter *0 found index index + >>>[-<<<+>>>]< # Tape: counter index found *index + + # DIVIDE by 3 + >>>>+++<<<< # Tape: counter index found *index 0 0 0 3 + [->+>>+>-[<-]<[<<[->>>+<<<]>>>>+<<-<]<<] # divide + # Tape: 0 R 0 0 B' Q + >>>>[-]>[-]<<<< # cleanup all but R cursor on R + >>+<[-<->]<[[-]>>-<<]>>[<<+>>-]<< # check if R==0 + # Tape: counter index found 0 *divisible + [ # IF divisible by 3 + <<+ # Tape: counter index *inc_found + > + [-]>[-]< + >++++++++++[<++++++++++>-]<++. # "f" tape=102 + +++. # "i" tape=105 + >+++[<++++++>-]<-.. # "zz" tape=122 + [-] + + > + [-] + ] # END IF + + <<< # MOVE index + [->>+>+<<<] # Tape: counter *0 found index index + >>>[-<<<+>>>]< # Tape: counter index found *index + + # DIVIDE by 5 + >>>>+++++<<<< # Tape: counter index found *index 0 0 0 5 + [->+>>+>-[<-]<[<<[->>>+<<<]>>>>+<<-<]<<] # divide + # Tape: 0 R 0 0 B' Q + >>>>[-]>[-]<<<< # cleanup all but R cursor on R + >>+<[-<->]<[[-]>>-<<]>>[<<+>>-]<< # check if R==0 + # Tape: counter index found 0 *divisible + [ # IF divisible by 5 + <<+ # Tape: counter index *inc_found + > + + [-]>[-]< + >++++++++++[<++++++++++>-]<--. # "b" tape=98 + >++++[<+++++>-]<-. # "u" tape=117 + +++++.. # "zz" tape=122 + [-] + + > + [-] + ] # END IF + + << # Tape: counter index *found + >>+<[-<->]<[[-]>>-<<]>>[<<+>>-]<< # check if found==0 + [ # IF not yet found + + < # MOVE index + [->>>+>+<<<<] # Tape: counter *0 found 0 index index + >>>>[-<<<<+>>>>]< # Tape: counter index found 0 *index + + # PRINT NUMBER tape= 0 *M 0 0 0 + [>>>>++++++++++<<<<[->+>>+>-[<-]<[<<[->>>+<<<]>>>>+<<-<]<<] + ++++++++[->++++++<]>[-<+>]>>>>[-<<<<+>>>>]<[-]<<<]<[.<] + + >[-]>[-]>[-]>[-]<<<< # clear digits + < + [-] + ] # ENDIF + + [-]++++++++++.[-] # newline tape=0 + + << # Tape: *counter index found + + >+<- # Tape: *dec_counter inc_index +] # END WHILE counter diff --git a/challenge-001/paulo-custodio/brainfuck/ch-2.pl b/challenge-001/paulo-custodio/brainfuck/ch-2.pl deleted file mode 100644 index 65b62fd55a..0000000000 --- a/challenge-001/paulo-custodio/brainfuck/ch-2.pl +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env perl - -# Challenge 001 -# -# Challenge #2 -# Write a one-liner to solve the FizzBuzz problem and print the numbers 1 -# through 20. However, any number divisible by 3 should be replaced by the word -# ‘fizz’ and any divisible by 5 by the word ‘buzz’. Those numbers that are both -# divisible by 3 and 5 become ‘fizzbuzz’. - -# Output brainfuck program - -use Modern::Perl; - -my $text = ""; -for my $n (1..20) { - if ($n%15==0) { - $text .= "fizzbuzz\n"; - } - elsif ($n%3==0) { - $text .= "fizz\n"; - } - elsif ($n%5==0) { - $text .= "buzz\n"; - } - else { - $text .= "$n\n"; - } -} - -for (split //, $text) { - print "[-]"; # zero cell - print "+" x ord($_); # set cell to ASCII value of character - print "."; # output character -} -print "\n"; |
