Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
David
advent-of-code-2019
Commits
94d474eb
Commit
94d474eb
authored
Dec 06, 2019
by
David
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Day 2 part 1
parent
4d1e2967
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
74 additions
and
2 deletions
+74
-2
2/src/main/scala/is/kow/adventofcode/_2019/IntCode.scala
2/src/main/scala/is/kow/adventofcode/_2019/IntCode.scala
+34
-0
2/src/main/scala/is/kow/adventofcode/_2019/Puzzle2.scala
2/src/main/scala/is/kow/adventofcode/_2019/Puzzle2.scala
+12
-0
2/src/test/scala/_2019/Puzzle2Spec.scala
2/src/test/scala/_2019/Puzzle2Spec.scala
+27
-0
settings.gradle.kts
settings.gradle.kts
+1
-2
No files found.
2/src/main/scala/is/kow/adventofcode/_2019/IntCode.scala
0 → 100644
View file @
94d474eb
package
is.kow.adventofcode._2019
import
scala.annotation.tailrec
class
IntCode
(
ops
:
List
[
Int
])
{
private
final
val
ADD
=
1
private
final
val
MULT
=
2
private
final
val
END
=
99
def
execute
()
:
List
[
Int
]
=
{
@tailrec
def
process
(
acc
:
List
[
Int
],
opCount
:
Int
)
:
List
[
Int
]
=
{
acc
.
drop
(
opCount
*
4
)
match
{
case
ADD
::
in1
::
in2
::
dest
::
xs
=>
println
(
s
"add ${acc(in1)} to ${acc(in2)} and put at $dest -- $xs"
)
val
result
=
acc
(
in1
)
+
acc
(
in2
)
process
(
acc
.
updated
(
dest
,
result
),
opCount
+
1
)
case
MULT
::
in1
::
in2
::
dest
::
xs
=>
println
(
s
"mult ${acc(in1)} and ${acc(in2)} and put at $dest -- $xs"
)
val
result
=
acc
(
in1
)
*
acc
(
in2
)
process
(
acc
.
updated
(
dest
,
result
),
opCount
+
1
)
case
END
::
xs
=>
println
(
"IT'S OVER"
)
acc
//return the new list?
case
_
=>
throw
new
Exception
(
"Unknown opcode"
)
}
}
process
(
ops
,
0
)
}
}
2/src/main/scala/is/kow/adventofcode/_2019/Puzzle2.scala
0 → 100644
View file @
94d474eb
package
is.kow.adventofcode._2019
object
Puzzle2
extends
App
{
val
app
=
List
(
1
,
0
,
0
,
3
,
1
,
1
,
2
,
3
,
1
,
3
,
4
,
3
,
1
,
5
,
0
,
3
,
2
,
10
,
1
,
19
,
1
,
19
,
9
,
23
,
1
,
23
,
6
,
27
,
1
,
9
,
27
,
31
,
1
,
31
,
10
,
35
,
2
,
13
,
35
,
39
,
1
,
39
,
10
,
43
,
1
,
43
,
9
,
47
,
1
,
47
,
13
,
51
,
1
,
51
,
13
,
55
,
2
,
55
,
6
,
59
,
1
,
59
,
5
,
63
,
2
,
10
,
63
,
67
,
1
,
67
,
9
,
71
,
1
,
71
,
13
,
75
,
1
,
6
,
75
,
79
,
1
,
10
,
79
,
83
,
2
,
9
,
83
,
87
,
1
,
87
,
5
,
91
,
2
,
91
,
9
,
95
,
1
,
6
,
95
,
99
,
1
,
99
,
5
,
103
,
2
,
103
,
10
,
107
,
1
,
107
,
6
,
111
,
2
,
9
,
111
,
115
,
2
,
9
,
115
,
119
,
2
,
13
,
119
,
123
,
1
,
123
,
9
,
127
,
1
,
5
,
127
,
131
,
1
,
131
,
2
,
135
,
1
,
135
,
6
,
0
,
99
,
2
,
0
,
14
,
0
)
val
fixed
=
app
.
updated
(
1
,
12
).
updated
(
2
,
2
)
val
result
=
new
IntCode
(
fixed
).
execute
()
println
(
"FINAL ANSWER: "
+
result
.
head
)
}
2/src/test/scala/_2019/Puzzle2Spec.scala
0 → 100644
View file @
94d474eb
package
_2019
import
is.kow.adventofcode._2019.IntCode
import
org.junit.runner.RunWith
import
org.scalatest.funspec.AnyFunSpec
import
org.scalatestplus.junit.JUnitRunner
@RunWith
(
classOf
[
JUnitRunner
])
class
Puzzle2Spec
extends
AnyFunSpec
{
describe
(
"Day 2: part 1"
)
{
Map
(
List
(
1
,
0
,
0
,
0
,
99
)
->
List
(
2
,
0
,
0
,
0
,
99
),
List
(
2
,
3
,
0
,
3
,
99
)
->
List
(
2
,
3
,
0
,
6
,
99
),
List
(
2
,
4
,
4
,
5
,
99
,
0
)
->
List
(
2
,
4
,
4
,
5
,
99
,
9801
),
List
(
1
,
1
,
1
,
4
,
99
,
5
,
6
,
0
,
99
)
->
List
(
30
,
1
,
1
,
4
,
2
,
5
,
6
,
0
,
99
)
).
foreach
(
pair
=>
{
it
(
s
"given ${pair._1} produces ${pair._2}"
)
{
assert
(
new
IntCode
(
pair
.
_1
).
execute
()
==
pair
.
_2
)
}
})
}
describe
(
"Puzzle 4: part 2"
)
{
}
}
settings.gradle.kts
View file @
94d474eb
include
(
"1"
)
include
(
"4"
)
include
(
"1"
,
"2"
,
"4"
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment