janAkali

joined 2 years ago
[–] [email protected] 6 points 6 months ago* (last edited 6 months ago) (1 children)

Nim

Solution: sort numbers using custom rules and compare if sorted == original. Part 2 is trivial.
Runtime for both parts: 1.05 ms

proc parseRules(input: string): Table[int, seq[int]] =
  for line in input.splitLines():
    let pair = line.split('|')
    let (a, b) = (pair[0].parseInt, pair[1].parseInt)
    discard result.hasKeyOrPut(a, newSeq[int]())
    result[a].add b

proc solve(input: string): AOCSolution[int, int] =
  let chunks = input.split("\n\n")
  let later = parseRules(chunks[0])
  for line in chunks[1].splitLines():
    let numbers = line.split(',').map(parseInt)
    let sorted = numbers.sorted(cmp =
      proc(a,b: int): int =
        if a in later and b in later[a]: -1
        elif b in later and a in later[b]: 1
        else: 0
    )
    if numbers == sorted:
      result.part1 += numbers[numbers.len div 2]
    else:
      result.part2 += sorted[sorted.len div 2]

Codeberg repo

[–] [email protected] 4 points 6 months ago* (last edited 6 months ago)

Nim

Could be done more elegantly, but I haven’t bothered yet.

proc solve(input: string): AOCSolution[int, int] =
  var lines = input.splitLines()

  block p1:
    # horiz
    for line in lines:
      for i in 0..line.high-3:
        if line[i..i+3] in ["XMAS", "SAMX"]:
          inc result.part1

    for y in 0..lines.high-3:
      #vert
      for x in 0..lines[0].high:
        let word = collect(for y in y..y+3: lines[y][x])
        if word in [@"XMAS", @"SAMX"]:
          inc result.part1

      #diag \
      for x in 0..lines[0].high-3:
        let word = collect(for d in 0..3: lines[y+d][x+d])
        if word in [@"XMAS", @"SAMX"]:
          inc result.part1

      #diag /
      for x in 3..lines[0].high:
        let word = collect(for d in 0..3: lines[y+d][x-d])
        if word in [@"XMAS", @"SAMX"]:
          inc result.part1

  block p2:
    for y in 0..lines.high-2:
      for x in 0..lines[0].high-2:
        let diagNW = collect(for d in 0..2: lines[y+d][x+d])
        let diagNE = collect(for d in 0..2: lines[y+d][x+2-d])
        if diagNW in [@"MAS", @"SAM"] and diagNE in [@"MAS", @"SAM"]:
          inc result.part2

Codeberg repo

[–] [email protected] 5 points 6 months ago* (last edited 6 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

[–] [email protected] 1 points 6 months ago

Nim, because it's fast and expressive.

[–] [email protected] 2 points 6 months ago* (last edited 6 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.

[–] [email protected] 4 points 6 months ago* (last edited 6 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

[–] [email protected] 12 points 6 months ago (3 children)

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

[–] [email protected] 12 points 7 months ago

I remember seeing lemmy maybe 4+ years ago on some open-source subreddit. It had practically non-existent user base, so I've ignored it. After that, I remember a first wave of people making mastodon accounts (even before elon). There I've first heard of concept of "fediverse". I liked the idea but I honestly thought it had zero chances to compete with mainstream social media.

And then everything turned to shit, making a gap between something like lemmy and reddit a lot smaller. So I've jumped the ship with everyone after the API shitstorm.

[–] [email protected] 4 points 7 months ago* (last edited 7 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

[–] [email protected] 4 points 8 months ago

Oh, and GBA rom is included with game files.

[–] [email protected] 6 points 8 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.

[–] [email protected] 2 points 8 months ago

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

view more: β€Ή prev next β€Ί