Last active
September 4, 2023 18:26
-
-
Save oguz-ismail/931922418eef25e35c119687700a18c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: jq -s -f aoc22-13.jq input.txt | |
def cmp_list($l; $r): | |
def cmp_elem($l; $r): | |
($l | type) as $lt | |
| ($r | type) as $rt | |
| if $lt == $rt then | |
if $lt == "null" then | |
0 | |
elif $lt == "number" then | |
if $l < $r then | |
-1 | |
elif $l == $r then | |
0 | |
else | |
1 | |
end | |
else | |
cmp_list($l; $r) | |
end | |
elif $lt == "null" then | |
-1 | |
elif $rt == "null" then | |
1 | |
elif $lt == "number" then | |
cmp_list([$l]; $r) | |
elif $rt == "number" then | |
cmp_list($l; [$r]) | |
else | |
"bad input" | halt_error | |
end; | |
first([$l, $r] | transpose[] | |
| cmp_elem(.[0]; .[1]) | |
| select(. != 0) | |
); | |
def silver: | |
. as $in | |
| reduce range(length / 2) as $i (0; | |
if $in[$i * 2:] | cmp_list(.[0]; .[1]) == -1 then | |
. + ($i + 1) | |
else | |
. | |
end | |
); | |
def gold: | |
reduce .[] as $lst ([1, 2]; | |
if cmp_list($lst; [[2]]) == -1 then | |
map(. + 1) | |
elif cmp_list($lst; [[6]]) == -1 then | |
.[1] += 1 | |
else | |
. | |
end | |
) | .[0] * .[1]; | |
silver, | |
gold |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
struct num { | |
intmax_t val; | |
size_t prev; | |
size_t next; | |
} nums[5000]; | |
size_t len; | |
size_t | |
nth(size_t i, size_t n) { | |
size_t j; | |
n %= len - 1; | |
for (j = 0; j < n; j++) | |
i = nums[i].next; | |
return i; | |
} | |
int | |
main(void) { | |
size_t i, j, k; | |
intmax_t val; | |
size_t zero; | |
i = 0; | |
while (scanf("%jd ", &val) != EOF) { | |
#if GOLD | |
nums[i].val = val * 811589153; | |
#else | |
nums[i].val = val; | |
#endif | |
nums[i].prev = i - 1; | |
nums[i].next = i + 1; | |
if (val == 0) | |
zero = i; | |
i++; | |
} | |
len = i; | |
assert(len > 0); | |
nums[0].prev = len - 1; | |
nums[len - 1].next = 0; | |
#if GOLD | |
for (k = 0; k < 10; k++) | |
#endif | |
for (i = 0; i < len; i++) { | |
if (i == zero) | |
continue; | |
if (nums[i].val < 0) | |
j = nth(i, (len - 1) - (-nums[i].val % (len - 1))); | |
else | |
j = nth(i, nums[i].val); | |
if (j == i || j == nums[i].prev) | |
continue; | |
nums[nums[i].prev].next = nums[i].next; | |
nums[nums[i].next].prev = nums[i].prev; | |
nums[i].prev = j; | |
nums[i].next = nums[j].next; | |
nums[nums[j].next].prev = i; | |
nums[j].next = i; | |
} | |
printf("%jd\n", nums[nth(zero, 1000)].val | |
+ nums[nth(zero, 2000)].val | |
+ nums[nth(zero, 3000)].val); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment