r/dailyprogrammer • u/jnazario 2 0 • Nov 02 '16
[2016-11-02] Challenge #290 [Intermediate] Blinking LEDs
Description
Mark saw someone doing experiments with blinking LEDs (imagine something like this ) and became fascinated by it. He wants to know more about it. He knows you are good with computers, so he comes to you asking if you can teach him how it works. You agree, but as you don't have any LEDs with you at the moment, you suggest: "Let's build an emulator with which we can see what's happening inside". And that's today's challenge.
1st Part
The 1st part should be easy, even though the description is rather verbose. If you want more challenge try the 2nd part afterwards.
Our system has 8 LEDs, we represent their state with a text output. When all LEDs are off, it is printed as string of eight dots "........". When a led is on, it is printed as "*". LED-0 is on the right side (least significant bit), LED-7 is on the left side. Having LEDs 0 and 1 on and all others off is written as "......**"
On input you get a sequence of lines forming a program. Read all lines of the input (detect EOF, or make the first line contain number of lines that follow, whichever is more convenient for you). Afterwards, print LED states as they are whenever the program performs an out instruction.
Each line is in the following format:
<line>: <whitespace> <instruction> |
<empty>
<instruction> : ld a,<num> |
out (0),a
<whitespace> is one or more of characters " " or "\t". <num> is a number between 0 and 255.
Instruction ld a,<num> sets internal 8-bit register A to the given number. Instruction out (0),a updates the LEDs according to the current number in A. The LED-0's state corresponds to bit 0 of number in A, when that number is represented in binary. For example, when A = 5, the LED state after out instruction is ".....*.*".
You should output the LED states after each out instruction.
Challenge input 1:
ld a,14
out (0),a
ld a,12
out (0),a
ld a,8
out (0),a
out (0),a
ld a,12
out (0),a
ld a,14
out (0),a
Expected output:
....***.
....**..
....*...
....*...
....**..
....***.
2nd Part
We will extend our programming language, so that we can do more updates without writing out instruction for each of them. We will have loops.
Each line has the following format:
<line>: <whitespace> <instruction> |
<label> |
<empty>
<instruction> : ld a,<num> |
ld b,<num> |
out (0),a |
rlca |
rrca |
djnz <labelref>
<label> is a sequence of characters a-z A-Z _ terminated with one character ":". <labelref> is a sequence of characters a-z A-Z _ (it corresponds to some label minus the trailing ":").
Instruction ld b,<num> sets a number to register B. Instruction rlca rotates bits in register A one position to the left, in circle (i.e. bit 0 goes to bit 1, bit 1 to bit 2, and bit 7 to bit 0). Instruction rrca rotates bits in register A one position to the right, in circle. Instruction djnz <labelref> (decrement and jump if not zero) subtracts one from the value of register B and if the new value of register B is not zero then the processing of instructions continues at the line containg label corresponding to the <labelref>. You can assume that in the input text <label> is always given before the corresponding <labelref> (i.e. jumps go backwards).
You should output the LED states after each out instruction.
Challenge Input 2:
ld b,3
triple:
ld a,126
out (0),a
ld a,60
out (0),a
ld a,24
out (0),a
djnz triple
Challenge Output 2:
.******.
..****..
...**...
.******.
..****..
...**...
.******.
..****..
...**...
Challenge Input 3:
ld a,1
ld b,9
loop:
out (0),a
rlca
djnz loop
Challenge Output 3:
.......*
......*.
.....*..
....*...
...*....
..*.....
.*......
*.......
.......*
Challenge Input 4:
ld a,2
ld b,9
loop:
out (0),a
rrca
djnz loop
Challenge Output 4:
......*.
.......*
*.......
.*......
..*.....
...*....
....*...
.....*..
......*.
Credit
This challenge was suggested by /u/lukz in /r/dailyprogrammer_ideas, many thanks! If you have a challenge idea please share it and there's a good chance we'll use it.
2
u/IceDane 0 0 Nov 03 '16
Haskell
Should support forward jumps since it does two passes. One to collect all the labels and one pass to resolve them into absolute "addresses" in the CPU.
10
u/skeeto -9 8 Nov 02 '16
C. It loads the program into a sort of bytecode representation with the labels resolved to PC-relative offsets. The parser is the bulk of the program and the interpreter itself is only 30 lines.
7
u/primaryobjects Nov 07 '16
Javascript
I went overboard and created a full ReactJs web app out of this challenge. :) Little css light bulbs light-up in animated fashion, as the program runs. Cute!
Demo | GitHub
3
u/eMkaQQ Nov 03 '16
part 1 is pretty easy with PL/SQL if we use table for inputs and cursor to fetch it
I will try to expand it to part 2. As a begginer I'm open to any sugestions.
3
u/nevec71 Nov 04 '16
C#. I only just started learning C#, any feedback is welcome.
2
u/taindissa_work Nov 02 '16
This is a really cool introduction to assembly and low level memory storage.
2
u/[deleted] Nov 02 '16
Crystal/Ruby, 1st part:
https://play.crystal-lang.org/#/r/1d8a
2
u/Vectorious Nov 03 '16 edited Sep 18 '25
skirt butter fade retire trees zephyr stupendous pet support unite
This post was mass deleted and anonymized with Redact
2
u/smokeyrobot Nov 04 '16 edited Nov 04 '16
Here is my Java version. I don't liek the String parsing that I did for int -> bit strings but I didn't know anything elegant in Java that would help me. I tried BitSets but management was still a pain. My solution would also easily extend to more registers and more instructions with very little to no code change. Any suggestions would be great.
2
u/itsme86 Nov 07 '16 edited Nov 07 '16
C#
I might have over-engineered this a bit. Works for all challenges and even supports forward jumps too, so yay! Compiles the program into an intermediary bytecode that's used for executing the program.
1
u/adrian17 1 4 Nov 02 '16 edited Nov 05 '16
Quick Python with decorators and regex-based matching.
Edit: just noticed that it's impossible to jump forward with the current approach (since the labels are read at the time of execution) :/
1
u/Daanvdk 1 0 Nov 02 '16
Python 3: Loads the program from stdin as it goes so in theory should be able to execute infinitely long programs and in practice be able to execute your program directly while you're typing it in the command line. Also checks the input for errors and gives detailed error information when an error occurs.
1
u/[deleted] Nov 02 '16
Crystal, 2nd part:
https://play.crystal-lang.org/#/r/1d8b
1
u/marchelzo Nov 03 '16 edited Nov 03 '16
Ty
1
u/Tetsumi- 1 0 Nov 03 '16
Racket, parts 1 and 2.
1
u/pie__flavor Nov 03 '16
Scala:
Going for small code instead of sanity here, as you can see from my rlca and rrca implementations.
1
u/ASpueW Nov 03 '16 edited Nov 03 '16
Rust
Test:
1
u/Specter_Terrasbane Nov 03 '16
Python 2, both parts
1
u/den510 Nov 03 '16
Python 3, all challenges. This was a fun, but I hate Python's lack of case handling like Java has.
+/u/CompileBot Python3
1
u/thtoeo Nov 03 '16
C#. Created little variation which allows using more variables and using commands on all variables. Allows also to use loop inside loop.
Commands:
Example 1 (loop with rotate):
Example 2 (loop inside loop):
Code:
1
u/Minolwa Nov 03 '16
Functional Python3 w/ Closures
Command line input is the file to be interpreted
1
u/Scroph 0 0 Nov 03 '16
D (dlang) solution. Most of the work is done inside the Cpu structure. Throws if it runs into an unknown instruction or an undefined label :
1
u/Haizan Nov 04 '16
C. I wanted an excuse to play around with some code generation. So this code builds a function from machine code and executes that.
Code is here it got a bit long for a comment.
This supports different sizes for the LED array (8/16/32) and has an alternate print function. Instead of printing ever "out" on a separate line it animates the changes on one line.
1
u/altorelievo Nov 05 '16 edited Nov 05 '16
Recursive method with Python
1
u/_HyDrAg_ Nov 11 '16 edited Nov 11 '16
Python 3. The idea of using regex and decorators comes from /u/adrian17. Currently reads labels on init. It could be easily edited to read them when it's interpreting, but it would lose the ability to jump forwards. Because it reads labels in advance, each label name should be unique.
1
u/demreddit Nov 11 '16 edited Nov 12 '16
Python 3.5. I built a little menu based program that includes some basic options like viewing the current led program and repeating it if desired, along with of course creating a new one. Sorry, no built in editing! I have no doubt this program is very breakable, but I got the challenge outputs to work and I'm extremely happy for that! Any comments or criticisms are welcome and appreciated. :)
1
u/marcelo_rocha Nov 13 '16
Dart run it in Dartpad
1
u/[deleted] Nov 16 '16 edited Nov 16 '16
Python. Works if there are no unlabeled instructions after the first unlabeled block which the challenge inputs contain.
Input filename and recursion depth are acquired via command line arguments.
1
u/Rasaford Nov 26 '16
Java Had some seriously hard to find bugs on this one. Nevertheless here it is:
1
u/[deleted] Dec 01 '16
Go
Feedback always welcome. This was trickier than expected, and I'm sure it's more fragile than I'd like - but all challenge cases pass as specified!
1
u/JusticeMitchTheJust Jan 20 '17
Very late to the party, but here's a Java 8 solution
+/u/CompileBot java
Input:
1
u/dpforyou Jan 21 '17
C# with TDD and like, functional tables or something:
0
u/[deleted] Nov 03 '16
[deleted] — view deleted comment