Skip to content
Snippets Groups Projects
Verified Commit 7fffe9ba authored by mirabilos's avatar mirabilos Committed by mirabilos
Browse files

… oh well, it had to come to this

parent 114d7eed
No related branches found
No related tags found
No related merge requests found
#!/bin/lksh
# might need LP64
set -o noglob +o posix
IFS=' '
set -A x -- $(<inputfile)
n=-1
while (( ++n < 25 )); do
print -r -- "@ $SECONDS seconds"
print -r -- "$n (${#x[@]}):" "${x[@]}"
i=-1 z=${#x[@]}
while (( ++i < z )); do
v=${x[i]}
w=${#v}
if [[ $v = 0 ]]; then
x[i]=1
elif (( !(w & 1) )); then
# a=${v::w/2} b=${v:w/2}
# b=${b##+(0)}
# x[i]="$a ${b:-0}"
x[i]="${v::w/2} $((# ${v:w/2}))"
else
x[i]=$((# v * 2024 ))
fi
done
set -A x -- ${x[*]}
done
print -r -- "@ $SECONDS seconds"
print -r -- "$n (${#x[@]}):" "${x[@]}"
#!/bin/mksh
# because s1 is just too slow
# needs LP64, which I fixed for s2
set -o noglob
set -A x -- $(<inputfile)
IFS=,
xs=${x[*]}
IFS=$' \t\n'
gcc -O3 -Wall -DINPUT=$xs s1b.c "$@" && time ./a.out
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifndef NBLINK
#define NBLINK 25
#endif
#define NX 1048576U
static const unsigned long I[] = { INPUT };
static unsigned long x[NX], y[NX];
static char ss[24];
static char *s;
static size_t ltostr(unsigned long v);
int
main(void)
{
size_t z, n, i, w;
int blink;
memcpy(x, I, sizeof(I));
z = sizeof(I) / sizeof(I[0]);
blink = -1;
while (++blink < NBLINK) {
printf("%d (%zu): …\n", blink, z);
#if 0
i = -1;
while (++i < z)
printf("\t%lu\n", x[i]);
#endif
n = 0;
i = -1;
while (++i < z) {
if (!x[i])
y[n++] = 1;
else if (!((w = ltostr(x[i])) & 1)) {
w >>= 1;
y[n++] = atol(s + w);
s[w] = '\0';
y[n++] = atol(s);
} else
y[n++] = x[i] * 2024UL;
}
memcpy(x, y, n * sizeof(I[0]));
z = n;
}
printf("%d (%zu): …\n", blink, z);
#if 0
i = -1;
while (++i < z)
printf("\t%lu\n", x[i]);
#endif
return (0);
}
static size_t
ltostr(unsigned long v)
{
s = ss + sizeof(ss);
*--s = '\0';
if (!v) {
*--s = '0';
return (1);
}
while (v) {
*--s = '0' + (v % 10U);
v /= 10U;
}
return (sizeof(ss) - 1U - (s - ss));
}
#!/bin/mksh
set -o noglob
set -A x -- $(<inputfile)
IFS=,
xs=${x[*]}
IFS=$' \t\n'
gcc -O3 -Wall -DINPUT=$xs s2.c "$@" && time ./a.out
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifndef NBLINK
#define NBLINK 75
#endif
struct hte {
unsigned long long key;
unsigned long long value;
};
//#define HTSZ (1048576U * 256U)
// thank you, https://www.reddit.com/r/adventofcode/comments/1hbnl0l/2024_day_11_plotted_the_number_of_distinct_stone/
#define HTSZ 8192U
#define HMAX (HTSZ / 8U * 7U)
#define NENT ((unsigned long long)-1)
struct hte ht[HTSZ];
struct hte hn[HTSZ];
size_t nkey;
unsigned long long nval;
static const unsigned int I[] = { INPUT };
static char ss[24];
static char *s;
static size_t lltostr(unsigned long long v);
#define BAFHror(eax,cl) (((eax) >> (cl)) | ((eax) << (32 - (cl))))
static inline unsigned int
hash(unsigned int value)
{
register unsigned int h = value, v;
v = (h >> 7) & 0x01010101U;
v += v << 1;
v += v << 3;
v ^= (h << 1) & 0xFEFEFEFEU;
v ^= BAFHror(v, 8);
v ^= (h = BAFHror(h, 8));
v ^= (h = BAFHror(h, 8));
h = v ^ BAFHror(h, 8);
return (h);
}
#define mbccS(x) #x
#define mbccS2(x) mbccS(x)
static inline void
enter(unsigned long long key, unsigned long long howmany)
{
unsigned int h = hash(key & 0xFFFFFFFFU) & (HTSZ - 1U);
nval += howmany;
search:
if (hn[h].key == key) {
hn[h].value += howmany;
#if 0
printf("entering %llu + %llu = %llu\n", key, howmany, hn[h].value);
#endif
return;
}
if (hn[h].key != NENT) {
/* lame, I know */
h = (h + 1U) & (HTSZ - 1U);
goto search;
}
if (++nkey == HMAX)
errx(1, "raise HTSZ");
hn[h].key = key;
hn[h].value = howmany;
#if 0
printf("entering %llu = %llu\n", key, howmany);
#endif
}
int
main(void)
{
size_t i, w;
unsigned long long v;
int blink;
memset(hn, '\xFF', sizeof(hn));
nkey = 0;
nval = 0;
i = sizeof(I) / sizeof(I[0]);
while (i--)
enter(I[i], 1U);
blink = -1;
while (++blink < NBLINK) {
printf("%d (%llu): %zu unique\n", blink, nval, nkey);
memcpy(ht, hn, sizeof(hn));
memset(hn, '\xFF', sizeof(hn));
nkey = 0;
nval = 0;
i = HTSZ;
while (i--) {
if (ht[i].key == NENT)
continue;
v = ht[i].key;
#if 0
printf("from %llu\n", v);
#endif
if (!v)
enter(1U, ht[i].value);
else if (!((w = lltostr(v)) & 1)) {
w >>= 1;
enter(atoll(s + w), ht[i].value);
s[w] = '\0';
enter(atoll(s), ht[i].value);
} else
enter(v * 2024ULL, ht[i].value);
}
}
printf("%d (%llu): %zu unique\n", blink, nval, nkey);
return (0);
}
static size_t
lltostr(unsigned long long v)
{
s = ss + sizeof(ss);
*--s = '\0';
if (!v) {
*--s = '0';
return (1);
}
while (v) {
*--s = '0' + (v % 10U);
v /= 10U;
}
return (sizeof(ss) - 1U - (s - ss));
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment