I hadn't thought about that! I'll take this as an opportunity to learn Rust!
I can't do it when the puzzles come out (too late in UK) but I've really enjoyed Rust so far and I got my 10 stars so far.
Results are really nice, and match and enums etc. really make the language nice.
Rust really has taken many of the best parts of Python, functional paradigms, and lower level languages, and the compiler errors - being warned about missing branches in matches! I'm hoping that finishing 2019 with Rust will lead to more in 2020!
I’m trying to solve them with Swift Playgrounds on the iPad, for example. And I can’t figure out how to create a simple text file for the data.
func calcFuel(_ mass:Int) -> Int {mass / 3 - 2}
let z0 = massList.map(calcFuel).reduce(0, +)
or simply ... let z1 = massList.map({$0 / 3 - 2}).reduce(0, +)
Any idea how to read the data on the iPad?Anyway, a complete Swift Playground on iPad solution:
import UIKit
let path = Bundle.main.url(forResource: "input01", withExtension: "txt")
var text = try String(contentsOf: path!, encoding: .utf8)
let massList:[Int] = text.components(separatedBy: .whitespacesAndNewlines).compactMap({Int($0)})
let z1 = massList.map({$0 / 3 - 2}).reduce(0, +)
print("\(z1)")This year, I’m learning Python by using a Raspberry Pi 4 directly connected to my iPad via USB. Should be another fun month!
let path = Bundle.main.url(forResource: "input01", withExtension: "txt")
var text = try String(contentsOf: path!, encoding: .utf8)Besides, there are immediate additional questions:
- what's a bundle, and why does a Bundle deal with urls?
- What's the difference between Bundle.main.url and FileManager.main.urls, and why one and not the other?
- where does the resource come from?
- how do I read a file from a path?
- Why is String responsible for reading (and writing!) contents of a file? In which world is this good design?
I don't believe that it required any compound data structures, just looping through the lines in the file.
You're right about the rest, though.
I have used http://www.https://cryptopals.com/ for a similar purpose, with the added benefit of learning about cryptography as a parallel outcome.