• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Jan 4th, 2018 198 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. NB. Solutions to Advent of Code 2017 puzzles
2.
3. NB. Most of the input can be formatted using ". if an array or number is required.
4.
5. NB. Day One, parts one and two. Apply to a 1D array.
6.
7. day1p1 =: 3 : '-:+/(+/*=/)"1(y{~(,((#y)&|)@>:))"0 i.#y'
8.
9. day1p2 =: 3 : '-:+/(+/*=/)"1(y{~(,((#y)&|)@(+&(-:#y))))"0 i.#y'
10.
11. NB. Day Two, parts one and two. Apply to a matrix.
12.
13. day2p1 =: +/@:((>./ - <./)"1)
14.
15. day2elf =: 3 : '%/({~(|:@:((<.@(%&(#y))),((#y)&|))@:((i.&0)@:,@:((=/~@:i.@#)+(||:)@:(\$~,~@#))"1)))y'
16. day2p2 =: +/@:(day2elf"1)
17.
18. NB. Day Three. Apply directly to a number.
19.
20. day3p1 =: >.@-:@<:@%:+<./@:|@(-(*:@<:@+:+*&(>:+:i.4))@>.@-:@<:@%:)
21.
22. day3p2 =: 3 : '({~ (i.&0)@:(y&>))/:~,(,+/\@:(3+/\(0&,)@:(,&0)@:{:))@:(|:@:|.)^:((2&+)>.^.y)(1 1\$1)'
23. NB. Day Four. Apply to a multi-line string
24.
25. day4p1 =: +/@:((#=#@~.)@(>@;:@>)"0)@cutopen
26.
27. day4p2 =: +/@:((#=#@~.)@:(/:~"1)@(>@;:@>)"0)@cutopen
28.
29. NB. Day Five. Apply to a list
30.
31. day5p1 =: 3 : 0
32. a =. y
33. pos =. 0
34. c =. 0
35. while. pos e. i.#a do.
36.         a =. (>: pos{a) pos} a
37.         pos =. pos + <: pos{a
38.         c =. >:c
39. end.
40. c
41. )
42.
43. day5p2 =: 3 : 0
44. a =. y
45. pos =. 0
46. c =. 0
47. while. pos e. i.#a do.
48.         g =. (>&2) pos{a
49.         a =. (>:(<:@<:^:g) pos{a) pos} a
50.         pos =. <:(>:@>:^:g) pos + pos{a
51.         c =. >:c
52. end.
53. c
54. )
55.
56. NB. Day 6. Takes part (1 or 2) as left argument, list as right (warning: slow)
57. NB. Formatting the input was a bit odd, I used the following:
58. NB. , ". > cutopen 1!:1 < '/path/to/input/text'
59.
60. day6 =: 4 : 0
61. s =. |: ,. y
62. while. -. ({:s) e. }:s do.
63.         s =. (, (0&(((i. >./){:s)})+(<:@-@(i.~&({:s)) |. +/@((,&(#{:s)) \$!.0 (%~@:>:@i.)))@(>./))@:{:) s
64. end.
65. 1-~(#s)-(x-1)*(s i. {:s)
66. )
67.
68. NB. Day 8.
69.
70.
71. NB. Day 10.
72. NB. Helper functions: convert to hex string, and bitwise xor
73.
74. hex =: ,@:(":@({&a.@:(87&+)^:(> 9:))"0)@:(16 16&(#.^:_1))
75.
76. xor =: #.@((8\$2)&#:@[ (+. *. -.@*.) (8\$2)&#:@])
77.
78. NB. The main function. Takes part number as left argument.
79.
80. day10 =: 4 : 0
81. l =. (,&(17 31 73 47 23)@:(a.&i.))^:(<:x) y
82. vals =. i.256
83. pos =. 0
84. skp =. 0
85. rnd =. 0
86. whilst. (<:x) *. rnd < 64 do.
87.         len =. 0
88.         while. len < #l do.
89.                 vals =. (pos, len{l) (-@{.@[ |. {:@[ (|.@{. , }.) {.@[ |. ]) vals
90.                 pos =. pos + skp + len{l
91.                 skp =. >:skp
92.                 len =. >:len
93.         end.
94.         rnd =. >:rnd
95. end.
96. (*/@:(2&{.))`(,@:(hex"0)@:(xor/"1)@:((16 16)&\$)) @. (<:x) vals
97. )
98.
99. NB. Day 13.
100.
101. day13p1 =: +/@:((*/ * 0: = 2&*@<:@{: | {.)"1@:((".@}:@>@{. , ".@>@{:)"1)@;:@>@cutopen)
102.
103. day13p2 =: (<0 0)&{@:((>:@{. , {:)"1^:(+./@:((0: = 2&*@<:@{: | {.)"1)@:])^:_)@:((".@}:@>@{. , ".@>@{:)"1@;:@>@cutopen)
104.
105. NB. Day 14.
106.
107. NB. Builds disk of 1s and 0s
108.
109. disk =: 3 : 0
110. in =. y , '-'
111. row =. 0
112. dsk =. ,.i.0
113. while. row < 128 do.
114.         dsk =. dsk, (,@:((4#2)&#:"0)@:((".`((-&87)@:(a.&i.))@.(e.&'abcdef'))"0)@:(2&day10)) (in,":row)
115.         row =. >:row
116. end.
117. dsk
118. )
119. NB. Part 1.
120.
121. day14p1 =: +/@:(+/@:disk)
122.
123. NB. Flood fill algorithm, for part 2.
124.
125. flood =: 4 : 0
126. node =. 2{.y
127. if. (*./ node < \$x) *. (*./ node >: 0 0) do.
128.         color =. (<node) { x
129. else.
130.         color =. {:y
131. end.
132. if. color = 2{y do.
133.         x =. ({:y) (<node)} x
134.         x =. (flood&((node - 0 1),(_2{.y)))@:(flood&((node - 1 0),(_2{.y)))
135.         x =. (flood&((node + 0 1),(_2{.y)))@:(flood&((node + 1 0),(_2{.y)))
136.         x
137. else.
138.         x
139. end.
140. )
141. NB. Part 2. Left argument of 1 makes the disk be output, good with viewmat. Otherwise put 0.
142.
143. day14p2 =: 3 : 0
144. dsk =. -. disk y
145. reg =. 0
146. while. 0 e. , dsk do.
147.         reg =. <:reg
148.         ind =. (#@[ ((<.@%~) , |) (i.&0)@,@])~ dsk
149.         dsk =. dsk flood ind,0,reg
150. end.
151. if. x = 1 do.
152.         dsk
153. else.
154.         -reg
155. end.
156. )
157.
158.
159. NB. Day 15. Slow. Apply to a list of (starting A value , starting B value)
160.
161. day15p2 =: 3 : 0
162. valA =. {.y
163. valB =. {:y
164. factA =. 16807x
165. factB =. 48271x
166. mod =. 2147483647x
167. pair =. 0
168. count =. 0
169. valsA =. i.0
170. valsB =. i.0
171. while. pair < 5e6 do.
172.         valA =. mod | factA * valA
173.         valB =. mod | factB * valB
174.         if. 0 = 4 | valA do.
175.                 valsA =. valsA , valA
176.         end.
177.         if. 0 = 8 | valB do.
178.                 valsB =. valsB , valB
179.         end.
180.         if. ((#valsA) > 0) *. ((#valsB) > 0) do.
181.                 pair =. >:pair
182.                 count =. count + ((2^16) | {.valsA) = ((2^16) | {.valsB)
183.                 valsA =. }.valsA
184.                 valsB =. }:valsB
185.         end.
186. end.
187. count
188. )
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top