ShareMyScreenbAdventOfCode/2022/PreambleJP

From J Wiki
Jump to navigation Jump to search

About me

Some perspective on my Advent of Code solutions: I'm not a professional programmer, and don't have a formal computer science background. The programming I encountered during my university education where limited to an introduction to OOP in Java, and use of Maple and Matlab for some math and engineering courses.

I started experimenting with APL and J for their conciseness in code golfing, later, when jumping through flaming hoops to vectorise Matlab code, I discovered their power in writing fast array code and discovered J had been open-sourced. After that, I was hooked and have been using J mostly for recreational programming for over 10 years.

Therefore, don't take my code for gospel; the only claim I make is it worked correctly for my input in a reasonable time frame.

Solutions file template

My solutions for ShareMyScreen assume only that the input is present in the current directory as dd.txt, e.g. 05.txt for day 5.

However I also like to share how I put all solutions together and how I make it work on my phone. For easily entering and executing the solutions file and summarising per-day solutions and timings, I use this template:

NB. All aoc 20xx solutions
DAYS=: 1      NB. days to execute
RES =: 0 3$a: NB. will contain results
PATH=: jpath '~J/aoc2022/' NB. path of inpyt txt's.
NB. === utilities ===
boxdraw_j_ 1 NB. ascii boxes
NB. spin up #cores threads in threadpool 0
0&T.@0^:(0>.([: {. 8&T.)-1&T.) '' 
NB. conj to execute requested solutions (not taking any arg), timing them
NB. use: V dod NN '' where V returns boxed solutions for parts and NN is day number
dod =: {{
if. n e. DAYS do.
  TIC=. 6!:1''
  dr=. u ''[y NB. y
  RES=:RES, (<n), dr,<(6!:1'')-TIC
end.
RES
}}
in=: [: freads PATH,'.txt' ,~ _2 {.!.'0' ": NB. read input of day y (given as num)

NB. Day template to copy-paste (replace dd by day num; append '' after num after dod)
{{ NB. Day dd
idd=: in dd
part1=:0:
part2=:0:
(part1;part2)idd
}} dod dd
NB. print solutions and timing.
summary =: {{
  echo 'AoC 20xx summary'
  echo (<;._1 '|Day|Part 1|Part 2|Time'),((<"0 days),.res),(<;._1 '|||Total time'),+/&.:>{:"1 res
}}
summary''
NB. set indentation to two, and use J's explicit block markers for folding code.
NB. vim: ts=2 sw=2 et fdm=marker foldmarker={{,}}

This allows copying a new day template for each day (when folded, vim conveniently allows copying the folded template block as if it were one line). Each day's solution is implemented as a single ananymous explicit verb ignoring its argument and returning the results for part 1 and 2 in boxes. It is executed by the dod conjunction (taking as left argument the day's solution and as right argument the day number) if the day number is in the days to be executed (DAYS; don't forget to add a dummy argument like '' to force execution of dod).

The in verb, takes a day number and provides a handy shortcut to load the day's input file from the correct path, without much typing. In the future, I'd like to add downloading the input if not present as well, and add a function to submit a solution too.

To get an overview of all days, set DAYS to 1+i.25 and load the file.

AoC mobile setup

As I generally don't have a lot of time, and I'm lucky enough J has a great Android port, I solve most of AoC on my phone. The built-in editor is sadly less than pleasant to use, so I use Vim on Termux, setting up a directory for containing the solution file, and all inputs in ~/config/folders.cfg:

J /storage/emulated/0/j

So I can run my solution file as load ~j/aoc2023/sol.ijs. In lower Android versions (<11 IIRC) this was easier, since one could directly call the jconsole binary from the Termux terminal. Stricter security requirements have removed this possibility.