Skip to content

Instantly share code, notes, and snippets.

@0racle
Last active December 15, 2023 04:13
Show Gist options
  • Save 0racle/094fe3ea41159d8505d8af48a5506ca8 to your computer and use it in GitHub Desktop.
Save 0racle/094fe3ea41159d8505d8af48a5506ca8 to your computer and use it in GitHub Desktop.
AoC 2023 Day 2 - Parsing input in J in multiple ways

Just an exploration of parsing this days input without regex.

Given the following code

c =. Parse;._2 fread 'input'

echo +/ (* #\) 12 13 14 *./@:>:"1 c
echo +/ */"1 c

... any of these Parse subs can be used and the result should be the same

Parse =: {{
    a =. (\: {:@|:) _2 ]\ 2 }. ;: ',;' -.~ y
    (>./@:(".@>@{."1)/.~ ('red';'green';'blue')&i.@{:@|:) a
}}
Parse =: {{
    'red green blue' =. 0 1 2
    a =. ".@> 2 }. ;: ',;' -.~ y
    ({."1 >.//. {:"1) /:~ _2 |.\ a
}}
Parse =: {{
    a =. _2 |.\ 0:`1:`2:`".@.('rgb' i. {.)@> 2 }. ;: ',;' -.~ y
    ({."1 >.//. {:"1) /:~ a
}}
Parse =: {{
    a =. _2 |.\ 2 }. ;: ',;' -.~ y
    >./ (#@>@{. "./. >@{:)&.|: \:~ a
}}
@0racle
Copy link
Author

0racle commented Dec 15, 2023

Smashed down to a 2-liner

P =: {{ >./ (#@>@{. "./. >@{:)&.|: \:~ _2 |.\ 2 }. ;: ',;' -.~ y }}
+/"1 ((#\ ,: 1:) * |:) (*/ ,~ 12 13 14 *./@:>: ])@P;._2 fread 'input'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment