Skip to content

Instantly share code, notes, and snippets.

@ferd

ferd/day21.erl Secret

Created December 22, 2019 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferd/a5a830d2ca58b10013cf1abe8271137f to your computer and use it in GitHub Desktop.
Save ferd/a5a830d2ca58b10013cf1abe8271137f to your computer and use it in GitHub Desktop.
%%% @doc
%%% welcome to day twenty-one, where dreams come true
%%% but all kinds of weird like a monkey paw thing
%%% @end
-module(day21).
-export([p1/0, p2/0]).
%% AND X Y
%% OR X Y
%% NOT X Y
%% where Y is writeable
%%
%% A: 1 ahead
%% B: 2 ahead
%% C: 3 ahead
%% D: 4 ahead
%% T: temporary (false at first)
%% J: jump according to last value (false at first)
%% ABCD: true if ground, false if hole
%% EFGHI: like ABCD, but in RUN mode
%% all jumps land on D (4th tile)
p1() ->
Program = intcode:parse_program(advent:input("day21")),
Pid = intcode:spawn_program(Program, self()),
Script =
"NOT A J\n"
"NOT B T\n"
"OR T J\n"
"NOT C T\n"
"OR T J\n"
"AND D J\n",
intcode:send_string(Pid, Script),
intcode:send_string(Pid, "WALK\n"),
_ = intcode:collect_result(Pid),
lists:last(intcode:collect_io()).
%io:format("~s~n", [intcode:collect_io()]).
p2() ->
Program = intcode:parse_program(advent:input("day21")),
Pid = intcode:spawn_program(Program, self()),
%% @
%% #####.#.#...##### D H = avail + holes in A-C
%% ABCDEFGHI
%% @
%% #####...####..### D (E|H) avail + holes in A-C
%% ABCDEFGHI
Script =
"NOT A T\n"
"NOT B J\n"
"OR T J\n" % hole in A/B in J
"NOT C T\n"
"OR T J\n" % hole in A/B/C in J
"AND D J\n"% D must be free
"NOT J T\n" % if J is true, then T's false; if J is false, it doesn't matter what T is
"OR E T\n"
"OR H T\n"
"AND T J\n",
intcode:send_string(Pid, Script),
intcode:send_string(Pid, "RUN\n"),
_ = intcode:collect_result(Pid),
lists:last(intcode:collect_io()).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment