Session 1 homework ================== Across the early weeks, problem 1 will just be a warmup using the things learned in that week's session, stretching you a little by being not the most complete of problem descriptions. However problem 2 will be building up week on week, so while your solutions to problem 1 can be throw away, you'll be able to think ahead with problem 2, but don't work ahead or you might skip important steps as we move through the weeks. At the end of session one we had covered hopefully enough about IO that you can easily load the input data below, but if you need reminding, please recheck what was recommended in the session. Your answers to the homework questions should be cargo binary projects which function as per the instructions. To submit your homework, please ensure that you have deleted the `target` directory (where cargo puts its build artifacts) before you tar it up. Submissions are due on the morning of the day before the next session. Problem 1: Adding up numbers ---------------------------- You have an input file whose content is as follows (ignoring the obvious start/end markers and the indentation, i.e. the first line is '14' and the last is '46'): ---8<--- 14 9 7 1 -5 0 -2 9 1 -100 75623 -65536 -100 5 2 100 46 ---8<--- Your goal is to write a program which when run as follows: $ cargo run -- inputfilename will read the contents of `inputfilename`, break it into lines, parse each line as a number of some useful type, sum all the numbers, and print the sum to stdout. If any of the lines does not parse as a suitable number, please just return the error out of your main function, using the techniques we learned in the 10 green bottles example. Bonus points if you make interesting use of `Iterator` methods. Note: You should test your program with good and bad inputs, and make sure that it reports errors as you'd expect. Problem 2 - In for the long haul -------------------------------- You now have an input file with the following content: ---8<--- This is your input file Anything which is not one of the important characters will be ignored by the program The program is as follows: +[- [<< [+ [--->] -[<<<] ] ]>>>- ] I'm guessing that you know where we're going with all this >-.---.>..>.<<<<-.<+.>>>>>.>.<<.<-. But perhaps you're as lost as a little lamb? ---8<--- As you can no doubt tell, this is a program in the [Brainfuck][] language. For now, we're not going to be writing anything as complex as a Brainfuck interpreter, though we will be building one in the future so bear that in mind as you write today's homework answer. [Brainfuck]: https://en.wikipedia.org/wiki/Brainfuck For today, please write a program which, when run as: $ cargo run -- inputfile will read `inputfile`, reduce it to the list of valid Brainfuck characters (i.e. strip out all of the comment characters) and then print out the program. There are eight valid command characters, and they're listed in the Wikipedia article. You can do this entirely with things we've already considered, by means of iterators you can find on `BufRead` and on `str`, and with judicious use of `match` and `print!()/println!()` However, for bonus points, make use of `Vec`, `Vec` or `String` and learn how to use `collect()` from the `Iterator` trait. Note: You'll want to get hold of as many different valid Brainfuck inputs as you can to try it out.