Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.85 KB | None | 0 0
  1. import scala.collection.mutable.ArrayBuffer
  2.  
  3.  
  4. object Day7 {
  5. val input = scala.io.Source.fromFile("data/data7.txt", "UTF-8").getLines.toVector
  6.  
  7. def part1(): Unit = {
  8. var res2:ArrayBuffer[Int] = ArrayBuffer()
  9. while(true) {
  10. var res1:ArrayBuffer[Int] = ArrayBuffer()
  11. var y = (0,0)
  12. var count = 0
  13. var arr:ArrayBuffer[Int] = ArrayBuffer()
  14. for (i <- 0 to 1) {
  15.  
  16. if (y._1==0) {
  17. arr = ArrayBuffer(0,1,2,3,4)
  18. var x = Math.round(Math.random()*4).toInt
  19. y = copy(arr(x),y._1)
  20. arr.remove(x)
  21.  
  22. x = Math.round(Math.random()*3).toInt
  23. y = copy(arr(x),y._1)
  24. arr.remove(x)
  25.  
  26. x = Math.round(Math.random()*2).toInt
  27. y = copy(arr(x),y._1)
  28. arr.remove(x)
  29.  
  30. x = Math.round(Math.random()*1).toInt
  31. y = copy(arr(x),y._1)
  32. arr.remove(x)
  33.  
  34. x = 0
  35. y = copy(arr(x),y._1)
  36. }
  37. else {
  38. arr = ArrayBuffer(5,6,7,8,9)
  39. var quit = false
  40. var x1 = Math.round(Math.random()*4).toInt
  41. var x2 = Math.round(Math.random()*3).toInt
  42. var x3 = Math.round(Math.random()*2).toInt
  43. var x4 = Math.round(Math.random()*1).toInt
  44. var x5 = Math.round(Math.random()*0).toInt
  45.  
  46. var y1 = (y._1, 0)
  47. var y2 = (y._1, 0)
  48. var y3 = (y._1, 0)
  49. var y4 = (y._1, 0)
  50. var y5 = (y._1, 0)
  51. while (!quit){
  52. y1 = copy(arr(x1),y5._1,y1._2)
  53.  
  54. y2 = copy(arr(x2),y1._1, y2._2)
  55.  
  56.  
  57. y3 = copy(arr(x3),y2._1, y3._2)
  58.  
  59. y4 = copy(arr(x4),y3._1, y4._2)
  60. var tmp = y4
  61. y5 = copy(arr(x5),y4._1, y5._2)
  62. if (y5._1 == -1) {
  63. quit = true
  64. y = (tmp._1,0)
  65. println(y._1)
  66. }
  67. }
  68. }
  69.  
  70. res1 += y._1
  71. }
  72. res2 += res1.max
  73. println("Max: " + res2.max)
  74. }
  75. }
  76.  
  77. def copy(n :Int, n2:Int, pcInit:Int = 0): (Int,Int) = {
  78. val list = input.mkString.split(",").map(x=>x.toInt).toArray
  79. var pc = pcInit
  80. while (pc < list.length && list(pc) != 99) {
  81. var count = 0
  82. var op = list(pc) % 100
  83. var mode1 = (list(pc)/100) % 10
  84. var mode2 = (list(pc)/1000) % 10
  85. var mode3 = (list(pc)/10000) % 10
  86. var ret = 0
  87. op match {
  88. case 1 => {
  89. if (immediate(mode1)) {
  90. if (immediate(mode2)) {
  91. ret = list(pc+1) + list(pc+2)
  92. }
  93. else {
  94. ret = list(pc+1) + list(list(pc+2))
  95. }
  96. }
  97. else {
  98. if (immediate(mode2)) {
  99. ret = list(list(pc+1)) + list(pc+2)
  100. }
  101. else {
  102. ret = list(list(pc+1)) + list(list(pc+2))
  103. }
  104. }
  105.  
  106. if (immediate(mode3)) {
  107. list(pc+3) = ret
  108. }
  109. else {
  110. list(list(pc+3)) = ret
  111. }
  112. pc += 4;
  113. }
  114.  
  115. case 2 => {
  116. if (immediate(mode1)) {
  117.  
  118. if (immediate(mode2)) {
  119. ret = list(pc+1) * list(pc+2)
  120. }
  121. else {
  122. ret = list(pc+1) * list(list(pc+2))
  123. }
  124. }
  125. else {
  126.  
  127. if (immediate(mode2)) {
  128. ret = list(list(pc+1)) * list(pc+2)
  129. }
  130. else {
  131. ret = list(list(pc+1)) * list(list(pc+2))
  132. }
  133. }
  134.  
  135. if (immediate(mode3)) {
  136. list(pc+3) = ret
  137. }
  138. else {
  139. list(list(pc+3)) = ret
  140. }
  141. pc += 4;
  142. }
  143.  
  144. case 3 => {
  145. println("Case 3, PC Diff:" + (pc - pcInit) + ", value:" + n2 + ", count:" + count)
  146. if(count == 0) list(list(pc+1)) = n
  147. else list(list(pc+1)) = n2
  148. pc += 2;
  149. count += 1
  150. }
  151.  
  152. case 4 => {
  153. println("Case 4, PC Diff:" + (pc - pcInit) + ", value:" + n2 + ", count:" + count)
  154. return (list(list(pc+1)),pc+2)
  155.  
  156. }
  157.  
  158. case 5 => {
  159. if (immediate(mode1)) {
  160. if (list(pc+1) != 0) {
  161. if (immediate(mode2)) pc = list(pc+2)
  162. else pc = list(list(pc+2))
  163. }
  164. else pc += 3
  165. }
  166. else {
  167. if (list(list(pc+1)) != 0) {
  168. if (immediate(mode2)) pc = list(pc+2)
  169. else pc = list(list(pc+2))
  170. }
  171. else pc += 3
  172. }
  173. }
  174. case 6 => {
  175. if (immediate(mode1)) {
  176. if (list(pc+1) == 0) {
  177. if (immediate(mode2)) pc = list(pc+2)
  178. else pc = list(list(pc+2))
  179. }
  180. else pc += 3
  181. }
  182. else {
  183. if (list(list(pc+1)) == 0) {
  184. if (immediate(mode2)) pc = list(pc+2)
  185. else pc = list(list(pc+2))
  186. }
  187. else pc += 3
  188. }
  189. }
  190. case 7 => {
  191. if (immediate(mode1)) {
  192. if (immediate(mode2)) {
  193. if (immediate(mode3)) {
  194. if (list(pc+1) < list(pc+2)) list(pc+3) = 1
  195. else list(pc+3) = 0
  196. }
  197. else {
  198. if (list(pc+1) < list(pc+2)) list(list(pc+3)) = 1
  199. else list(list(pc+3)) = 0
  200. }
  201. }
  202. else {
  203. if (immediate(mode3)) {
  204. if (list(pc+1) < list(list(pc+2))) list(pc+3) = 1
  205. else list(pc+3) = 0
  206. }
  207. else {
  208. if (list(pc+1) < list(list(pc+2))) list(list(pc+3)) = 1
  209. else list(list(pc+3)) = 0
  210. }
  211. }
  212. }
  213. else {
  214. if (immediate(mode2)) {
  215. if (immediate(mode3)) {
  216. if (list(list(pc+1)) < list(pc+2)) list(pc+3) = 1
  217. else list(pc+3) = 0
  218. }
  219. else {
  220. if (list(list(pc+1)) < list(pc+2)) list(list(pc+3)) = 1
  221. else list(list(pc+3)) = 0
  222. }
  223. }
  224. else {
  225. if (immediate(mode3)) {
  226. if (list(list(pc+1)) < list(list(pc+2))) list(pc+3) = 1
  227. else list(pc+3) = 0
  228. }
  229. else {
  230. if (list(list(pc+1)) < list(list(pc+2))) list(list(pc+3)) = 1
  231. else list(list(pc+3)) = 0
  232. }
  233. }
  234. }
  235. pc += 4
  236. }
  237. case 8 => {
  238. if (immediate(mode1)) {
  239. if (immediate(mode2)) {
  240. if (immediate(mode3)) {
  241. if (list(pc+1) == list(pc+2)) list(pc+3) = 1
  242. else list(pc+3) = 0
  243. }
  244. else {
  245. if (list(pc+1) == list(pc+2)) list(list(pc+3)) = 1
  246. else list(list(pc+3)) = 0
  247. }
  248. }
  249. else {
  250. if (immediate(mode3)) {
  251. if (list(pc+1) == list(list(pc+2))) list(pc+3) = 1
  252. else list(pc+3) = 0
  253. }
  254. else {
  255. if (list(pc+1) == list(list(pc+2))) list(list(pc+3)) = 1
  256. else list(list(pc+3)) = 0
  257. }
  258. }
  259. }
  260. else {
  261. if (immediate(mode2)) {
  262. if (immediate(mode3)) {
  263. if (list(list(pc+1)) == list(pc+2)) list(pc+3) = 1
  264. else list(pc+3) = 0
  265. }
  266. else {
  267. if (list(list(pc+1)) == list(pc+2)) list(list(pc+3)) = 1
  268. else list(list(pc+3)) = 0
  269. }
  270. }
  271. else {
  272. if (immediate(mode3)) {
  273. if (list(list(pc+1)) == list(list(pc+2))) list(pc+3) = 1
  274. else list(pc+3) = 0
  275. }
  276. else {
  277. if (list(list(pc+1)) == list(list(pc+2))) list(list(pc+3)) = 1
  278. else list(list(pc+3)) = 0
  279. }
  280. }
  281. }
  282. pc += 4
  283. }
  284. case 99 => {
  285. println("HALT")
  286. return (-1,0)
  287. }
  288. case _ => {println("Ogiltig: " + op); pc+=4}
  289. }
  290. }
  291. (-1,pc)
  292. }
  293.  
  294. def immediate(n:Int):Boolean = n == 1
  295.  
  296. def part2(): Unit = {
  297.  
  298. }
  299.  
  300. def apply() = {
  301. println("SOLUTION DAY 7")
  302. println("Running part 1")
  303. part1()
  304. println("Running part 2")
  305. //part2()
  306. }
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement