h4x0r

joined 1 year ago
[โ€“] h4x0r@lemmy.dbzer0.com 8 points 1 day ago (4 children)

No sed necessary with extglob enabled:

echo ${line//@(TH|[EL ]|DO)/}
[โ€“] h4x0r@lemmy.dbzer0.com 16 points 2 days ago (1 children)

it's missing strong typing

Python is strongly typed, but not statically typed.

[โ€“] h4x0r@lemmy.dbzer0.com 3 points 2 weeks ago* (last edited 2 weeks ago) (1 children)

c

#include "aoc.h"
#include <stdio.h>
#include <string.h>

constexpr usize LINE_BUFSZ = (1 << 7);
constexpr u64 TEN = 10;
constexpr u64 TWELVE = 12;

static void
joltage(strc line, u64* total, usize on) {
  usize len = strlen(line);
  usize off = len - on;
  usize slen = 0;
  c8 stack[LINE_BUFSZ] = {};
  for (usize i = 0; i < len; i++) {
    while (slen > 0 && off > 0 && stack[slen - 1] < line[i]) {
      slen--;
      off--;
    }
    stack[slen++] = line[i];
  }
  u64 jltg = 0;
  for (usize i = 0; i < on; i++) {
    jltg = (jltg * TEN) + (u64)(stack[i] - '0');
  }
  *total += jltg;
}

static void
solve(u64 on) {
  FILE* input = fopen("input", "r");
  c8 line[LINE_BUFSZ] = {};
  u64 total = 0;
  while (fgets(line, sizeof(line), input)) {
    line[strcspn(line, "\n")] = 0;
    joltage(line, &total, on);
  }
  fclose(input);
  printf("%lu\n", total);
}

i32
main(void) {
  solve(2);
  solve(TWELVE);
}
[โ€“] h4x0r@lemmy.dbzer0.com 3 points 2 weeks ago

One last time with threads.

p1 isolated: 12ms

p2 isolated: 13ms

combined run: 25ms

typedef struct job_s {
  Mode mode;
  Point rng;
  usize total;
} Job;

static void*
worker(void* a) {
  Job* job = a;
  (job->mode == MODE_ONE) ? one(job->rng, &job->total)
                          : two(job->rng, &job->total);
  return nullptr;
}

static void
solve(Mode mode) {
  FILE* input = fopen("input", "r");
  c8 line[LINE_BUFSZ] = {};
  fgets(line, sizeof(line), input);
  line[strcspn(line, "\n")] = 0;
  fclose(input);
  usize nrng = 1;
  for (strc s = line; *s; s++) {
    if (*s == ',') {
      nrng++;
    }
  }
  Job* jobs = calloc(nrng, sizeof(*jobs));
  pthread_t* threads = calloc(nrng, sizeof(*threads));
  usize idx = 0;
  strc tok = strtok(line, ",");
  while (tok) {
    sscanf(tok, "%zu-%zu", &jobs[idx].rng.x, &jobs[idx].rng.y);
    jobs[idx].mode = mode;
    tok = strtok(nullptr, ",");
    idx++;
  }
  for (usize i = 0; i < nrng; i++) {
    pthread_create(&threads[i], nullptr, worker, &jobs[i]);
  }
  usize total = 0;
  for (usize i = 0; i < nrng; i++) {
    pthread_join(threads[i], nullptr);
    total += jobs[i].total;
  }
  free(threads);
  free(jobs);
  printf("%zu\n", total);
}

i32
main(void) {
  solve(MODE_ONE);
  solve(MODE_TWO);
}
[โ€“] h4x0r@lemmy.dbzer0.com 3 points 2 weeks ago

Circling back around to go faster.

p1 isolated: 76ms

p2 isolated: 82ms

combined run: 156ms

static void
one(Point rng, usize* total) {
  for (usize i = rng.x; i <= rng.y; i++) {
    c8 pid[PID_BUFSZ] = {};
    snprintf(pid, sizeof(pid), "%zu", i);
    usize len = strlen(pid);
    if (len % 2 != 0) {
      continue;
    }
    usize hlen = len / 2;
    c8 a[PID_BUFSZ] = {};
    memcpy(a, pid, hlen);
    c8 b[PID_BUFSZ] = {};
    memcpy(b, pid + hlen, hlen);
    if (strcmp(a, b) == 0) {
      *total += i;
    }
  }
}

static void
two(Point rng, usize* total) {
  for (usize i = rng.x; i <= rng.y; i++) {
    c8 pid[PID_BUFSZ] = {};
    snprintf(pid, sizeof(pid), "%zu", i);
    usize len = strlen(pid);
    for (usize j = 1; j <= len / 2; j++) {
      if (len % j != 0) {
        continue;
      }
      bool valid = true;
      for (usize k = j; k < len; k++) {
        if (pid[k] != pid[k - j]) {
          valid = false;
          break;
        }
      }
      if (valid) {
        *total += i;
        break;
      }
    }
  }
}

static void
solve(Mode mode) {
  FILE* input = fopen("input", "r");
  c8 line[LINE_BUFSZ] = {};
  fgets(line, sizeof(line), input);
  line[strcspn(line, "\n")] = 0;
  usize total = 0;
  strc tok = strtok(line, ",");
  while (tok) {
    Point rng = {};
    sscanf(tok, "%zu-%zu", &rng.x, &rng.y);
    (mode == MODE_ONE ? one : two)(rng, &total);
    tok = strtok(nullptr, ",");
  }
  fclose(input);
  printf("%zu\n", total);
}

i32
main(void) {
  solve(MODE_ONE);
  solve(MODE_TWO);
}
[โ€“] h4x0r@lemmy.dbzer0.com 4 points 2 weeks ago* (last edited 2 weeks ago) (3 children)

c

#include "aoc.h"
#include <stdio.h>
#include <string.h>

constexpr usize LINE_BUFSZ = (1 << 9);
constexpr usize PID_BUFSZ = (1 << 4);

static void
solve(strl re) {
  FILE* input = fopen("input", "r");
  c8 line[LINE_BUFSZ] = {};
  fgets(line, sizeof(line), input);
  line[strcspn(line, "\n")] = 0;
  usize total = 0;
  strc tok = strtok(line, ",");
  while (tok) {
    Point rng = {};
    sscanf(tok, "%zu-%zu", &rng.x, &rng.y);
    for (usize i = rng.x; i <= rng.y; i++) {
      c8 pid[PID_BUFSZ] = {};
      snprintf(pid, sizeof(pid), "%zu", i);
      is_regex_match(pid, re) ? total += i : 0;
    }
    tok = strtok(nullptr, ",");
  }
  fclose(input);
  printf("%zu\n", total);
}

i32
main(void) {
  solve("^(.+)\\1$");
  solve("^(.+)\\1+$");
}
[โ€“] h4x0r@lemmy.dbzer0.com 3 points 2 weeks ago* (last edited 2 weeks ago)

c

#include "aoc.h"
#include <stdio.h>
#include <string.h>

constexpr usize LINE_BUFSZ = (1 << 3);
constexpr i32 START = 50;
constexpr i32 TOP = 100;

static void
solve(Mode mode) {
  FILE* input = fopen("input", "r");
  c8 line[LINE_BUFSZ] = {};
  u32 zs = 0;
  i32 idx = START;
  while (fgets(line, sizeof(line), input)) {
    line[strcspn(line, "\n")] = 0;
    i32 val = 0;
    sscanf(&line[1], "%d", &val);
    if (mode == MODE_ONE) {
      i32 d = line[0] == 'L' ? -val : val;
      idx = (idx + d) % TOP;
      idx += idx < 0 ? TOP : 0;
      idx == 0 ? zs++ : 0;
    } else {
      for (i32 i = 0; i < val; i++) {
        idx = line[0] == 'L' ? idx - 1 : idx + 1;
        idx = idx == -1 ? TOP - 1 : idx;
        idx = idx == TOP ? 0 : idx;
        idx == 0 ? zs++ : 0;
      }
    }
  }
  fclose(input);
  printf("%u\n", zs);
}

i32
main(void) {
  solve(MODE_ONE);
  solve(MODE_TWO);
}
[โ€“] h4x0r@lemmy.dbzer0.com 12 points 3 weeks ago

Imagine being wealthy and choosing to live in this grotesque affront.

[โ€“] h4x0r@lemmy.dbzer0.com 23 points 4 weeks ago (7 children)

This is the real truth of it.

There's always some naysayer in these threads with this tired aCkShUaLlY argument, and it is capitulative bullshit.

State payrolls are significant, any governor could easily setup an escrow account, divert all state payroll federal taxes to it, and use it for any shortfalls. They could then create a mechanism within their own state tax system and allow employers to opt in.

[โ€“] h4x0r@lemmy.dbzer0.com 1 points 1 month ago

If someone is going to slit your throat and cut off your dick, would you rather be sober or high?

[โ€“] h4x0r@lemmy.dbzer0.com 2 points 1 month ago (1 children)

Have you considered RTFM?

[โ€“] h4x0r@lemmy.dbzer0.com 8 points 2 months ago

guh-new is not unix folks...

view more: next โ€บ