Hi all!
Just as a bit of fun, I was wondering how short we can manage to get the solutions to the AOC challenges. Try them first before looking what I’ve got so far:
Day 1:
Challenge 1:
-1+/0>‘:d?
Challenge 2:
-3+/0>’:3 msum d
Day 2:
NB: Challenge data imported as a table t headed d & x
Challenge 1:
forward
downup]; // \n for clarity i[0]*i[1]-i[2]</li-code> <p>Challenge 2:</p> <li-code lang='"q"'>((+/)t.x[i]*((+\)?[t.d=
down;t.x;?[j;0;0-t.x]])[i])(+/)t.x[i:(&:)j:t.d=`forward]
Day 3:
NB: Challenge data imported as a list t of lists of Booleans (closest thing to binary I could think of)
Challenge 1:
Challenge 2: (a tad long :/)
(/){a:{1<(#:)(+:)x[1]}{(y[0],b;y[1][1_til(#:)y[1];(&:)y[1;0]=b:{x[0;(2(+/)y)-(#:)y]}[x;y[1;0]]])}/(();(+:)t); 2 sv a[0],+:[a[1]][0]}each(<=;>)Challenge 2: (breakdown)
//find most or least common in a boolean list common:{x[0; (2*(+/)y) - (#:)y]} //examples common[<=] 1100b //most common, default 1 common[>] 1100b //least common, default 0 //select values based on most common first bit // append most common bit to those found so far map:{( y[0],b; y[1][1_ til (#:) y[1]; (&:) y[1;0]=b:common[x;y[1;0]] ] )} //example (using “most common, default 1” comparison) show a:map[<=] ((); flip (110b; 111b; 001b)) // (,1b;(11b;01b)) map[<=] a //(11b;,01b) //limit for iterator //“while the number of values is greater than one” // note the flip, since the intermediate values for map are the flip of the list limit: {1<(#:)(+:)x[1]} // given a comparitor for common, reduce the list t until limit hit // then build final number & convert to decimal reduce: { a:limit map/(();(+:)t); 2 sv a[0],+:[a[1]][0] } // run reduce for O2 & CO2 criteria, then get the product of the results (*/) reduce each (<=;>)