in reply to tic-tac-toe regex golf

A different approach - here's my generic solution to find out who is the winner of the tic-tac-toe. The basic idea is to list out the winning patterns, and then automatically generate regular expressions based on these patterns. This solution assumes that there is only one winner in the game.

#!/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
And the output is -
X wins O wins X wins No body wins