janAkali

joined 2 years ago
[–] janAkali@lemmy.one 1 points 2 years ago (4 children)

Treated each range as an object, and used set operations on them

That's smart. Honestly, I don't understand how it works. πŸ˜…

The difference operation tends to fragment the range that it’s used on, so I meant to write some code to defragment the ranges after each round of mappings. Forgot to do so, but the code ran quick enough this time anyway.

I've got different solution from yours, but this part is the same, lol. My code slices the ranges into 1-3 parts on each step, so I also planned to 'defragment' them. But performance is plenty without this step, ~450 microseconds for both parts on my PC.

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

Nim

Part 1 was really easy.
Part 2, I struggled to solve efficiently, so I just ran naive bruteforce for 5 minutes until I got the answer.
Later, I've rewritten my solution for Part 2. The idea is to handle ranges as ranges, check seed ranges against map ranges, transform overlaps, but keep not-overlapping parts.

Total runtime after rewrite: ~ 0.4 ms.
Today's puzzle was nice - 8.5/10.

Code: day_05/solution.nim

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

LANGUAGE: Nim

Welcome to the advent of parsing!
Took me a lot more time than it should (Please, don't check prior commits πŸ˜…).

day_04.nim

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

That was not fun to solve. Lots of ugly code. This is a less ugly second version. Language: Nim.

day_03.nim

[–] janAkali@lemmy.one 2 points 2 years ago (2 children)

Is that something american?

[–] janAkali@lemmy.one 4 points 2 years ago (6 children)

"We The People" - definitely something humans would write.

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

Have you joined the community?

Yep, but it is a bit quiet in there.

Good solution. I like your parsing with scanf. The only reason I didn't use it myself - is that I found out about std/strscans literally yesterday.

[–] janAkali@lemmy.one 3 points 2 years ago* (last edited 2 years ago) (4 children)

A solution in Nim language. Pretty straightforward code. Most logic is just parsing input + a bit of functional utils: allIt checks if all items in a list within limits to check if game is possible and mapIt collects red, green, blue cubes from each set of game.

https://codeberg.org/Archargelod/aoc23-nim/src/branch/master/day_02/solution.nim

import std/[strutils, strformat, sequtils]

type AOCSolution[T] = tuple[part1: T, part2: T]

type
  GameSet = object
    red, green, blue: int
  Game = object
    id: int
    sets: seq[GameSet]

const MaxSet = GameSet(red: 12, green: 13, blue: 14)

func parseGame(input: string): Game =
  result.id = input.split({':', ' '})[1].parseInt()
  let sets = input.split(": ")[1].split("; ").mapIt(it.split(", "))
  for gSet in sets:
    var gs = GameSet()
    for pair in gSet:
      let
        pair = pair.split()
        cCount = pair[0].parseInt
        cName = pair[1]

      case cName:
      of "red":
        gs.red = cCount
      of "green":
        gs.green = cCount
      of "blue":
        gs.blue = cCount

    result.sets.add gs

func isPossible(g: Game): bool =
  g.sets.allIt(
    it.red <= MaxSet.red and
    it.green <= MaxSet.green and
    it.blue <= MaxSet.blue
  )


func solve(lines: seq[string]): AOCSolution[int]=
  for line in lines:
    let game = line.parseGame()

    block p1:
      if game.isPossible():
        result.part1 += game.id

    block p2:
      let
        minRed = game.sets.mapIt(it.red).max()
        minGreen = game.sets.mapIt(it.green).max()
        minBlue = game.sets.mapIt(it.blue).max()

      result.part2 += minRed * minGreen * minBlue


when isMainModule:
  let input = readFile("./input.txt").strip()
  let (part1, part2) = solve(input.splitLines())

  echo &"Part 1: The sum of valid game IDs equals {part1}."
  echo &"Part 2: The sum of the sets' powers equals {part2}."
[–] janAkali@lemmy.one 11 points 2 years ago* (last edited 2 years ago) (2 children)

There is so much that I would want to do that requires time to be "flowing"..

If we're talking about physics-accurate superpowers, please add partial blindness - photons are frozen in place, they can't reach your eyelids, unless you walk into them. And suffocation due to completely still air.

And ... now you can't even nap in peace 😐.

[–] janAkali@lemmy.one 24 points 2 years ago* (last edited 2 years ago) (1 children)

Iirc, you can't log into Mastadon with a lemmy account, but there should be a way to follow a mastodon account from a lemmy instance. Never tried it, though.

Edit: Apparently, it's not possible either. Mastodon users can follow lemmy communities, but not the other way around. πŸ˜”

[–] janAkali@lemmy.one 19 points 2 years ago* (last edited 2 years ago) (4 children)

There've been protests, riots, violent acts of protest at draft centers. This just doesn't get as much coverage as Putin's or US propaganda.

It's not that masses not disgruntled enough. It's just almost nothing people can do to stop the war. Would you do something stupid and worthless, when even a social media post can and will cost you portion of your life in prison?

[–] janAkali@lemmy.one 9 points 2 years ago* (last edited 2 years ago) (5 children)

How much their ad-free tier costs? Can I pay without them tracking me? No? Then fuck you (website owners), I will be freeloading and will advertise freeloading.

Btw, use uBlock origin on Firefox, I haven't seen one of these annoying screens in a while.

Edit: uh oh

What type of information we collect? (iv)Identifiers and Precise Location: we may collect certain identifiers such as your IP addresses, and precise location solely through the mobile app in the event you have consented to provide us with your location.

Company may also share personally identifiable information with companies or organizations connected, or affiliated with Company, such as subsidiaries, sister-companies and parent companies, and other partners, with the express provision that their use of such information must comply with this policy.

Do not track disclosure Our Service does not respond to Do Not Track signals. For more information about Do Not Track signals, please see ...

view more: β€Ή prev next β€Ί