5 kyu

Stop the Zombie Apocalypse!

339 of 358zorza

Description:

Introduction

You are a number. But that's good for you since you live in the beautiful world, where the only living creatures are numbers. Unfortunately, a nasty zombie virus is spreading out in the digital cities. You work at the digital CDC and your job is to look over the city maps and tell which areas are contaminated by the zombie virus so the digital army would know where to drop the bombs. They are the new kind of digital zombies which can travel only in vertical and horizontal directions and infect only numbers same as them, but don't let it fool you, they're still very dangerous. Time is running out...

Task

You'll be given two-dimensional array with numbers in it. For some mysterious reason patient zero is always found in north west area of the city (element [0][0] of the matrix) and the plague spreads from there to other cells by moving left, right, up or down. You must create a function that returns a map (2-dimensional array) with all the contaminated areas marked as 1 and virus-free marked as 0.

In other words: find all the matrix elements with the same value as [0][0] that you can go to by moving only down, up, right or left from [0][0] - without going into a field storing any other value.

Examples


    city1 = [
        [7, 2, 3],
        [7, 2, 3],
        [1, 2, 7]
    ];
    
    contaminatedInCity1 = [
        [1, 0, 0],
        [1, 0, 0],
        [0, 0, 0]
    ]; // number 7 is a zombie, but the 7 in bottom right corner is not a zombie yet - the virus didn't get there.
    
    city2 = [
        [9, 1, 2],
        [9, 9, 9],
        [7, 4, 9],
        [7, 9, 7]
    ]; 
    
    contaminatedInCity2 = [
        [1, 0, 0],
        [1, 1, 1],
        [0, 0, 1],
        [0, 0, 0]
    ]; // infection inflicted the 9s, but the virus didn't get to the one in the south of the city yet.
    
Algorithms
Arrays
Matrix

More By Author:

Check out these other kata created by zorza

Stats:

CreatedNov 13, 2014
PublishedNov 13, 2014
Warriors Trained1308
Total Skips125
Total Code Submissions4780
Total Times Completed358
JavaScript Completions339
CoffeeScript Completions4
Python Completions24
Total Stars90
% of votes with a positive feedback rating96% of 78
Total "Very Satisfied" Votes72
Total "Somewhat Satisfied" Votes6
Total "Not Satisfied" Votes0
Total Rank Assessments10
Average Assessed Rank
4 kyu
Highest Assessed Rank
3 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • zorza Avatar
  • jhoffner Avatar
  • dfhwze Avatar
  • LosBlobbos Avatar
Ad