janAkali

joined 2 years ago
[–] janAkali@lemmy.one 5 points 8 months ago* (last edited 8 months ago) (1 children)

Nim

From a first glance it was obviously a regex problem.
I'm using tinyre here instead of stdlib re library just because I'm more familiar with it.

import pkg/tinyre

proc solve(input: string): AOCSolution[int, int] =
  var allow = true
  for match in input.match(reG"mul\(\d+,\d+\)|do\(\)|don't\(\)"):
    if match == "do()": allow = true
    elif match == "don't()": allow = false
    else:
      let pair = match[4..^2].split(',')
      let mult = pair[0].parseInt * pair[1].parseInt
      result.part1 += mult
      if allow: result.part2 += mult

Codeberg repo

[–] janAkali@lemmy.one 1 points 8 months ago

Nim, because it's fast and expressive.

[–] janAkali@lemmy.one 2 points 8 months ago* (last edited 8 months ago)

Cool to see another solution in Nim here =)

(0..<record.len).anyIt(record.dup(delete(it)).validate)

That's smart. I haven't thought of using iterators to loop over indexes (except in a for loop).

I got stuck on part 2 trying to check everything inside a single loop, which kept getting more ugly.

Yeah I've thought of simple ways to do this and found none. And looking at the input - it's too easy to bruteforce, especially in compiled lang like Nim.

[–] janAkali@lemmy.one 3 points 8 months ago* (last edited 8 months ago)

Nim

Got correct answer for part 1 on first try, but website rejected it. Wasted some time debugging and trying different methods. Only to have the same answer accepted minutes later. =(

proc isSafe(report: seq[int]): bool =
  let diffs = collect:
    for i, n in report.toOpenArray(1, report.high): n - report[i]
  (diffs.allIt(it > 0) or diffs.allIt(it < 0)) and diffs.allIt(it.abs in 1..3)

proc solve(input: string): AOCSolution[int, int] =
  let lines = input.splitLines()
  var reports: seq[seq[int]]
  for line in lines:
    reports.add line.split(' ').map(parseInt)

  for report in reports:
    if report.isSafe():
      inc result.part1
      inc result.part2
    else:
      for t in 0..report.high:
        var mReport = report
        mReport.delete t
        if mReport.isSafe():
          inc result.part2
          break

Codeberg repo

[–] janAkali@lemmy.one 12 points 8 months ago (3 children)

Hey, why isn’t China more green? I’d think the CCP hates western spyware OSes.

[–] janAkali@lemmy.one 4 points 8 months ago* (last edited 8 months ago)

Nim

I've got my first sub-1000 rank today (998 for part 2). Yay!
Simple and straightforward challenge, very fitting for 1st day. Gonna enjoy it while it lasts.

proc solve(input: string): AOCSolution[int, int] =
  var l1,l2: seq[int]
  for line in input.splitLines():
    let pair = line.splitWhitespace()
    l1.add parseInt(pair[0])
    l2.add parseInt(pair[1])
  l1.sort()
  l2.sort()

  block p1:
    for i in 0..l1.high:
      result.part1 += abs(l1[i] - l2[i])

  block p2:
    for n in l1:
      result.part2 += n * l2.count(n)

Codeberg repo

[–] janAkali@lemmy.one 4 points 9 months ago

Oh, and GBA rom is included with game files.

[–] janAkali@lemmy.one 6 points 9 months ago (1 children)

I believe for many companies, developers work on giant codebases with many hundred thousands or even millions of lines of code.

With such large codebase you have no control over any system. Because control is split between groups of devs.

If you want to refactor a single subsystem it would take coordination of all groups working on that part and will halt development, probably for months. But first you have to convince all the management people, that refactor is needed, that on itself could take eternity.

So instead you patch it on your end and call it a day.

[–] janAkali@lemmy.one 2 points 9 months ago

I believe they both died after the Mountain and the Smaug part, so not really relevant.

[–] janAkali@lemmy.one 116 points 9 months ago (8 children)

dragon attack survivor

That's one way to describe a burglar who came with a group to kill and plunder. And succeeded in both without major troubles.

[–] janAkali@lemmy.one 2 points 10 months ago (1 children)

No one tell them that they can monopolize solar panels.

[–] janAkali@lemmy.one 1 points 10 months ago* (last edited 10 months ago)

Meanwhile Nim:

echo "I am still worthy"
let a = r"I hate the ugly '\' at the end of " &
         "multiline statements"
for x in 0..9:
  if x == 6: echo x

echo x # this is error in Nim, but not in python. Insane!
assert false + 1 # this is an error (python devs in shambles)
assert true - 1 # see above

Thanks for coming to my Ted-talk.
More here: Nim for Python Programmers

view more: β€Ή prev next β€Ί