about summary refs log tree commit diff stats
path: root/doc/pydoc/ranger.defaults.html
blob: 22015dc05745e38c8d7b07fb4e225e6e6d60e3b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Python: package ranger.defaults</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head><body bgcolor="#f0f0f8">

<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<tr bgcolor="#7799ee">
<td valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial">&nbsp;<br><big><big><strong><a href="ranger.html"><font color="#ffffff">ranger</font></a>.defaults</strong></big></big></font></td
><td align=right valign=bottom
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/home/hut/work/ranger/ranger/defaults/__init__.py">/home/hut/work/ranger/ranger/defaults/__init__.py</a></font></td></tr></table>
    <p><tt>Default&nbsp;options&nbsp;and&nbsp;configration&nbsp;files</tt></p>
<p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#aa55cc">
<td colspan=3 valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Package Contents</strong></big></font></td></tr>
    
<tr><td bgcolor="#aa55cc"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
<td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="ranger.defaults.apps.html">apps</a><br>
</td><td width="25%" valign=top><a href="ranger.defaults.keys.html">keys</a><br>
</td><td width="25%" valign=top><a href="ranger.defaults.options.html">options</a><br>
</td><td width="25%" valign=top></td></tr></table></td></tr></table>
</body></html>
ch[0]; for $match[1].split(", ") { if $_ ~~ /(\d) \s (.*) \s bag/ -> $contain_bag { push %bag{$bag}, { count => $contain_bag[0].Int, bag => $contain_bag[1].Str }; } } } } #+END_SRC For part 1, we wrap the whole thing in a while loop that runs until =$previous_count= is equal to =$count=. The first =for= loop within the =MAIN= loop is pushing bags that are to valid to =@valid_bags=. Valid bags are the bags that can contain at least one =shiny gold= bag. Take the case where =bag 1= holds =shiny gold= bag & =bag 2= holds =bag 1=. This means =bag 2= can also hold a =shiny gold= bag by holding =bag 1= so it should be included in the count. Now the reason we have the =MAIN= loop is that if the statement that says =bag 2 holds bag 1= comes before the statement that says =bag 1 holds shiny gold= then =bag 2= is not counted in =@valid_bags=. This causes the =$count= to be less than the actual count, to account for this we just loop indefinitely until =$previous_count= is equal to =$count=. =$previous_count= holds the value of =$count= at the start of =MAIN= loop. If the value of =$count= isn't affected then it means that we've accounted for all valid bags & =$count= holds the final solution. #+BEGIN_SRC raku if $part == 1 { MAIN: while True { my $previous_count = $count; for keys %bag { for @(%bag{$_}) -> $contain { push @valid_bags, $_ if @valid_bags ∋ $contain<bag>; } } $count = 0; COUNT: for keys %bag { for @(%bag{$_}) -> $contain { if @valid_bags ∋ $contain<bag> { $count++; next COUNT; } } } last MAIN if $previous_count == $count; } } elsif $part == 2 { ... } say "Part $part: ", $count; #+END_SRC ** Part 2 For part 2, we will use =%bag= which was previous defined. And we define a subroutine: =count-bags=. =count-bags= counts the number of bags that some =$bag= holds. We have to pass =%bag= & =$bag= to =count-bags=. It'll also account for sub bags, it's an recursive function. The subroutine is easy to understand, if the bag doesn't hold any other bags then we return 0. If it holds other bags then we count the number of bags it's holding & add it to =$count=. Later we count the number of bags these sub bags are holding, if it's 0 then the loop exits, if not then we multiply the number of sub bags to the number of bags those sub bags are holding and add it to =$count=. This will be easier to understand with an example so take a sample input & follow the code manually. #+BEGIN_SRC raku # count-bags takes %bag & the bag for which we have to count the bags # & returns the count. Count here means the number of bags that will # be inside $bag. sub count-bags ( %bag, Str $bag --> Int # count will be an integer. ) { return 0 unless %bag{$bag}; my Int $count = 0; for @(%bag{$bag}) { my $sub_bags = count-bags(%bag, $_<bag>); $count += $_<count>; unless $sub_bags == 0 { $count += $_<count> * $sub_bags; } } return $count; } #+END_SRC We just run =count-bags= on 'shiny gold' & print the =$count=. #+BEGIN_SRC raku $count = count-bags(%bag, 'shiny gold'); #+END_SRC