progress
This commit is contained in:
parent
4fa2c17e4a
commit
3a4bec5028
40
2015_day_3/README.md
Normal file
40
2015_day_3/README.md
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
|
||||||
|
Use realloc/malloc?
|
||||||
|
|
||||||
|
To me, this is kinda a memory management problem. Basically, we must create a 2D array of infinite size. Which we can't, as I don't have infinite memory (640K ought to be enough for anyone? Maybe I should see if I can compile this and run it on a virtual 8088). Fortunately, the file can be dealt with using fgetc, so I don't need to allocate that. So now I need to learn the allocation functions in C.
|
||||||
|
|
||||||
|
|
||||||
|
XXX ^N
|
||||||
|
XSX S = 2,2
|
||||||
|
XXX Next Command = V (South 1)
|
||||||
|
|
||||||
|
XXX ^N
|
||||||
|
X1X S = 1,2
|
||||||
|
XSX Next Command = V (South 1)
|
||||||
|
|
||||||
|
The way I'm thinking of this is as a grid of say 9x9. Santa starts in the centre, incrementing the value of our centre element by one. Then we get the order to move by one in North, East, South or West. That puts Santa on the edge of the array, incrementing that element. Then say we get the order to go outside the predefined area. That's where the brain get stretched. We need to add a new row or column to the array, then move Santa onto there - which is fine, except the addresses involved need translated if we're adding rows to the South or West, as presumably the space defaults to being added to the North side or East side, empty on those sides, so we have to shuffle the whole used array a step North or East, translate Santa's coordinates to match, then move Santa to the new cordinates as commanded by the command, increment there and then start the whole process again. This feels like a job for a state machine
|
||||||
|
So it's time for me to learn the C stdlib memory allocation tools.
|
||||||
|
A pointer for a 2D array of ints is int ** array;
|
||||||
|
calloc() seems to be the simplest method for dynamic reallocation.
|
||||||
|
https://stackoverflow.com/questions/16715633/multidimensional-arrays-allocated-through-calloc This was very helpful, but spooked me, as apparently i can't assume that my memory is continuous (I guess if I declare new variables after the array, then that might intrude on where I want to be able to expand the array into? That wouldn't be too hard, but still something to be mindful of). The other thing is the way the array is addressed. The 'long 1D array' solution, where the 2d array is actually just a its rows stuck end to end, doesn't really appeal, but its easier to allocate, so it might be easiest to have an array addressing function - needs doing anyway with the translations going on.
|
||||||
|
Adding South rows ought to be simple, as that's just pushing all the values further along the array, to leave the first row space empty. North rows are also super easy, it's just adding extra space up there. East and West columns are the problem, doing that move and resetting the addressing (needs much less adjustment for North and South rows). Helpfully, Santa only moves horizontally and vertically, and only one square at a time, so only one row must be added at a time.
|
||||||
|
https://newton.ex.ac.uk/teaching/resources/jmr/appendix-growable.html
|
||||||
|
Upon adding this, I figured that it would be best to adopt a struct to copy the array between instances of it
|
||||||
|
|
||||||
|
Had a chat with am AI, it reckoned that the thing to do was to expand the array by a row or column when Santa was detected as being out of bounds
|
||||||
|
|
||||||
|
https://adventofcode.com/2015/day/3
|
||||||
|
--- Day 3: Perfectly Spherical Houses in a Vacuum ---
|
||||||
|
|
||||||
|
Santa is delivering presents to an infinite two-dimensional grid of houses.
|
||||||
|
|
||||||
|
He begins by delivering a present to the house at his starting location, and then an elf at the North Pole calls him via radio and tells him where to move next. Moves are always exactly one house to the north (^), south (v), east (>), or west (<). After each move, he delivers another present to the house at his new location.
|
||||||
|
|
||||||
|
However, the elf back at the north pole has had a little too much eggnog, and so his directions are a little off, and Santa ends up visiting some houses more than once. How many houses receive at least one present?
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
> delivers presents to 2 houses: one at the starting location, and one to the east.
|
||||||
|
^>v< delivers presents to 4 houses in a square, including twice to the house at his starting/ending location.
|
||||||
|
^v^v^v^v^v delivers a bunch of presents to some very lucky children at only 2 houses.
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user