Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Alexander Gehrke
aoc-2020
Commits
9786d300
Commit
9786d300
authored
Dec 10, 2020
by
Alexander Gehrke
Browse files
[day10] solution. a recursive one, and still fast, take that reddit :P
parent
15c85b83
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/main/scala/Day10.scala
0 → 100644
View file @
9786d300
package
aoc2020
import
aoc2020.lib._
def
day10
(
ratings
:
List
[
Int
])
:
String
=
val
adapters
=
(
0
::
(
ratings
.
max
+
3
)
::
ratings
).
sorted
val
joltages
=
adapters
.
sliding
(
2
,
1
).
toSeq
.
groupMapReduce
{
case
List
(
low
,
high
)
=>
high
-
low
}(
_
=>
1
)(
_
+
_
)
val
variants
=
adapterVariants
(
0
,
adapters
.
tail
)
s
"Joltage differences:\n 1 Jolt: ${joltages(1)}\n 3 Jolts: ${joltages(3)}\n"
+
s
" Product: ${joltages(1) * joltages(3)}\n"
+
s
"\nPossible variants: ${variants}"
val
adapterVariants
:
((
Int
,
List
[
Int
]))
=>
Long
=
memoize
{
case
(
prev
,
remaining
)
=>
{
remaining
match
{
case
h
::
t
if
h
<
prev
+
4
=>
adapterVariants
(
h
,
t
)
+
adapterVariants
(
prev
,
t
)
case
last
::
Nil
=>
1
case
_
=>
0
}
}}
src/main/scala/Main.scala
View file @
9786d300
...
...
@@ -11,6 +11,7 @@ package aoc2020
case
7
=>
input
()(
day7
)
case
8
=>
input
()(
day8
)
case
9
=>
input
(
_
.
toLong
)(
day9
)
case
10
=>
input
(
_
.
toInt
)(
day10
)
case
_
=>
"No such day implemented"
}
println
(
out
)
Write
Preview
Supports
Markdown
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