Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scala.collection.mutable.ArrayBuffer
- object Day7 {
- val input = scala.io.Source.fromFile("data/data7.txt", "UTF-8").getLines.toVector
- def part1(): Unit = {
- var res2:ArrayBuffer[Int] = ArrayBuffer()
- while(true) {
- var res1:ArrayBuffer[Int] = ArrayBuffer()
- var y = (0,0)
- var count = 0
- var arr:ArrayBuffer[Int] = ArrayBuffer()
- for (i <- 0 to 1) {
- if (y._1==0) {
- arr = ArrayBuffer(0,1,2,3,4)
- var x = Math.round(Math.random()*4).toInt
- y = copy(arr(x),y._1)
- arr.remove(x)
- x = Math.round(Math.random()*3).toInt
- y = copy(arr(x),y._1)
- arr.remove(x)
- x = Math.round(Math.random()*2).toInt
- y = copy(arr(x),y._1)
- arr.remove(x)
- x = Math.round(Math.random()*1).toInt
- y = copy(arr(x),y._1)
- arr.remove(x)
- x = 0
- y = copy(arr(x),y._1)
- }
- else {
- arr = ArrayBuffer(5,6,7,8,9)
- var quit = false
- var x1 = Math.round(Math.random()*4).toInt
- var x2 = Math.round(Math.random()*3).toInt
- var x3 = Math.round(Math.random()*2).toInt
- var x4 = Math.round(Math.random()*1).toInt
- var x5 = Math.round(Math.random()*0).toInt
- var y1 = (y._1, 0)
- var y2 = (y._1, 0)
- var y3 = (y._1, 0)
- var y4 = (y._1, 0)
- var y5 = (y._1, 0)
- while (!quit){
- y1 = copy(arr(x1),y5._1,y1._2)
- y2 = copy(arr(x2),y1._1, y2._2)
- y3 = copy(arr(x3),y2._1, y3._2)
- y4 = copy(arr(x4),y3._1, y4._2)
- var tmp = y4
- y5 = copy(arr(x5),y4._1, y5._2)
- if (y5._1 == -1) {
- quit = true
- y = (tmp._1,0)
- println(y._1)
- }
- }
- }
- res1 += y._1
- }
- res2 += res1.max
- println("Max: " + res2.max)
- }
- }
- def copy(n :Int, n2:Int, pcInit:Int = 0): (Int,Int) = {
- val list = input.mkString.split(",").map(x=>x.toInt).toArray
- var pc = pcInit
- while (pc < list.length && list(pc) != 99) {
- var count = 0
- var op = list(pc) % 100
- var mode1 = (list(pc)/100) % 10
- var mode2 = (list(pc)/1000) % 10
- var mode3 = (list(pc)/10000) % 10
- var ret = 0
- op match {
- case 1 => {
- if (immediate(mode1)) {
- if (immediate(mode2)) {
- ret = list(pc+1) + list(pc+2)
- }
- else {
- ret = list(pc+1) + list(list(pc+2))
- }
- }
- else {
- if (immediate(mode2)) {
- ret = list(list(pc+1)) + list(pc+2)
- }
- else {
- ret = list(list(pc+1)) + list(list(pc+2))
- }
- }
- if (immediate(mode3)) {
- list(pc+3) = ret
- }
- else {
- list(list(pc+3)) = ret
- }
- pc += 4;
- }
- case 2 => {
- if (immediate(mode1)) {
- if (immediate(mode2)) {
- ret = list(pc+1) * list(pc+2)
- }
- else {
- ret = list(pc+1) * list(list(pc+2))
- }
- }
- else {
- if (immediate(mode2)) {
- ret = list(list(pc+1)) * list(pc+2)
- }
- else {
- ret = list(list(pc+1)) * list(list(pc+2))
- }
- }
- if (immediate(mode3)) {
- list(pc+3) = ret
- }
- else {
- list(list(pc+3)) = ret
- }
- pc += 4;
- }
- case 3 => {
- println("Case 3, PC Diff:" + (pc - pcInit) + ", value:" + n2 + ", count:" + count)
- if(count == 0) list(list(pc+1)) = n
- else list(list(pc+1)) = n2
- pc += 2;
- count += 1
- }
- case 4 => {
- println("Case 4, PC Diff:" + (pc - pcInit) + ", value:" + n2 + ", count:" + count)
- return (list(list(pc+1)),pc+2)
- }
- case 5 => {
- if (immediate(mode1)) {
- if (list(pc+1) != 0) {
- if (immediate(mode2)) pc = list(pc+2)
- else pc = list(list(pc+2))
- }
- else pc += 3
- }
- else {
- if (list(list(pc+1)) != 0) {
- if (immediate(mode2)) pc = list(pc+2)
- else pc = list(list(pc+2))
- }
- else pc += 3
- }
- }
- case 6 => {
- if (immediate(mode1)) {
- if (list(pc+1) == 0) {
- if (immediate(mode2)) pc = list(pc+2)
- else pc = list(list(pc+2))
- }
- else pc += 3
- }
- else {
- if (list(list(pc+1)) == 0) {
- if (immediate(mode2)) pc = list(pc+2)
- else pc = list(list(pc+2))
- }
- else pc += 3
- }
- }
- case 7 => {
- if (immediate(mode1)) {
- if (immediate(mode2)) {
- if (immediate(mode3)) {
- if (list(pc+1) < list(pc+2)) list(pc+3) = 1
- else list(pc+3) = 0
- }
- else {
- if (list(pc+1) < list(pc+2)) list(list(pc+3)) = 1
- else list(list(pc+3)) = 0
- }
- }
- else {
- if (immediate(mode3)) {
- if (list(pc+1) < list(list(pc+2))) list(pc+3) = 1
- else list(pc+3) = 0
- }
- else {
- if (list(pc+1) < list(list(pc+2))) list(list(pc+3)) = 1
- else list(list(pc+3)) = 0
- }
- }
- }
- else {
- if (immediate(mode2)) {
- if (immediate(mode3)) {
- if (list(list(pc+1)) < list(pc+2)) list(pc+3) = 1
- else list(pc+3) = 0
- }
- else {
- if (list(list(pc+1)) < list(pc+2)) list(list(pc+3)) = 1
- else list(list(pc+3)) = 0
- }
- }
- else {
- if (immediate(mode3)) {
- if (list(list(pc+1)) < list(list(pc+2))) list(pc+3) = 1
- else list(pc+3) = 0
- }
- else {
- if (list(list(pc+1)) < list(list(pc+2))) list(list(pc+3)) = 1
- else list(list(pc+3)) = 0
- }
- }
- }
- pc += 4
- }
- case 8 => {
- if (immediate(mode1)) {
- if (immediate(mode2)) {
- if (immediate(mode3)) {
- if (list(pc+1) == list(pc+2)) list(pc+3) = 1
- else list(pc+3) = 0
- }
- else {
- if (list(pc+1) == list(pc+2)) list(list(pc+3)) = 1
- else list(list(pc+3)) = 0
- }
- }
- else {
- if (immediate(mode3)) {
- if (list(pc+1) == list(list(pc+2))) list(pc+3) = 1
- else list(pc+3) = 0
- }
- else {
- if (list(pc+1) == list(list(pc+2))) list(list(pc+3)) = 1
- else list(list(pc+3)) = 0
- }
- }
- }
- else {
- if (immediate(mode2)) {
- if (immediate(mode3)) {
- if (list(list(pc+1)) == list(pc+2)) list(pc+3) = 1
- else list(pc+3) = 0
- }
- else {
- if (list(list(pc+1)) == list(pc+2)) list(list(pc+3)) = 1
- else list(list(pc+3)) = 0
- }
- }
- else {
- if (immediate(mode3)) {
- if (list(list(pc+1)) == list(list(pc+2))) list(pc+3) = 1
- else list(pc+3) = 0
- }
- else {
- if (list(list(pc+1)) == list(list(pc+2))) list(list(pc+3)) = 1
- else list(list(pc+3)) = 0
- }
- }
- }
- pc += 4
- }
- case 99 => {
- println("HALT")
- return (-1,0)
- }
- case _ => {println("Ogiltig: " + op); pc+=4}
- }
- }
- (-1,pc)
- }
- def immediate(n:Int):Boolean = n == 1
- def part2(): Unit = {
- }
- def apply() = {
- println("SOLUTION DAY 7")
- println("Running part 1")
- part1()
- println("Running part 2")
- //part2()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement