@ -16,11 +16,11 @@
<day01-test> ]
@ section{ Where does the elevator land? }
@ i section{ Where does the elevator land? }
The building has an indefinite number of floors in both directions. So the ultimate destination is just the number of up movements minus the number of down movements. In other words , a left parenthesis = @racket [ 1 ] and a right parenthesis = @racket [ -1 ] , and we sum them.
@ racket[ regexp-match* ] will return a list of all occurrences of one string within another. The length of this list is the number of occurrences. Therefore , we can use it to count the ups and downs.
@ i racket[ regexp-match* ] will return a list of all occurrences of one string within another. The length of this list is the number of occurrences. Therefore , we can use it to count the ups and downs.
@chunk [ <day01-setup>
( require racket rackunit )
@ -40,7 +40,7 @@ The building has an indefinite number of floors in both directions. So the ultim
( define ( q1 str )
( get-destination str ) ) ]
@ subsection{ Alternate approach: numerical conversion }
@ i subsection{ Alternate approach: numerical conversion }
Rather than counting matches with @racket [ regexp-match* ] , we could also convert the string of parentheses directly into a list of numbers.
@ -54,11 +54,11 @@ Rather than counting matches with @racket[regexp-match*], we could also convert
( define ( q1-alt str )
( apply + ( elevator-string->ints str ) ) ) ]
@ section[ #:tag " q2 " ] { At what point does the elevator enter the basement? }
@ i section[ #:tag " q2 " ] { At what point does the elevator enter the basement? }
The elevator is in the basement whenever it ' s at a negative-valued floor. So instead of looking at its ultimate destination , we need to follow the elevator along its travels , computing its intermediate destinations , and stop as soon as it reaches a negative floor.
We could characterize this as a problem of tracking @italic { cumulative values } or @italic { state } . Either way , @ racket[ for/fold ] is the weapon of choice. We ' ll determine the relative movement at each step , and collect these in a list. ( The @racket [ get-destination ] function is used within the loop to convert each parenthesis into a relative movement , either @racket [ 1 ] or @racket [ -1 ] . ) On each loop , @racket [ for/fold ] checks the cumulative value of these positions , and stops when they imply a basement value. The length of this list is our answer.
We could characterize this as a problem of tracking @italic { cumulative values } or @italic { state } . Either way , @ i racket[ for/fold ] is the weapon of choice. We ' ll determine the relative movement at each step , and collect these in a list. ( The @racket [ get-destination ] function is used within the loop to convert each parenthesis into a relative movement , either @racket [ 1 ] or @racket [ -1 ] . ) On each loop , @racket [ for/fold ] checks the cumulative value of these positions , and stops when they imply a basement value. The length of this list is our answer.
@margin-note { Nothing wrong with @racket [ foldl ] and @racket [ foldr ] , but @racket [ for/fold ] is more flexible , and makes more readable code. }
@ -76,9 +76,9 @@ We could characterize this as a problem of tracking @italic{cumulative values} o
( length relative-movements ) ) ]
@ subsection{ Alternate approaches: @tt { for/first } or @tt { for/or } }
@ i subsection{ Alternate approaches: @tt { for/first } or @tt { for/or } }
When you need to stop a loop the first time a condition occurs , you can also consider @ racket[ for/first ] or @ racket[ for/or ] . The difference is that @racket [ for/first ] ends after the first evaluation of the body , but @racket [ for/or ] evaluates the body every time , and ends the first time the body is not @racket [ #f ] .
When you need to stop a loop the first time a condition occurs , you can also consider @ i racket[ for/first ] or @ i racket[ for/or ] . The difference is that @racket [ for/first ] ends after the first evaluation of the body , but @racket [ for/or ] evaluates the body every time , and ends the first time the body is not @racket [ #f ] .
The two are similar. The choice comes down to readability and efficiency — meaning , if each iteration of the loop is expensive , you ' ll probably want to cache intermediate values , which means you might as well use @racket [ for/fold ] .