r/dailyprogrammer • 0 0 • Oct 26 '17

[2017-10-26] Challenge #337 [Intermediate] Scrambled images

Description

For this challenge you will get a couple of images containing a secret word, you will have to unscramble the images to be able to read the words.

To unscramble the images you will have to line up all non-gray scale pixels on each "row" of the image.

Formal Inputs & Outputs

You get a scrambled image, which you will have to unscramble to get the original image.

Input description

Challenge 1: input

Challenge 2: input

Challenge 3: input

Output description

You should post the correct images or words.

Notes/Hints

The colored pixels are red (#FF0000, rgb(255, 0, 0))

Bonus

Bonus: input

This image is scrambled both horizontally and vertically.
The colored pixels are a gradient from green to red ((255, 0, _), (254, 1, _), ..., (1, 254, _), (0, 255, _)).

Finally

Have a good challenge idea?

Consider submitting it to /r/dailyprogrammer_ideas

78 Upvotes

55 comments sorted by

View all comments

2

u/JakDrako Oct 26 '17

VB.Net in LinqPad, with bonus (auto detects if the image is scrambled both ways)

Sub Main

	Dim bmp = New Bitmap("scrambled_bonus.png")
	Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
	Dim bmpData As System.Drawing.Imaging.BitmapData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat)

	Dim stride = bmpData.Stride, height = bmpData.Height, length = stride * bmpData.Height
	Dim rgb(length - 1), tmp1(stride - 1), tmp2(stride - 1) As Byte

	Marshal.Copy(bmpData.Scan0, rgb, 0, length) ' copy bmp pixels to array

	Dim bonus = False
	For lines = 0 To height - 1

		Array.Copy(rgb, lines * stride, tmp1, 0, stride) ' copy bmp "line" to work buffer

		Dim marker = 0
		For x = 0 To stride - 1 Step 4 ' 4 bytes = 32 bits = ARGB. Order is actually BGRA
			If Not (tmp1(x) = tmp1(x + 1) AndAlso tmp1(x) = tmp1(x + 2)) Then ' pixel is grayscale
				marker = x + 3
				If Not (tmp1(x) = 0 AndAlso tmp1(x + 1) = 0) Then bonus = True ' rows are scrambled too
			Else
				If marker > 0 Then Exit For ' once we have the marker, we stop at the next grayscale pixel
			End If
		Next

		' Unscramble that line (marker will end up at the end of the line)
		Array.copy(tmp1, 0, tmp2, (stride - 1) - marker, marker + 1) ' copy 0-maxRed to end of line    
		Array.copy(tmp1, marker + 1, tmp2, 0, (stride - 1) - marker) ' copy rest to beginning of line

		' Recopy unscrambled line to bitmap
		Array.copy(tmp2, 0, rgb, lines * stride, stride)

	Next

	If bonus Then ' Sort the rows from green to red

		' Copy each row to list
		Dim lst = New List(Of Byte())
		For lines = 0 To height - 1
			Dim tmp(stride - 1) As Byte
			Array.Copy(rgb, lines * stride, tmp, 0, stride)
			lst.Add(tmp)
		Next

		' sort the list - red goes from 0 to 255
		lst.Sort(Function(a, b) a(stride - 2).CompareTo(b(stride - 2))) ' compare red value of last pixel

		' put the list back into the bmp
		Dim ln = 0
		For Each ba In lst
			Array.copy(ba, 0, rgb, ln * stride, stride)
			ln += 1
		Next

	End If

	Marshal.Copy(rgb, 0, bmpData.Scan0, length) ' put rgb values back in bitmap
	bmp.UnlockBits(bmpData)

	bmp.Dump("Unscrambled bitmap")

End Sub