tonetheman's blog

working through advent of code with nim (so far)

The repo is here:

I am certain I will run into a problem that will stump me soon.

Up until yesterday I was using this type of code to read a file line by line in nim.

    let inf = open("input.txt")
    defer: inf.close()

    var total_priority = 0
    var line : string
    while inf.read_line(line):
      # do something with line here

I looked for better official examples but came up with two different things and neither really satisfied me. But this is what I went with.

    for line in lines("input.txt"):
        let data = line.split(",")

I really like the Pythonic way of opening an input file and defering a close. Perhaps I like that because it is what I learned many years ago in C or matches it closely.