r/dailyprogrammer • u/nint22 1 2 • Nov 25 '13
[11/11/13] Challenge #142 [Easy] Falling Sand
(Easy): Falling Sand
Falling-sand Games are particle-simulation games that focus on the interaction between particles in a 2D-world. Sand, as an example, might fall to the ground forming a pile. Other particles might be much more complex, like fire, that might spread depending on adjacent particle types.
Your goal is to implement a mini falling-sand simulation for just sand and stone. The simulation is in 2D-space on a uniform grid, where we are viewing this grid from the side. Each type's simulation properties are as follows:
- Stone always stays where it was originally placed. It never moves.
- Sand keeps moving down through air, one step at a time, until it either hits the bottom of the grid, other sand, or stone.
Formal Inputs & Outputs
Input Description
On standard console input, you will be given an integer N which represents the N x N grid of ASCII characters. This means there will be N-lines of N-characters long. This is the starting grid of your simulated world: the character ' ' (space) means an empty space, while '.' (dot) means sand, and '#' (hash or pound) means stone. Once you parse this input, simulate the world until all particles are settled (e.g. the sand has fallen and either settled on the ground or on stone). "Ground" is defined as the solid surface right below the last row.
Output Description
Print the end result of all particle positions using the input format for particles.
Sample Inputs & Outputs
Sample Input
5
.....
#
#
.
Sample Output
.
. #
#
.
. ..
6
u/Hoten 0 1 Nov 25 '13
Here are some additional sample inputs for you to test your solution with. All are 13x13
2
u/missblit Nov 25 '13 edited Nov 25 '13
Overly compact C++
Edit: Now even more overly compact
13
u/kirsybuu 0 1 Nov 25 '13
D
Using some range goodness and ASCII hax to get a very simple and clean implementation.
8
u/SOMECLEVERUSERNAME2 Nov 25 '13
haskell
2
u/bheinks 0 0 Nov 25 '13 edited Nov 25 '13
Python
Are we to assume that a blank line represents
len(grid)number of spaces? I can get my code to look slightly less hacky with the spaces actually provided in the input, as opposed to a single newline.Edit: Now accounts for spaces under already moved grains.
5
u/GetRekt Nov 25 '13
C#
5
u/dunnowins Nov 25 '13 edited Nov 25 '13
A solution in Ruby since no one has posted one yet:
4
u/Mindrust Nov 27 '13 edited Nov 29 '13
Java
I see a lot of people have conditional statements to check for stones and sand underneath the dot character. As far as I can tell, that is unnecessary. Just checking for a space beneath the dot should be sufficient...unless I'm missing something.
}
7
u/aZeex2ai Nov 25 '13 edited Nov 25 '13
Python. Edit: OOP is so verbose in Python.
6
u/taterNuts Nov 26 '13 edited Dec 03 '13
learning python:
Edit: This isn't a super robust solution, and assumes each column drops 1 grain of sand each
3
u/5900 Nov 25 '13
perl #!/usr/bin/perl
3
u/OffPiste18 Nov 25 '13 edited Nov 25 '13
Scala. Yay tail recursion!
This one actually had some deceptively subtle edge cases... I'm noticing a few of the solutions try to go for overly-simple algorithms and miss cases like sand starting stacked on top of itself but still able to fall, or multiple stones in a column with sand falling in between. I think I have everything covered here.
3
u/chunes 1 2 Nov 25 '13
Java:
Note that the input parsing only works properly if blank lines are actually filled with spaces. I had to doctor the input given in the challenge a bit.
3
u/Tjd__ Nov 25 '13 edited Nov 25 '13
PhantomJS, swapping is hackish but kept curly brace count to 0.
3
u/Edward_H Nov 25 '13
My F# solution:
3
u/TheGag96 Nov 26 '13
Java. I edited the blank line into five spaces because that makes the most logical sense. I probably could have used just a single string and not a two dimensional array, but I think it's a bit more understandable and flexible this way.
3
u/ehaliewicz Nov 28 '13
Common Lisp solution
3
u/cahna Nov 28 '13
Moonscript
3
u/punk1290 Nov 30 '13
Very rough Python/First post on : dailyprogrammer
3
u/bcd87 Jan 07 '14 edited Jan 08 '14
This is my solution in C. If I could've done anything better I'd gladly hear it :)
5
u/martinvanostrand Nov 25 '13
Second time posting! I'm new to programming and would love to hear some advice about my code.
Java:
5
u/bheinks 0 0 Nov 26 '13
Icky Python one-liner
2
u/lordtnt Nov 25 '13
Python 2.7
2
u/domy94 Nov 25 '13 edited Nov 25 '13
C#
Any tips on making it shorter/better/more elegant are appreciated.
edit: enum was useless.
2
u/[deleted] Nov 25 '13 edited Nov 25 '13
Here's my Python 2.7 solution. Not very pythonic, but it's decently efficient as it only traverses the 'world' once. I believe all edge cases are handled properly. Input is read from a file rather than the console for ease of testing and use.
Custom test case input:
...and output:
Edit: grammar
Edit 2: cleanup
2
u/MotherOfTheShizznit Nov 25 '13
I believe the following is essentially the straightforward C-style algorithm. It goes column by column and starts at the bottom, working its way up to directly find the final position of each sand grain.
2
u/rectal_smasher_2000 1 1 Nov 25 '13
C++
2
u/jayt92 Nov 25 '13
Here is a pretty naive solution in Python (2.7).
2
u/toodim Nov 25 '13
Python 3.2. I got thrown off at first because the blank row doesn't actually have 5 spaces in the input data, so I added them in.
2
u/pbl24 Nov 25 '13 edited Nov 26 '13
My Python 2.7.3 solution.
Main program flow:
It basically works by starting at the bottom of the grid and "pulling" down grains of sand as it sees fit. Full source listing (including input parsing):
2
u/farmer_jo Nov 25 '13
Ruby
2
u/[deleted] Nov 25 '13
Really straightforward Python 3:
2
u/jmct Nov 25 '13 edited Nov 25 '13
Haskell
2
u/MCFRESH01 Nov 26 '13 edited Nov 26 '13
Ruby EDIT: now works with the 50 line test found below .. sort of
2
u/[deleted] Nov 26 '13
In Ruby:
2
u/[deleted] Nov 26 '13 edited Nov 26 '13
Emacs Lisp (with cl). The "clever" bit is that I did the falling just by using sort.
Example use:
2
u/vgbm 1 0 Nov 26 '13 edited Nov 26 '13
Here is what I believe to be some bad Java (66 lines, empty lines included, by my count):
2
u/0x746d616e Nov 26 '13
Straightforward solution in Go. Starts at the bottom and handles each sand pixel separately.
2
u/letalhell Nov 27 '13 edited Nov 27 '13
Very ugly...
Python 2.7
2
u/lardlad6342 Nov 27 '13
Here's my C++ solution, I'm a beginner; please give me any criticism you have so I can learn something!
2
u/spfy Nov 27 '13
I spent a lot of time on this. It works, but I feel it has more indents and is longer than everyone else's ): Anyway, here's my C solution:
I start at the top row and see if I can move any sand down before trying the next row. To make sure I don't miss any, I do that X times--where X is the number of total rows.
2
u/lets_see_exhibit_A Nov 27 '13
Java!
}
2
u/agambrahma Nov 27 '13
C++
2
u/thirdegree Nov 27 '13 edited Nov 27 '13
Python 2.7
For some reason I can't get it to touch the first line. Working on it. This is what I have for now though.
Criticism welcome.
Edit: found it
2
u/nSpace1988 Nov 27 '13 edited Nov 27 '13
Java
2
u/lukz 2 0 Nov 27 '13
BASIC (try it at www.calormen.com/jsbasic/)
Sample session
2
u/angertoast Nov 27 '13
C++
2
u/scpatton Nov 28 '13 edited Nov 28 '13
Java
2
u/_ewan Nov 29 '13
Python
First time posting here, trying to get back into development after many years away from it, so any feedback is welcome.
2
u/forthebooks Nov 29 '13
MATLAB. I'm not sure how to get rid of the apostrophes in the output.
Sample input:
Sample output:
Program:
2
u/tet5uo Nov 30 '13 edited Dec 01 '13
Okay. Here's my humble Javascript submission. This took me way longer than I'd care to admit. You should have seen it before my re-factoring haha.
This is the first time I used a iife to keep all my vars from spillin out everywhere :D
My main sorting logic is like one function, then the rest of that mess is me trying to figure out how to maniplulate the arrays and such to get the collumns into their own arrays for sorting and back. I have much to learn heh.
2
u/donaldxv Dec 03 '13
Just another straight forward Python:
2
u/apesate Dec 04 '13 edited Dec 04 '13
First post on : dailyprogrammer =) C #include <stdio.h> #include <stdbool.h>
2
u/LSatyreD Dec 06 '13
Edit: formatting noob. Also, first C++ program!
First Challenge and well, Whelp. I just stayed up all night working on this... only to realize I did it wrong. Oh well, enjoy, here is my eternally falling sand world, with water!
I will try to do this challenge the right way soon but may get caught up in adding things to my fuck-up. This isn't the most beautiful code but it runs:
2
u/RipIt_From_Space Dec 07 '13
Java.
Only my second post on this subreddit but I think my code is good, only problem is it could most likely be condensed a lot more.
}
2
u/GrowingCoder Dec 08 '13 edited Dec 08 '13
Solution in Scala: http://pastebin.com/KWr1H6X3
Guess I have too many lines....
Always open for suggestions :)
2
u/[deleted] Dec 09 '13
Written in Python.
Anybody willing to critique?
2
u/h3ckf1r3 Dec 10 '13
I'm honestly shocked at how difficult that turned out. I tried doing it a few different ways, at first I wanted to be all fancy and utilize OO techniques, but that tanked, then I tried another way, and after a rewrite I finally got it to work.
the code isn't very pretty, and there's no doubt a better way to do this, but after spending quite some time on an "easy" challenge, I have to say, enough is enough. So here it is:
EDIT: its ruby btw :)
2
u/Umbraco Dec 10 '13
Clojure.
Would be nice if strings could implicitly be treated as character lists...
2
u/jellyw00t Dec 14 '13
}
Only my second ever submission.. Feedback welcome! Java
2
u/danohuiginn Dec 15 '13
python, using regex, and going for conciseness over clarity:
2
u/minikomi Dec 16 '13
Racket with pattern matching
2
u/ReginaldIII Dec 22 '13
C++
2
u/kidinthecorner Dec 27 '13
I am new to programming, please suggest some improvements on my code.
Python 2.7
2
u/VerifiedMyEmail Dec 30 '13
javascript with html note that the output is on the console.
2
u/thegubble Jan 08 '14
C# First time submitting something :D This has the advantage of only simulating once. It reads in the grid, then goes column by column dropping any sand it finds above a space down as low as it can go starting from the bottom and working up.
It can handle no sand, lots of sand in the same column, massive grids with many stones per column and most common tricks :)
2
u/gamehelp16 Jan 11 '14
HTML + Javascript
2
u/pythagean Jan 17 '14
C#, its not very neat:
2
u/[deleted] Jan 23 '14
C++, but only for the <iostream> library. Everything else is technically just C. I set it up where it displays the simulation after each cycle/step. I don't really like my flag and do-while loop setup. Anyone know how I can implement a while loop and/or not have to reset my flag each cycle?
2
u/Devil777 Jan 28 '14
Java A little lengthy, i have add some animation to the falling sand :P
}
2
u/felix1429 Mar 08 '14 edited Mar 09 '14
Java:
2
u/bodiddlie Nov 25 '13
Python 2.7 - a little ugly, but it works.
1
u/SiNoEvol Feb 27 '14
Perl:
1
u/dohaqatar7 1 1 Mar 30 '14 edited Mar 30 '14
wrote something nice in java. This displays the state of the sand each after every fall, so I can see the progress.
I took the input from a file because it makes test cases so much easier. I also did it in a seperate method so that I could just use an array from inside the program.
Edit: added one line that I forgot
1
u/moshdixx Dec 08 '13
noob C++
0
u/pandubear 0 1 Nov 25 '13
Based on kirsybuu's idea, here's something that could be a Python one-liner (though there probably is a better way than transposing twice) if you inlined the functions:
Edit: Wait, I don't think you can inline that. urgh.