Advent of Code 2017 #2

355 bytes

r=Reverses=StringSplitl=NestListv@i_:=r@ih@i_:=r[i,2]o@i_:=Transpose@v@it=Join@@Map[Table[p->#[[2]],{p,Join[l[o,#[[1]],3],l[o,h[#[[1]]],3]]}]&,Characters[s[#,"/"]&/@s[Import["g","List"]," => "]]];Count[Nest[ArrayFlatten[Partition[#,If[Divisible[Dimensions[#][[1]],2],{2,2},{3,3}]]/.t]&,{{".","#","."},{".",".","#"},{"#","#","#"}},#],"#",2]&/@{5,18}
I'm done with this now. Mathematica wins.

continuing. day 16 auto sep_by(char nc) { return [nc](char c){ return c != nc; }; }void permutation_promenade(string_view input){ char id_perm[16]; iota(begin(id_perm), end(id_perm), 'a'); uint8_t pos_perm[16]; iota(begin(pos_perm), end(pos_perm), 0); const auto by_not_slash = sep_by('/'); for (string_view arg : input | split(sep_by(','))){ switch (arg[0]){ break; case 's': rotate(rbegin(pos_perm), rbegin(pos_perm) + to(arg.substr(1)), rend(pos_perm)); break; case 'x': { auto [a, b, _] = parse_n(arg.substr(1), by_not_slash); swap(pos_perm[to(a)], pos_perm[to(b)]); } break; case 'p': swap(*find(begin(id_perm), end(id_perm), arg[1]), *find(begin(id_perm), end(id_perm), arg[3])); } } char prog1[16], * buf1 = prog1, prog2[16], * buf2 = prog2; iota(begin(prog1), end(prog1), 'a'); const auto dance = [&]{ for (uint8_t j = 0; j < 16; ++j) buf2[j] = id_perm[buf1[pos_perm[j]] - 'a']; swap(buf1, buf2); }; dance(); cout

Tried with vectors instead and it worsened the time initially (not too unexpected), but enabled some easy parallelization for a time of ~ 0.49 s: void dueling_generators2_para(uint64_t a, uint64_t b){ const auto gen = [](uint64_t a, uint64_t mask, uint64_t mul){ return [=]() mutable { vector q; q.reserve(5'000'000); while ((a *= mul) %= 2147483647, q.size() < 5'000'000) if ((a & mask) == 0) q.push_back(a); return move(q); }; }; future gena = async(launch::async, gen(a, 3, 16807)), genb = async(launch::async, gen(b, 7, 48271)); while (!gena.valid() || !genb.valid()) this_thread::yield(); vector qa = gena.get(), qb = genb.get(); size_t count = 0; for (int i = 0; i < 5'000'000; ++i) count += (qa[i] & 0xffff) == (qb[i] & 0xffff); cout

oops, I guess .valid() doesn't do what I thought it did. Maybe I should use .wait_for() or something.

322
Guess I wasn't tired of winning yet.
r=Reverses=StringSplitl=NestLista=Partitionj=Joinh@i_:=r[i,2]o=Transpose@*rk={2,2}c=Characterst=j@@Map[Table[p->#[[2]],{p,j[l[o,#[[1]],3],l[o,h@#[[1]],3]]}]&,c[s[#,"/"]&/@s[Import["g","List"]," => "]]]Count[Nest[ArrayFlatten[a[#,If[Mod[Dimensions[#][[1]],2]==0,k,k+1]]/.t]&,a[c@".#...####", 3],#],"#",2]&/@{5,18}

312
r=Reverses=StringSplitl=NestLista=Partitionj=Joinh@i_:=r[i,2]o=Transpose@*rk={2,2}c=Characterst=j@@Map[Table[p->#[[2]],{p,j[l[o,#[[1]],3],l[o,h@#[[1]],3]]}]&,c[s[#,"/"]&/@s[Import["g","List"]," => "]]]Count[Nest[ArrayFlatten[a[#,If[Mod[Length[#],2]==0,k,k+1]]/.t]&,a[c@".#...####",3],#],"#",2]&/@{5,18}

day 17. I'm sure there must be a clever more efficient way to do it, but whatever, I got the right answer. struct circular_list{ auto take(circular_list* cdr_, int car_) { cdr = cdr_; car = car_; return this; } circular_list* cdr; int car;};template void spinlock(){ circular_list* node = new circular_list[50'000'001]; circular_list* cur = node[0].take(&node[0], 0); const auto step_insert = [&](int x){ for (uint16_t i = 0; i < step; ++i) cur = cur->cdr; cur = cur->cdr = node[x].take(cur->cdr, x); }; for (int i = 1; i

day 18. Simply magical. template struct ins_t { F op; int64_t* to, * from; };template auto make_duet(string_view input, const F& snd, const F& rcv){ static const F set = +[](int64_t* to, int64_t* from, int pc){ *to = *from; return pc + 1; }, add = +[](int64_t* to, int64_t* from, int pc){ *to += *from; return pc + 1; }, mul = +[](int64_t* to, int64_t* from, int pc){ *to *= *from; return pc + 1; }, mod = +[](int64_t* to, int64_t* from, int pc){ *to %= *from; return pc + 1; }, jgz = +[](int64_t* to, int64_t* from, int pc){ return *to > 0 ? pc + (int)*from : pc + 1; }; unordered_map data; int cur_data = 0; vector prog; for (string_view line : input | split(by_line)){ switch (line[1]){ break; case 'n': { auto [x, _] = parse_n(line.substr(4), by_word); prog.push_back({snd, &data[0], isalpha(x[0]) ? &data[x[0]] : &(data[cur_data++] = to(x))}); } break; case 'e': { auto [x, y, _] = parse_n(line.substr(4), by_word); prog.push_back({set, &data[x[0]], isalpha(y[0]) ? &data[y[0]] : &(data[cur_data++] = to(y))}); } break; case 'd': { auto [x, y, _] = parse_n(line.substr(4), by_word); prog.push_back({add, &data[x[0]], isalpha(y[0]) ? &data[y[0]] : &(data[cur_data++] = to(y))}); } break; case 'u': { auto [x, y, _] = parse_n(line.substr(4), by_word); prog.push_back({mul, &data[x[0]], isalpha(y[0]) ? &data[y[0]] : &(data[cur_data++] = to(y))}); } break; case 'o': { auto [x, y, _] = parse_n(line.substr(4), by_word); prog.push_back({mod, &data[x[0]], isalpha(y[0]) ? &data[y[0]] : &(data[cur_data++] = to(y))}); } break; case 'c': { auto [x, _] = parse_n(line.substr(4), by_word); prog.push_back({rcv, &data[0], isalpha(x[0]) ? &data[x[0]] : &(data[cur_data++] = to(x))}); } break; case 'g': { auto [x, y, _] = parse_n(line.substr(4), by_word); prog.push_back({jgz, isalpha(x[0]) ? &data[x[0]] : &(data[cur_data++] = to(x)), isalpha(y[0]) ? &data[y[0]] : &(data[cur_data++] = to(y))}); } } } return pair(move(data), move(prog));}void duet1(string_view input){ auto [data, prog] = make_duet(input, +[](int64_t* to, int64_t* from, int pc){ *to = *from; return pc + 1; },+[](int64_t* to, int64_t* from, int pc){ static bool b = false; if (*from && !b) return cout = prog[0].size()){ status[oldcur] = dead; cur = !oldcur; } else pc[oldcur] = updatedpc; } cout

1. "I don't need to rotate or flip stuff, lol. I'm not using APL. I just need to step through the structure while translating its coordinate system. It's just math."
2. ... ok but how do you do that?
3. pic related
I should've just implemented APL. I don't have the will to continue living long enough to even test this shitty code.

Yeah, you don't need to actually do the inserts, since what you're looking for is always in a known position.


With a fresh look, got it down to 303
r=Reverses=StringSplitl=NestLista=Partitionj=Joinh@i_:=r[i,2]o=Transpose@*rc=Charactersj@@Map[#/.{y_,w_}:>Table[p->w,{p,j[l[o,y,3],l[o,h@y,3]]}]&,c@s[#,"/"]&/@s[Import["g","List"]," => "]]Count[Nest[ArrayFlatten[a[#,If[Mod[Length@#,2]==0,{2,2},{3,3}]]/.%]&,a[c@".#...####",3],#],"#",2]&/@{5,18}

Probably time for a new thread before the Day22 round.