attempt 1

This commit is contained in:
Grimmauld 2024-12-09 14:55:22 +01:00
parent 9347e3c4e4
commit 7b91042fd3
Signed by: Grimmauld
SSH key fingerprint: SHA256:Q8IL6Y7sSKqzkyFdV1L0O/EflEh1fFV3tBtwxpapRH4
5 changed files with 129 additions and 0 deletions

View file

@ -0,0 +1 @@
{"errcode":"M_MISSING_TOKEN","error":"Missing access token"}

79
day9/default.nix Normal file
View file

@ -0,0 +1,79 @@
{ lib, utils, ... }:
with lib;
with builtins;
with utils;
let
disk = map toIntBase10 (stringToCharacters (head (readLines ./input)));
files = genList (id: {
size = elemAt disk (id * 2);
freeAfter = elemAt disk (id * 2 + 1);
inherit id;
} ) ((length disk) / 2);
totalSize = listSumWith (getAttr "size") files;
totalFree = listSumWith (getAttr "size") files;
diskExpanded = concatLists (map (f: (genList (const f.id) f.size) ++ (genList (const null) f.freeAfter)) files);
diskSemiExpanded = concatLists (map (f: (genList (const f.id) f.size) ++ (genList (const null) f.freeAfter)) files);
toMove = reverseList (filter (invert isNull) (drop totalSize diskExpanded));
base = take totalSize diskExpanded;
# compress = base: toMove: let firstNullIndex = lists.findFirstIndex isNull null base; in
# if (toMove == []) || (isNull firstNullIndex) then
# base
# else (take firstNullIndex base) ++ [ (head toMove) ] ++ (compress (drop (firstNullIndex + 1) base) (tail toMove) );
build_link_chunk = f: { content = genList (const f.id) f.size; inherit (f) freeAfter; };
compress = uncompressed_chunks: toMove': let
h = head uncompressed_chunks;
moveAmount = min (length toMove') h.freeAfter;
in if toMove' == [] then h.content else h.content ++ (take moveAmount toMove') ++ (compress (tail uncompressed_chunks) (drop moveAmount toMove'));
# foldl' (current: c:
# if isNull c then
# {
# next = current.next + 1;
# content = current.content ++ [ (elemAt toMove current.next) ];
# } else {
# inherit (current) next;
# content = current.content ++ [ c ];
# }
# ) { next = 0; content = []; } base;
chunks = map build_link_chunk files;
# compressed = take totalSize (compress chunks toMove); # taking totalSize because compress overshoots. Compress overshoots because the last file contents are partially duplicated.
compressed = compress chunks toMove; # taking totalSize because compress overshoots. Compress overshoots because the last file contents are partially duplicated.
# expandToMove = foldl' () [] (genList)
checksum = imap0 mul compressed;
# compressed = genList (i: let current = in) (length diskExpanded)
# chunks = map build_link_chunk files;
# compressed = compress chunks;
in {
part1 = listSum checksum;
test1 = elemAt compressed 10000;
test2 = elemAt compressed 20000;
test3 = elemAt compressed 30000;
test4 = elemAt compressed 40000;
test5 = elemAt compressed 49000;
# inherit files;
# inherit chunks;
# inherit compressed;
lenCompressed = length compressed;
compressedHead = head compressed;
inherit totalSize;
moveLen = length toMove;
nullsInBase = count isNull base;
# sumTest = listSumLog [1 2 3 4 5];
diskSize = listSum disk; # TOO DAMN LARGE FOR LINEAR RECURSION!
fileCount = length files; # TOO DAMN LARGE FOR LINEAR RECURSION!
}

34
day9/ideas.nix Normal file
View file

@ -0,0 +1,34 @@
# compress = files: let
# compressionHead = head files;
# compressionTail = last files;
# tailContentLen = length compressionTail.content;
#
# toPop = max compressionHead.freeAfter tailContentLen;
#
# newCompressionHead = {
# content = compressionHead.content ++ (takeTail toPop compressionTail.content);
# freeAfter = compressionHead.freeAfter - toPop;
# };
#
# newTail = {
# content = dropTail toPop compressionTail.content;
# freeAfter = compressionTail.freeAfter + toPop;
# };
#
# newTail' = let second_to_last = last (init files); in{
# inherit (second_to_last) content;
# freeAfter = second_to_last.freeAfter ++ newTail.freeAfter;
# };
#
# reducedRight = if newTail.content != [] then (dropTail 1 (tail files)) ++ [ newTail ] else (dropTail 2 (tail files)) ++ [ newTail' ];
#
# in if length files <= 1 then
# files
# else if compressionHead.freeAfter == 0 then
# [ newCompressionHead ] ++ (compress reducedRight)
# else
# compress ([newCompressionHead] ++ reducedRight );
# link_disk = fs:
# disk_linked =

1
day9/input Normal file

File diff suppressed because one or more lines are too long

View file

@ -8,7 +8,18 @@ with builtins; rec {
abs = i: if i >= 0 then i else i * -1;
delta = x: y: abs (x - y);
listSum = foldl' add 0;
# listSum = listSumLog;
listSumWith = f: l: foldl' add 0 (map f l);
listSumLog = l:
if length l == 1 then
head l
else if l == [] then
0
else
(listSumLog (take ((length l) / 2) l)) + (listSumLog (drop ((length l) / 2) l));
chunk = size: l: if length l < size then l else (take size l) ++ (chunk size (drop size l));
deleteAt = l: i: (take i l) ++ (drop (i + 1) l);
eq = x: y: x == y;
greaterThan = x: y: x > y;
@ -20,6 +31,9 @@ with builtins; rec {
inRange = i: l: (i >= 0) && (i < length l);
identity = x: x;
dropTail = len: l: take ((length l) - len) l;
takeTail = len: l: drop ((length l) - len) l;
transpose = l: map (i: map ((flip elemAt) i) l) (listRange (head l));
matVecMul = m: v: map (r: foldl' add 0 (zipListsWith mul v r)) m;
vecSum = zipListsWith add;