Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- NB. Solutions to Advent of Code 2017 puzzles
- NB. Most of the input can be formatted using ". if an array or number is required.
- NB. Day One, parts one and two. Apply to a 1D array.
- day1p1 =: 3 : '-:+/(+/*=/)"1(y{~(,((#y)&|)@>:))"0 i.#y'
- day1p2 =: 3 : '-:+/(+/*=/)"1(y{~(,((#y)&|)@(+&(-:#y))))"0 i.#y'
- NB. Day Two, parts one and two. Apply to a matrix.
- day2p1 =: +/@:((>./ - <./)"1)
- day2elf =: 3 : '%/({~(|:@:((<.@(%&(#y))),((#y)&|))@:((i.&0)@:,@:((=/~@:i.@#)+(||:)@:($~,~@#))"1)))y'
- day2p2 =: +/@:(day2elf"1)
- NB. Day Three. Apply directly to a number.
- day3p1 =: >.@-:@<:@%:+<./@:|@(-(*:@<:@+:+*&(>:+:i.4))@>.@-:@<:@%:)
- day3p2 =: 3 : '({~ (i.&0)@:(y&>))/:~,(,+/\@:(3+/\(0&,)@:(,&0)@:{:))@:(|:@:|.)^:((2&+)>.^.y)(1 1$1)'
- NB. Day Four. Apply to a multi-line string
- day4p1 =: +/@:((#=#@~.)@(>@;:@>)"0)@cutopen
- day4p2 =: +/@:((#=#@~.)@:(/:~"1)@(>@;:@>)"0)@cutopen
- NB. Day Five. Apply to a list
- day5p1 =: 3 : 0
- a =. y
- pos =. 0
- c =. 0
- while. pos e. i.#a do.
- a =. (>: pos{a) pos} a
- pos =. pos + <: pos{a
- c =. >:c
- end.
- c
- )
- day5p2 =: 3 : 0
- a =. y
- pos =. 0
- c =. 0
- while. pos e. i.#a do.
- g =. (>&2) pos{a
- a =. (>:(<:@<:^:g) pos{a) pos} a
- pos =. <:(>:@>:^:g) pos + pos{a
- c =. >:c
- end.
- c
- )
- NB. Day 6. Takes part (1 or 2) as left argument, list as right (warning: slow)
- NB. Formatting the input was a bit odd, I used the following:
- NB. , ". > cutopen 1!:1 < '/path/to/input/text'
- day6 =: 4 : 0
- s =. |: ,. y
- while. -. ({:s) e. }:s do.
- s =. (, (0&(((i. >./){:s)})+(<:@-@(i.~&({:s)) |. +/@((,&(#{:s)) $!.0 (%~@:>:@i.)))@(>./))@:{:) s
- end.
- 1-~(#s)-(x-1)*(s i. {:s)
- )
- NB. Day 8.
- NB. Day 10.
- NB. Helper functions: convert to hex string, and bitwise xor
- hex =: ,@:(":@({&a.@:(87&+)^:(> 9:))"0)@:(16 16&(#.^:_1))
- xor =: #.@((8$2)&#:@[ (+. *. -.@*.) (8$2)&#:@])
- NB. The main function. Takes part number as left argument.
- day10 =: 4 : 0
- l =. (,&(17 31 73 47 23)@:(a.&i.))^:(<:x) y
- vals =. i.256
- pos =. 0
- skp =. 0
- rnd =. 0
- whilst. (<:x) *. rnd < 64 do.
- len =. 0
- while. len < #l do.
- vals =. (pos, len{l) (-@{.@[ |. {:@[ (|.@{. , }.) {.@[ |. ]) vals
- pos =. pos + skp + len{l
- skp =. >:skp
- len =. >:len
- end.
- rnd =. >:rnd
- end.
- (*/@:(2&{.))`(,@:(hex"0)@:(xor/"1)@:((16 16)&$)) @. (<:x) vals
- )
- NB. Day 13.
- day13p1 =: +/@:((*/ * 0: = 2&*@<:@{: | {.)"1@:((".@}:@>@{. , ".@>@{:)"1)@;:@>@cutopen)
- day13p2 =: (<0 0)&{@:((>:@{. , {:)"1^:(+./@:((0: = 2&*@<:@{: | {.)"1)@:])^:_)@:((".@}:@>@{. , ".@>@{:)"1@;:@>@cutopen)
- NB. Day 14.
- NB. Builds disk of 1s and 0s
- disk =: 3 : 0
- in =. y , '-'
- row =. 0
- dsk =. ,.i.0
- while. row < 128 do.
- dsk =. dsk, (,@:((4#2)&#:"0)@:((".`((-&87)@:(a.&i.))@.(e.&'abcdef'))"0)@:(2&day10)) (in,":row)
- row =. >:row
- end.
- dsk
- )
- NB. Part 1.
- day14p1 =: +/@:(+/@:disk)
- NB. Flood fill algorithm, for part 2.
- flood =: 4 : 0
- node =. 2{.y
- if. (*./ node < $x) *. (*./ node >: 0 0) do.
- color =. (<node) { x
- else.
- color =. {:y
- end.
- if. color = 2{y do.
- x =. ({:y) (<node)} x
- x =. (flood&((node - 0 1),(_2{.y)))@:(flood&((node - 1 0),(_2{.y)))
- x =. (flood&((node + 0 1),(_2{.y)))@:(flood&((node + 1 0),(_2{.y)))
- x
- else.
- x
- end.
- )
- NB. Part 2. Left argument of 1 makes the disk be output, good with viewmat. Otherwise put 0.
- day14p2 =: 3 : 0
- dsk =. -. disk y
- reg =. 0
- while. 0 e. , dsk do.
- reg =. <:reg
- ind =. (#@[ ((<.@%~) , |) (i.&0)@,@])~ dsk
- dsk =. dsk flood ind,0,reg
- end.
- if. x = 1 do.
- dsk
- else.
- -reg
- end.
- )
- NB. Day 15. Slow. Apply to a list of (starting A value , starting B value)
- day15p2 =: 3 : 0
- valA =. {.y
- valB =. {:y
- factA =. 16807x
- factB =. 48271x
- mod =. 2147483647x
- pair =. 0
- count =. 0
- valsA =. i.0
- valsB =. i.0
- while. pair < 5e6 do.
- valA =. mod | factA * valA
- valB =. mod | factB * valB
- if. 0 = 4 | valA do.
- valsA =. valsA , valA
- end.
- if. 0 = 8 | valB do.
- valsB =. valsB , valB
- end.
- if. ((#valsA) > 0) *. ((#valsB) > 0) do.
- pair =. >:pair
- count =. count + ((2^16) | {.valsA) = ((2^16) | {.valsB)
- valsA =. }.valsA
- valsB =. }:valsB
- end.
- end.
- count
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement