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
8d536cfa
Commit
8d536cfa
authored
Dec 05, 2020
by
Alexander Gehrke
Browse files
[day5] solution
parent
a9375c1b
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/main/scala/Day5.scala
0 → 100644
View file @
8d536cfa
import
aoc2020.lib._
def
day5
(
input
:
List
[
String
])
:
String
=
val
ids
=
for
i
<-
input
yield
seatId
(
seatCoords
(
i
))
s
"max id: ${ids.max}\nown id: ${findOwn(ids).map(_.toString).getOrElse("
No
free
seat
!
")}"
def
seatCoords
(
code
:
String
)
:
(
Int
,
Int
)
=
(
recursiveSeat
(
'B'
,
'F'
)(
code
.
substr
(
0
,
7
).
toList
,
0
,
127
),
recursiveSeat
(
'R'
,
'L'
)(
code
.
substr
(
7
).
toList
,
0
,
7
)
)
@annotation
.
tailrec
def
recursiveSeat
(
upChar
:
Char
,
downChar
:
Char
)(
code
:
List
[
Char
],
low
:
Int
,
high
:
Int
)
:
Int
=
code
match
{
case
c
::
rest
=>
val
(
nLow
,
nHigh
)
=
if
c
==
upChar
then
up
(
low
,
high
)
else
down
(
low
,
high
)
recursiveSeat
(
upChar
,
downChar
)(
rest
,
nLow
,
nHigh
)
case
Nil
=>
low
}
inline
def
up
(
low
:
Int
,
high
:
Int
)
=
(
high
-
(
high
-
low
)
/
2
,
high
)
inline
def
down
(
low
:
Int
,
high
:
Int
)
=
(
low
,
high
-
(
high
-
low
)
/
2
-
1
)
inline
def
seatId
(
loc
:
(
Int
,
Int
))
=
loc
.
_1
*
8
+
loc
.
_2
def
findOwn
(
ids
:
List
[
Int
])
:
Option
[
Int
]
=
ids
.
sorted
.
sliding
(
2
).
find
{
l
=>
l
(
1
)
-
l
(
0
)
==
2
}.
map
(
_
(
0
)+
1
)
src/main/scala/Main.scala
View file @
8d536cfa
...
...
@@ -5,5 +5,6 @@
case
2
=>
input
()(
day2
)
case
3
=>
input
(
boolChar
(
'#'
))(
day3
)
case
4
=>
input
()(
day4
)
case
5
=>
input
()(
day5
)
}
println
(
out
)
src/main/scala/lib/extensions.scala
View file @
8d536cfa
...
...
@@ -12,3 +12,8 @@ extension (s: String)
case
Array
(
a
,
b
)
=>
Some
((
a
.
asInstanceOf
[
String
],
b
.
asInstanceOf
[
String
]))
case
_
=>
None
}
def
substr
(
from
:
Int
,
to
:
Int
)
:
String
=
s
.
substring
(
from
,
to
).
asInstanceOf
[
String
]
def
substr
(
from
:
Int
)
:
String
=
s
.
substring
(
from
).
asInstanceOf
[
String
]
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