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
0dc106ce
Commit
0dc106ce
authored
Dec 06, 2020
by
Alexander Gehrke
Browse files
[day4, refactor] factor out logic to split input list by separator lines
parent
0e5f0d26
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/main/scala/Day4.scala
View file @
0dc106ce
package
aoc2020
import
aoc2020.lib._
import
aoc2020.lib.Passports._
def
day4
(
input
:
List
[
String
])
:
String
=
...
...
@@ -6,13 +7,6 @@ def day4(input: List[String]): String =
s
"complete: ${complete.length}\nvalid: ${complete.count(_.isValid)}"
def
findPassports
(
input
:
List
[
String
])
:
List
[
Passport
]
=
def
go
(
lines
:
List
[
String
],
accu
:
List
[
Passport
])
:
List
[
Passport
]
=
lines
.
span
(
_
!=
""
)
match
{
case
(
passport
,
""
::
rest
)
=>
go
(
rest
,
Passport
.
fromStrings
(
passport
)
::
accu
)
case
(
passport
,
Nil
)
=>
Passport
.
fromStrings
(
passport
)
::
accu
case
_
=>
List
()
// malformed input
}
go
(
input
,
List
())
input
.
split
(
""
).
foldLeft
(
List
[
Passport
]())(
(
passes
,
lines
)
=>
Passport
.
fromStrings
(
lines
)
::
passes
)
src/main/scala/lib/extensions.scala
View file @
0dc106ce
package
aoc2020.lib
/* for splitting input with separator lines */
extension
[
A
](
input
:
List
[
A
])(
using
Eql
[
A
,
A
])
def
split
(
separator
:
A
)
:
LazyList
[
List
[
A
]]
=
input
.
span
(
_
!=
separator
)
match
{
case
(
h
,
_
::
t
)
=>
h
#::
t
.
split
(
separator
)
case
(
Nil
,
Nil
)
=>
LazyList
()
case
(
h
,
Nil
)
=>
LazyList
(
h
)
}
/* Using -Yexplicit-nulls isn't really ready for use with the java standard
* library. e.g. String doesn't have `@NotNull` annotations
*/
...
...
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