in reply to tic-tac-toe regex golf
And the output is -#!/usr/local/bin/perl -w use strict; use re 'eval'; # A list of all winning patterns, could generate them # with some sort of algorithm, but it's easier just # to type them out. ;-) my @win = qw/ XXX...... X..X..X.. ......XXX ..X..X..X ...XXX... .X..X..X. X...X...X ..X.X.X.. /; foreach (@win) # dynamically generate regular expressions { # to look for matching winning patterns my $c = 0; s/X/!$c++?'([^-])':'(??{$1})'/ge; } while (my $game = <DATA>) { my $winner = 'No body'; foreach (@win) { $winner = $1, last if $game =~ /$_/; } print "$winner wins\n"; } __DATA__ XO-OX--OX OX-XO--XO OOXOXOXOO OX-XOOXOX
X wins O wins X wins No body wins
|
|---|