r/dailyprogrammer • u/Coder_d00d 1 3 • Jun 25 '14
[6/25/2014] Challenge #168 [Intermediate] Block Count, Length & Area
Description:
In construction there comes a need to compute the length and area of a jobsite. The areas and lengths computed are used by estimators to price out the cost to build that jobsite. If for example a jobsite was a building with a parking lot and had concrete walkways and some nice pavers and landscaping it would be good to know the areas of all these and some lengths (for concrete curbs, landscape headerboard, etc)
So for today's challenge we are going to automate the tedious process of calculating the length and area of aerial plans or photos.
ASCII Photo:
To keep this within our scope we have converted the plans into an ASCII picture. We have scaled the plans so 1 character is a square with dimensions of 10 ft x 10 ft.
The photo is case sensitive. so a "O" and "o" are 2 different blocks of areas to compute.
Blocks Counts, Lengths and Areas:
Some shorthand to follow:
- SF = square feet
- LF = linear feet
If you have the following picture.
####
OOOO
####
mmmmhas a block count of 2. we have 2 areas not joined made up of
- O and m have a block count of 1. they only have 1 areas each made up of their ASCII character.
- O has 4 blocks. Each block is 100 SF and so you have 400 SF of O.
- O has a circumference length of that 1 block count of 100 LF.
- m also has 4 blocks so there is 400 SF of m and circumference length of 100 LF
has 2 block counts each of 4. So # has a total area of 800 SF and a total circumference length of 200 LF.
Pay close attention to how "#" was handled. It was seen as being 2 areas made up of # but the final length and area adds them together even thou they not together. It recognizes the two areas by having a block count of 2 (2 non-joined areas made up of "#" characters) while the others only have a block count of 1.
Input:
Your input is a 2-D ASCII picture. The ASCII characters used are any non-whitespace characters.
Example:
####
@@oo
o*@!
****Output:
You give a Length and Area report of all the blocks.
Example: (using the example input)
Block Count, Length & Area Report
=================================
#: Total SF (400), Total Circumference LF (100) - Found 1 block
@: Total SF (300), Total Circumference LF (100) - Found 2 blocks
o: Total SF (300), Total Circumference LF (100) - Found 2 blocks
*: Total SF (500), Total Circumference LF (120) - Found 1 block
!: Total SF (100), Total Circumference LF (40) - Found 1 blockEasy Mode (optional):
Remove the need to compute the block count. Just focus on area and circumference length.
Challenge Input:
So we have a "B" building. It has a "D" driveway. "O" and "o" landscaping. "c" concrete walks. "p" pavers. "V" & "v" valley gutters. @ and T tree planting. Finally we have # as Asphalt Paving.
ooooooooooooooooooooooDDDDDooooooooooooooooooooooooooooo
ooooooooooooooooooooooDDDDDooooooooooooooooooooooooooooo
ooo##################o#####o#########################ooo
o@o##################o#####o#########################ooo
ooo##################o#####o#########################oTo
o@o##################################################ooo
ooo##################################################oTo
o@o############ccccccccccccccccccccccc###############ooo
pppppppppppppppcOOOOOOOOOOOOOOOOOOOOOc###############oTo
o@o############cOBBBBBBBBBBBBBBBBBBBOc###############ooo
ooo####V#######cOBBBBBBBBBBBBBBBBBBBOc###############oTo
o@o####V#######cOBBBBBBBBBBBBBBBBBBBOc###############ooo
ooo####V#######cOBBBBBBBBBBBBBBBBBBBOcpppppppppppppppppp
o@o####V#######cOBBBBBBBBBBBBBBBBBBBOc###############ooo
ooo####V#######cOBBBBBBBBBBBBBBBBBBBOc######v########oTo
o@o####V#######cOBBBBBBBBBBBBBBBBBBBOc######v########ooo
ooo####V#######cOOOOOOOOOOOOOOOOOOOOOc######v########oTo
o@o####V#######ccccccccccccccccccccccc######v########ooo
ooo####V#######ppppppppppppppppppppppp######v########oTo
o@o############ppppppppppppppppppppppp###############ooo
oooooooooooooooooooooooooooooooooooooooooooooooooooooooo
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooFAQ:
Diagonals do not connect. The small example shows this. The @ areas are 2 blocks and not 1 because of the Diagonal.
3
u/dohaqatar7 1 1 Jun 25 '14
You should clarify weather diagonals connection count as continuous blocks.
In this image, would there be one or two blocks of '@'?
Going by the sample, there should be two separate blocks, but you might want to clear up any confusion.
8
u/skeeto -9 8 Jun 25 '14 edited Jun 25 '14
C. It computes everything in a single x/y pass over the map, using disjoint forest sets to compute regions very efficiently. This data structure is an inverse tree, where leaves point to their parents and the root node identifies the set. The map dimensions are hardcoded in order to keep things simple.
Challenge output:
5
u/dohaqatar7 1 1 Jun 25 '14 edited Jun 25 '14
That was a nice fun challenge! Counting the circumference turned out to be the hardest part of it.
Java
BlockCount class
Tile class
TileStatistics Class
Challenge Output
3
u/BryghtShadow Jun 26 '14
Python 3.4
Uses disjoint-set forest. Maintains order on outputs values.
Output:
3
u/ryani Jun 26 '14 edited Jun 26 '14
I decided to challenge myself to solve the problem in constant space. This means no recursion or allocation of data structures. I could have made all my variables global to 'prove' it, but it's easy to verify the lack of recursion and there are no calls to memory-allocating functions.
To simplify the problem slightly, I allowed an extra character on the border of the map, to avoid some useless checks for border indices. I also didn't implement map loading because that didn't seem particularly interesting or challenging, just lots of vector/allocator munging. The assumption is that the map could be passed in from a caller. I did hardcode map width/height but that was just to get around VC++ storing string literals in read-only memory.
Implementation is in C, feedback welcome!
Lots of pointless cleverness in this solution, but that's the fun of writing C :) One particular 'cleverness': Since the problem specified 'ascii', I assume values >= 0x80 are unused, which lets me store a 'visited' bit in each cell. Can you think of a way to avoid this? I also store values 0-4 temporarily in cells during a depth-first-search, although those are guaranteed not to interact with the rest of the map and be restored before
fillexits.Thanks for the fun challenge!
1
u/toodim Jun 25 '14
It isn't clear how you calculate circumference length.
2
u/luxgladius 0 0 Jun 25 '14 edited Jun 25 '14
Perl
Output
2
u/Vectorious Jun 25 '14 edited Sep 18 '25
chubby salt angle chase butter violet wide expansion vast wild
This post was mass deleted and anonymized with Redact
2
u/gengisteve Jun 25 '14
Decided to try a different approach on this one. Python 3.3 without recursion:
2
u/[deleted] Jun 26 '14
Hey everyone, I decided to take a bit of a GUI approach to this challenge, let me know what you all think!
[Here's an image album] (http://imgur.com/a/VQv66) containing the challenge input and the the output from the program.
Also, [Here's a Google drive link] (https://drive.google.com/file/d/0B19zs_SgJDAtTmx3VW5vNUdkZTg/edit?usp=sharing) to the .jar file of the program, if you want to give it a try for yourself.
As for the code itself, I'm still a bit of a newbie at programming (I've only been doing it for a year) and this is my first post on this subreddit, so I am certain there are many things in it that I could have done more efficiently and effectively. Any help and friendly advice would be tremendously appreciated!!!
Code (JAVA):
Output (copied from the "Results" JTextArea):
3
u/Godspiral 3 3 Jun 25 '14
In J,
b =. > cutLF wd 'clippaste'
area =: (100 * [: +/"1 ] ="1 0 ~.)@:,
connections =: ([: ([: +/@:, 3 3 ([: +/ 1 3 5 7&{ * 4&{)@:,;._3 (0,0,~0,.0,.~])) =)"0 _
2nd one:
1
u/Godspiral 3 3 Jun 25 '14 edited Jun 25 '14
I don't understand how # is different than others for calculation of area and circumference.
edit: ok get it now. # is not special. Rather symbols that appear in multiple "islands" are seperate blocks. Diagonals don't count as connections.
1
u/jeaton Jun 25 '14
JavaScript:
1
u/Komorebi Jun 26 '14 edited Jun 26 '14
I'm new to Python from a C / Java background, and trying to learn how to do things the "Pythonic" way. Any feedback welcome.
Solution in Python 2.7:
One particular piece of feedback I'd like is how to maintain the datastructure for the symbols. In C I would make a struct and in Java I'd make a class. Here for Python I did sort of a dictionary. What's the best way here?
1
u/mortenaa Jun 26 '14
My solution in Dart. Figured out a way to do it where I just go through the array from the top left to the bottom right, line by line. I then for each position in the array look at the position above and to the left. Then the current position can be connected to the group above, the group to the left, or it is connecting two groups that should be joined.
I see that the results people have posted are different for some circumferences. For instance I get
while some people get
Would be interesting to see which is correct.
Test output:
1
u/viciu88 Jun 26 '14
Java. Using flood fill algorithm.
1
u/[deleted] Jun 29 '14
Java solution. A simple recursive Depth First Search approach where I group Cells into Blocks and add Edges between cells within a block. At the end for circumference I just use 4 sides per cell, minus the number of edges that have been created inside the block.
1
u/[deleted] Jun 29 '14 edited Mar 08 '24
Reddit Wants to Get Paid for Helping to Teach Big A.I. Systems The internet site has long been a forum for discussion on a huge variety of topics, and companies like Google and OpenAI have been using it in their A.I. projects. "The Reddit corpus of data is really valuable," Steve Huffman, founder and chief executive of Reddit, said in an interview. "But we don’t need to give all of that value to some of the largest companies in the world for free."
1
u/[deleted] Jul 24 '14 edited Jul 24 '14
First time doing one of these. Here is my solution in C#. When I solve problems like this, I prefer to use static methods for all of my functions and then just run my main method as a true "main" method.
EDIT: Just confirmed and this works for the Challenge input as well. :)
1
u/Gravicle Aug 08 '14
Here is an implementation in Swift as specced in Xcode 6 Beta 5. I am very new to this and very open for critique!
Runtime was 0.12s on a Mid-2014 2.2GHz i7 Retina MacBook Pro
Implementation: gist
1
u/toodim Jun 25 '14
Python 3.4.
Challenge Result
1
u/poeir Jun 25 '14 edited Jun 25 '14
Here is my Python 2.7 solution.
edit: Refactor out repeated code (get_neighbors), inspired by /u/toodim's solution.
0
u/[deleted] Jun 27 '14
[deleted] — view deleted comment