From 9498021941a791e067c59c6d803e86ded73cb191 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 31 Dec 2015 11:04:13 -0800 Subject: [PATCH] update --- day6.scrbl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/day6.scrbl b/day6.scrbl index fb943e4..e3c81d0 100644 --- a/day6.scrbl +++ b/day6.scrbl @@ -16,7 +16,7 @@ Our @link-rp["day6-input.txt"]{input} is a list instructions for turning on (or We need to a) create a data structure to hold our grid of lights, then b) step through the instructions on the list, and then c) count how many lights are lit at the end. -When you need random access across a set of items that has a fixed size, you should think @racket[vector]. (It would be possible to do this problem with a @racket[hash], but it will be a lot slower.) The grid-ness of the problem might suggest a two-dimensional vector — e.g., a 1000-unit vector where each slot holds another 1000-unit vector. But this doesn't buy us any convenience. We'll just use a single @racket[(* 1000 1000)]-unit vector, and translate our Cartesian coordinates into linear vector indexes by treating a coordinate like @tt{(246, 139)} as @racket[246139]. +When you need random access across a fixed-size set of items, you should think @secref["vectors" #:doc '(lib "scribblings/guide/guide.scrbl")]. (We could do this problem with a @seclink["hash-tables" #:doc '(lib "scribblings/guide/guide.scrbl")]{hash table}, but it would be a lot slower.) The grid-ness of the problem might suggest a two-dimensional vector — e.g., a 1000-unit vector where each slot holds another 1000-unit vector. But this doesn't buy us any convenience. We'll just use a single @racket[(* 1000 1000)]-unit vector, and translate our Cartesian coordinates into linear vector indexes by treating a coordinate like @tt{(246, 139)} as @racket[246139]. Each instruction consists of two pieces. First, an operation: either @italic{turn on}, @italic{turn off}, or @italic{toggle} (meaning, invert the current state of the bulb). Second, a definition of a rectangular segment of the grid that the operation will be applied to (e.g., @italic{333,60 through 748,159}). Therefore, a natural way to model each instruction is as a Racket function followed by four numerical arguments.