Last active
November 26, 2022 20:40
-
-
Save SamirTalwar/a0dddc670bda4a5cc757574bdeb5abde to your computer and use it in GitHub Desktop.
Solutions for Advent of Code 2021, in J. This is a warmup for 2022.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env jconsole | |
| NB. magic helper function | |
| readfile =: 1 !: 1 | |
| NB. read from STDIN | |
| input =: readfile 3 | |
| NB. split on whitespace, convert to numbers, and unbox | |
| numbers =: > (_1 ". each (cutopen input)) | |
| NB. drop the last element, drop the first element, and compare with `<` | |
| NB. The below is the same as: | |
| NB. matches =: (}: numbers) < (}. numbers) | |
| matches =: (}: < }.) numbers | |
| NB. fold with `+` | |
| answer =: +/ matches | |
| echo answer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env jconsole | |
| readfile =: 1 !: 1 | |
| NB. sliding windows | |
| windows =: dyad : '(1 - x) }."1 (i. x , 1) }. y' | |
| input =: readfile 3 | |
| numbers =: > (_1 ". each (cutopen input)) | |
| answer =: +/ (}: < }.) (+/"2 (3 windows numbers)) | |
| echo answer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env jconsole | |
| readfile =: 1 !: 1 | |
| forward =: monad : 'y , 0' | |
| down =: monad : '0 , y' | |
| up =: monad : '0 , (- y)' | |
| input =: readfile 3 | |
| instructions =: cutopen input | |
| echo */ +/ > ". each instructions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env jconsole | |
| readfile =: 1 !: 1 | |
| apply =: dyad define | |
| current =. > x | |
| aim =. 2 } current | |
| action =. > 0 } > y | |
| amount =. _1 ". > 1 } > y | |
| select. action | |
| case. 'forward' do. current + (amount , (amount * aim) , 0) | |
| case. 'down' do. current + (0 , 0 , amount) | |
| case. 'up' do. current - (0 , 0 , amount) | |
| end. | |
| ) | |
| NB. modifies a function to fold from left to right over a list | |
| over =: adverb define | |
| u~ / |. (x ; y) | |
| ) | |
| input =: readfile 3 | |
| instructions =: cut each cutopen input | |
| result =: (0 0 0) apply over instructions | |
| NB. take the first two elements and multiply them | |
| echo */ 2 {. result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env jconsole | |
| readfile =: 1 !: 1 | |
| fmt =: 8 !: 0 | |
| to_decimal =: monad define | |
| powers =. (2&^) i. {. #"1 y | |
| +/ powers * |. y | |
| ) | |
| mean =: monad : '(+/ % #) y' | |
| round =: monad : '<. +&0.5 y' | |
| input =: ,"2 > _ ". each >&cut each > cutopen readfile 3 | |
| gamma_binary =: round mean"2 input | |
| gamma_decimal =: to_decimal gamma_binary | |
| epsilon_binary =: -. gamma_binary | |
| epsilon_decimal =: to_decimal epsilon_binary | |
| echo > '0.0' fmt gamma_decimal * epsilon_decimal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment