Definitely the smallest diff between Part 1 and Part 2 — I just deleted a line in my part 1 solution that hard-coded where the light entered the grid

**enter 0 0 (pair 1 0).**

…and instead added that programmaticaly in JavaScript. This wasn’t a fast or awesome Part 2 solution, and relied on JavaScript logic a fair amount, but the Part 1 solution was very clean in pretty much all Dusa.

Challenges

I think lots of people got tripped up on the fact that this problem used \ characters, which are generally an escape character. Dusa doesn’t even support escape characters! So I had to read in the backslashes as &. This was the first time my JavaScript parsing logic used much of anything more than maps, split(), or slice(). (Day 15 also needed charCodeAt(), I suppose.)

Logic

Here’s the part 1 solution Dusa — the answer came from counting the charged facts, where a cell is charged if light enters it.

**charged X Y :- enter X Y _.**

The key logic to this solution was this: the direction of a light beam was described by a tuple pair DX DY, and if a beam is leaving a cell in a particular direction, and there’s a cell (regardless of contents) in that direction, then the beam is entering that adjacent cell in the direction of travel, in the same direction.

**enter (plus X DX) (plus Y DY) (pair DX DY) :-
    leave X Y (pair DX DY),
    cell (plus X DX) (plus Y DY) is _.**

All the rest of the solution is deriving leave facts from enter facts.

“Dots just let light pass through” is represented like this:

**leave X Y Dir :-
    cell X Y is ".",
    enter X Y Dir.**

I once again really wanted multiple-conclusion rules, because without them, second rule below requires some cut-paste of premises. Conceptually: light passes through a - cell if it has no vertical component, and if it has a vertical component then there will be beams exiting to both the left and the right.

**leave X Y Dir :-
    cell X Y is "-",
    enter X Y Dir,
    Dir == pair _ 0.

leave X Y (pair -1 0),
leave X Y (pair 1 0) :-
    cell X Y is "-",
    enter X Y (pair 0 _).**

(The rules for | are analogous.)

Finally, the reflecting mirrors were very nice to describe as one-rule-per-mirror-configuration, though I couldn’t help feeling like this in particular was an awkward way to describe multiplying a vector by a 2x2 rotation matrix.

**leave X Y (pair (minus 0 DY) (minus 0 DX)) :-
    cell X Y is "/",
    enter X Y (pair DX DY).

leave X Y (pair DY DX) :-
    cell X Y is "&",
    enter X Y (pair DX DY).**

JavaScript

These solutions have to use escape characters, I used file IO to avoid that when I solved the problem “for real.”