File tree Expand file tree Collapse file tree 1 file changed +13
-10
lines changed Expand file tree Collapse file tree 1 file changed +13
-10
lines changed Original file line number Diff line number Diff line change @@ -19,22 +19,25 @@ def part1
19
19
known_wires [ wire ] = signal . to_i
20
20
when /-\> /
21
21
input1 , gate , input2 , wire = line . scan ( /\w +/ )
22
- to_be_determined [ wire ] = { inputs : [ input1 , input2 ] , gate : gate }
22
+ op = case gate
23
+ when 'AND' ; :&
24
+ when 'OR' ; :|
25
+ when 'XOR' ; :^
26
+ else ; raise "lolwut: unknown gate #{ gate } "
27
+ end
28
+ to_be_determined [ wire ] = {
29
+ inputs : [ input1 , input2 ] ,
30
+ proc : -> { [ input1 , input2 ] . map { known_wires [ _1 ] } . reduce ( &op ) }
31
+ }
23
32
end
24
33
end
25
34
26
35
while to_be_determined . keys . any? { |wire | wire . start_with? ( 'z' ) } do
27
36
puts to_be_determined . keys . inspect if ENV [ 'DEBUG' ]
28
37
to_be_determined
29
- . select { |wire , data | data [ :inputs ] . all? { |input | known_wires . keys . include? ( input ) } }
30
- . each do |wire , data |
31
- known_wires [ wire ] =
32
- case data [ :gate ]
33
- when 'AND' ; known_wires [ data [ :inputs ] [ 0 ] ] & known_wires [ data [ :inputs ] [ 1 ] ]
34
- when 'OR' ; known_wires [ data [ :inputs ] [ 0 ] ] | known_wires [ data [ :inputs ] [ 1 ] ]
35
- when 'XOR' ; known_wires [ data [ :inputs ] [ 0 ] ] ^ known_wires [ data [ :inputs ] [ 1 ] ]
36
- else ; raise "lolwut: unknown gate #{ data [ :gate ] } "
37
- end
38
+ . select { |wire , gate | gate [ :inputs ] . all? { known_wires . key? ( _1 ) } }
39
+ . each do |wire , gate |
40
+ known_wires [ wire ] = gate [ :proc ] . call
38
41
to_be_determined . delete ( wire )
39
42
end
40
43
end
You can’t perform that action at this time.
0 commit comments