AdventOfCode in 42


Hi, I’m trying to complete all the tasks on advent of code (https://adventofcode.com/) with 42 (https://L42.is)

The first challenge is solved as follows:

reuse [L42.is/AdamsTowel]
Fs = Load:{reuse[L42.is/FileSystem]}
Main = (
  fs = Fs.Real.#$of()
  input = fs.read("input")
  var last = Math.maxI()
  res = Match.Count()(for s in input.split(S.nl()) (
    e = I(string=s)
    add(e>last)
    last:=e 
    ))
  Debug(res)
  )

The same code, with line by line comments:

//you should always know where your towel is
reuse [L42.is/AdamsTowel]

//importing the file system library; based on object capabilities
Fs = Load:{reuse[L42.is/FileSystem]}

Main = (
  //creating an object connected with the real file system
  fs = Fs.Real.#$of()

  //read the file as a string  
  input = fs.read("input")

  //we need to check when the dept grows, but not count
  //the first reading as growing from no-info
  var last = Math.maxI()

  //Here we use a string spliterator and Count
  //we split the input on newlines
  res = Match.Count()(for s in input.split(S.nl()) (
    e = I(string=s)//convert string->int32
    add(e>last)//if e>last, it counts!
    last:=e //update last
    ))
  Debug(res)//the result!
  )

Source: DEV Community

December 2, 2021
Category : News
Tags: 42 | adventofcode | programming

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.