Last active
December 10, 2022 11:10
-
-
Save meooow25/864afaa090af56e9fb4e204e7ebdab9b to your computer and use it in GitHub Desktop.
Advent of Code 2022, learning J
This file contains 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
splitlines2 =: (LF, LF) & strsplit | |
splitlines =: LF & cut | |
parsemany =: ". each | |
part1 =: >./ @: > @: ((+/ @: > @: parsemany @: splitlines) each) @: splitlines2 | |
ans1 =: part1 readaoc 1 | |
part2 =: +/ @: (3 & {.) @: \:~ @: > @: ((+/ @: > @: parsemany @: splitlines) each) @: splitlines2 | |
ans2 =: part2 readaoc 1 |
This file contains 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
data =: > > ((3 | 'ABCXYZ' & i.) each @: ((0 2) & {)) each cutopen readaoc 2 NB. AX->0,BY->1,CZ->2 | |
score =: 3 * 3 | 1 + -~ | |
round =: score + >: @: ] NB. Win score + choice | |
ans1 =: +/ round/"1 data | |
move =: 3 | 2 + + | |
round2 =: 3 & * @: ] + >: @: move NB. Win score + choice | |
ans2 =: +/ round2/"1 data |
This file contains 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
data =: cutopen readaoc 3 | |
intersection =: [ -. -. | |
prio =: (((96+i.27),65+i.26) { a.) & i. | |
commonhalf =: 3 : 0 | |
n =. 2 %~ # y | |
(~. n {. y) intersection ~. n }. y | |
) | |
ans1 =: +/ prio ; commonhalf each data | |
common3 =: 3 : '(~. > 0 { y) intersection (~. > 1 { y) intersection ~. > 2 { y' | |
ans2 =: +/ prio ; common3 each (3 ,: 3) <;.3 data |
This file contains 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
data =: > ". @: ('- , ' & charsub) each cutLF readaoc 4 | |
contains =: (0&{ <: 2&{) *. 3&{ <: 1&{ | |
contains1 =: contains +. contains @: (2 & |.) | |
ans1 =: +/ contains1"1 data | |
overlaps =: (0&{ >. 2&{) <: 1&{ <. 3&{ | |
ans2 =: +/ overlaps"1 data |
This file contains 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
'crates moves' =: LF2 strsplit readaoc 5 | |
crates =: > }: cutLF crates | |
numcrates =: 4 %~ 1 + 1 { $ crates | |
stacks =: (-. @: (' ' & =) # ]) each <"1 (1 + 4 * i. numcrates) { |: crates | |
moves =: > (". @: ((1 3 5) & {) @: > @: cut) each cutLF moves | |
move =: 1 : 0 | |
'cnt fr to' =. y + 0 _1 _1 | |
s =. > fr { st | |
st =: (< cnt }. s) fr} st | |
st =: (< (u cnt {. s) , > to { st) to} st | |
) | |
solve =: 1 : 0 | |
st =: stacks | |
for_m. moves do. u move m end. | |
> {. each st | |
) | |
ans1 =: |. solve | |
ans2 =: ] solve |
This file contains 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
data =: readaoc 6 | |
go =: {{y + 1 i.~ (~. -: ]) "1 (y ]\ data)}} | |
ans1 =: go 4 | |
ans2 =: go 14 |
This file contains 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
data =: > cut each cutLF readaoc 7 | |
st =: 0 0 | |
sz =: i.0 | |
go =: 3 : 0 | |
if. '..' -: > 2 { y do. | |
_2 Z: 1 = # st | |
sz =: ({.st),sz | |
st =: }. (({.st) + 1{st) 1} st | |
elseif. ((1$'$');'cd') -: 2{.y do. | |
st =: 0,st | |
elseif. ] s =: 0 ". > {.y do. | |
st =: (({.st) + s) 0} st | |
end. | |
) | |
] F.. ([: go [) data , 100 3 $ cut '$ cd ..' | |
ans1 =: +/ sz #~ 100000 >: sz | |
ans2 =: <./ sz #~ (40000000 > ({.sz) - ]) sz |
This file contains 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
data =: > ". each ([ , ' ' , ])/ each cutLF readaoc 8 | |
foro =: {{u &.: ] v u &.: |: v u &.: |. v u &.: (|. @: |:)}} NB. for 4 orientations | |
vis =: ] > [: |.!._1 >./\ NB. visible | |
ans1 =: +/ , vis foro +. data | |
nxt =: {{1 + +/ */\ y >"2 (y |.!._~ ])"0 (1 + i. ({. $ data) - 3)}} NB. next greater | |
ans2 =: >./ , nxt foro * }: }. }:"1 }."1 data |
This file contains 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
data =: cutLF readaoc 9 | |
dir =: (4 2 $ _1 0 1 0 0 _1 0 1) {~ 'LRUD' i. ] | |
moves =: ; {{((". }. y) , 2) $ dir {. y}} each data | |
shift =: ([: >./ 1 < |) { 0 0 ,: * | |
follow =: {{x + shift y - x}} | |
moverope =: {{((2*x) + {.y) ] F:. follow y}} | |
solve =: {{# ~. y {: F:. moverope moves}} | |
ans1 =: solve 2 2 $ 0 | |
ans2 =: solve 10 2 $ 0 |
This file contains 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
data =: ; {{ > ('an' i. {. y) { ((0 , ". 4 }. y) ; 0) }} each cutLF readaoc 10 | |
pos =: +/\ 0,1,data | |
ans1 =: +/ (20 + 40 * i. 6) { (i. # pos) * pos | |
ans2 =: ' @' {~ (6 40 $ i. 40) e."(_ 0 1) _1 0 1 +/~ 6 40 $ }. pos |
This file contains 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
1!:44 ('/home/...../aoc2022') NB. Set working dir | |
readfile =: 1!:1 | |
readaoc =: 3 : 0 | |
readfile < ('in/day' , (":y) , '.in') | |
) | |
strsplit=: #@[ }.each [ (E. <;.1 ]) , NB. From https://code.jsoftware.com/wiki/Phrases/Strings |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment