Last active
December 9, 2015 20:21
-
-
Save MadcapJake/558eff70f13d2ff2c223 to your computer and use it in GitHub Desktop.
Attempt at #adventofcode day 7 part 1
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
sub non-zero(%env) { | |
#| for debugging, removes wires with a value of zero | |
my %res; | |
for %env.kv -> $wire, $value { | |
if $value != 0 { %res.push: $wire => $value }; | |
} | |
%res; | |
} | |
sub sortfile($filename) { | |
#| places assignments at the beginning of a sorted input file | |
my @deferred; | |
for "day07/{$filename}".IO.lines { | |
if $_ ~~ / ^^ <:Number>+ ' -> ' <:Letter>+ / { | |
spurt "day07/sorted-{$filename}", "$_\n", :append; | |
} else { push @deferred, $_ } | |
} | |
for @deferred { spurt "day07/sorted-{$filename}", "$_\n", :append } | |
} | |
sub prefix:<u+^>(Int $i) { | |
#| uint16 bitwise negation | |
my @bin = $i.base(2).comb; | |
my @zer = @bin.unshift: |('0' xx 16 - @bin.elems); | |
my @com = @zer.map: { $_ ~~ '0' ?? '1' !! '0' } | |
return :2(@com.join).base(10).Int; | |
} | |
grammar CircuitStatement { | |
token TOP { [<statement> \n*]+ } | |
rule statement { [ <expression> || <input=wire> ] '->' <output=wire> } | |
token expression { <gate> || $<val>=<:Number>+ } | |
token gate { <AND> | <LSHIFT> | <RSHIFT> | <OR> | <NOT> } | |
rule AND { [ $<val>=<:Number>+ || <w1=wire> ] 'AND' <w2=wire> } | |
rule OR { <w1=wire> 'OR' <w2=wire> } | |
rule LSHIFT { <wire> 'LSHIFT' $<val>=<:Number>+ } | |
rule RSHIFT { <wire> 'RSHIFT' $<val>=<:Number>+ } | |
rule NOT { 'NOT' <wire> } | |
token wire { <[ a .. z ]>+ } | |
} | |
class CircuitActions { | |
has %.env; | |
method wire($/) { without %!env{~$/} { %!env{~$/} = 0 }; $/.make: ~$/ } | |
method AND ($/) { $/.make: ($<val> // %!env{~$<w1>.made}) +& %!env{~$<w2>.made} } | |
method OR ($/) { $/.make: %!env{~$<w1>.made} +| %!env{~$<w2>.made} } | |
method LSHIFT($/) { $/.make: %!env{~$<wire>.made} +< +$<val> } | |
method RSHIFT($/) { $/.make: %!env{~$<wire>.made} +> +$<val> } | |
method NOT ($/) { $/.make: u+^%!env{~$<wire>.made} } | |
method gate($/) { $/.make: $<AND>.made // $<OR>.made // $<LSHIFT>.made // $<RSHIFT>.made // $<OR>.made // $<NOT>.made } | |
method expression($/) { | |
with $<val> { $/.make: +$_ } | |
else { $/.make: $<gate>.made } | |
} | |
method statement($/) { | |
with $<input> { $/.make: %!env{$<output>.made} = %!env{~$<input>} } | |
else { $/.make: %!env{$<output>.made} = $<expression>.made } | |
} | |
} | |
my $circuit = CircuitActions.new; | |
sortfile('input1.txt'); | |
CircuitStatement.parsefile('day07/sorted-input1.txt', :actions($circuit)); | |
put non-zero($circuit.env); | |
# put $circuit.env; | |
# say $circuit.env<kh>; | |
# say $circuit.env<la>; | |
# say $circuit.env<lb>; | |
# say $circuit.env<lr>; # lf OR lq -> lr | |
# say $circuit.env<lc>; # lb OR la -> lc | |
# say $circuit.env<lu>; # lr AND lt -> lu | |
# say $circuit.env<lw>; # lc LSHIFT 1 -> lw | |
# say $circuit.env<lv>; # 1 AND lu -> lv | |
# say $circuit.env<lx>; # lw OR lv -> lx | |
# say $circuit.env<a>; # lx -> a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment