Created
December 15, 2021 14:00
-
-
Save pjlsergeant/71d5ea177a18a4b5a51e157bb5eb5731 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!perl | |
use strict; | |
use warnings; | |
# key insights: | |
# | |
# * things having to happen "simultaneously" is misleading. Because we only add | |
# into the middle each time, you can start with each pair, work out what | |
# happens there, and then move on to the next one | |
# | |
# * we only need to figure out how many of each letter is created, we don't | |
# need to build the sequence | |
# | |
# * we are traversing a graph. OV -> V actually means OV -> ( OV, VV ) + add one | |
# point to the V count | |
# | |
my $debug = 1; | |
my %starting_counts = ( N => 2, C => 1, B => 1 ); | |
# This is our core data structure. We have a pair, and we have the number of | |
# iterations the pair started at | |
my @sites = ( [ NN => 0 ], [ NC => 0 ], [ CB => 0 ] ); | |
my %dictionary = # CH -> B becomes CH -> ( CB, BH, B ) | |
map { | |
my ( $from, $to ) = split( / \-> /, $_ ); | |
my $left = substr( $from, 0, 1 ) . $to; | |
my $right = $to . substr( $from, 1, 1 ); | |
$from => [ $left, $right, $to ]; | |
} split( | |
/\n/, | |
"CH -> B | |
HH -> N | |
CB -> H | |
NH -> C | |
HB -> C | |
HC -> B | |
HN -> C | |
NN -> C | |
BH -> H | |
NC -> B | |
NB -> B | |
BN -> B | |
BB -> N | |
BC -> B | |
CC -> N | |
CN -> C" | |
); | |
# Take an element from the front of the sites | |
while ( my $head = shift(@sites) ) { | |
my ( $site, $iteration ) = @$head; | |
print "Head is [$site] it[$iteration], stack-size[" . @sites . "]\n" | |
if $debug; | |
# If it's at 40, then we discard it | |
if ( $iteration == 40 ) { | |
print "\tDiscarding at 40\n" if $debug; | |
next; | |
} | |
# Is it an active site? If not, discard it | |
my $reaction = $dictionary{$site}; | |
unless ($reaction) { | |
print "\tSite [$site] is unreactive\n" if $debug; | |
} | |
my ( $left_site, $right_site, $product ) = @$reaction; | |
print "\tSite [$site] makes [$product], so [$left_site],[$right_site]\n" | |
if $debug; | |
# Count the new thing we made | |
$starting_counts{$product}++; | |
# Add the newly-created sites to the front of the stack | |
unshift( @sites, [ $right_site, $iteration + 1 ] ); | |
unshift( @sites, [ $left_site, $iteration + 1 ] ); | |
} | |
use Data::Printer; | |
p %starting_counts; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment