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
77c85597
Commit
77c85597
authored
Dec 02, 2020
by
Alexander Gehrke
Browse files
[day2] solution + refactoring of day 1/main
parent
e746f7ae
Changes
4
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
77c85597
...
...
@@ -15,11 +15,9 @@ sbt '++ 3.0.0-M1 publishLocal'
To run the code for a day:
```
sbt "run <input_dir> <day>
<part>
"
sbt "run <input_dir> <day>"
```
where
-
`<input_dir>`
is the path to a folder with AOC inputs named "day1.txt", "day2.txt", ...
Can be downloaded e.g. using
[
cargo-aoc
](
https://github.com/gobanos/cargo-aoc
)
-
`<day>`
is the day to run
-
`<part>`
is either 1 or 2. Defaults to 1 if not given
1
where
`<input_dir>`
is the path to a folder with AOC inputs named "day1.txt", "day2.txt", ...
and
`<day>`
is the day to run.
Inputs can be downloaded e.g. using
[
cargo-aoc
](
https://github.com/gobanos/cargo-aoc
)
or of course manually.
src/main/scala/Day1.scala
View file @
77c85597
def
day1_1
(
input
:
List
[
Int
])
:
Int
=
input
.
groupWithSum
(
2020
,
groupSize
=
2
).
product
def
day1_2
(
input
:
List
[
Int
])
:
Int
=
input
.
groupWithSum
(
2020
,
groupSize
=
3
).
product
def
day1
(
input
:
List
[
Int
])
:
String
=
List
(
2
,
3
).
map
(
groupSize
=>
input
.
groupWithSum
(
2020
,
groupSize
).
product
)
.
mkString
(
"\n"
)
extension
(
input
:
List
[
Int
])
def
groupWithSum
(
sum
:
Int
,
groupSize
:
Int
=
2
)
:
List
[
Int
]
=
input
.
combinations
(
groupSize
).
filter
(
_
.
sum
==
sum
).
next
...
...
src/main/scala/Day2.scala
0 → 100644
View file @
77c85597
def
day2
(
passwords
:
List
[
String
])
:
String
=
List
(
passwords
.
count
(
validAmount
),
passwords
.
count
(
validPositions
)
).
mkString
(
"\n"
)
// part 1, password must contain given amount of character
def
validAmount
(
passLine
:
String
)
:
Boolean
=
val
(
min
,
max
,
char
,
pass
)
=
parse
(
passLine
)
(
min
to
max
)
contains
pass
.
count
(
_
==
char
)
// part 2, password must contain character at exactly one of the given positions
def
validPositions
(
passLine
:
String
)
:
Boolean
=
val
(
a
,
b
,
char
,
pass
)
=
parse
(
passLine
)
pass
(
a
-
1
)
==
char
^
pass
(
b
-
1
)
==
char
def
parse
(
passLine
:
String
)
:
(
Int
,
Int
,
Char
,
String
)
=
val
s
"$min-$max $char:$pass"
=
passLine
(
min
.
toInt
,
max
.
toInt
,
char
(
0
),
pass
)
src/main/scala/Main.scala
View file @
77c85597
@main
def
runDay
(
inputDir
:
String
,
day
:
Int
,
part
:
Int
=
1
)
:
Unit
=
@main
def
runDay
(
inputDir
:
String
,
day
:
Int
)
:
Unit
=
val
s
=
scala
.
io
.
Source
.
fromFile
(
s
"${inputDir}/day${day}.txt"
).
getLines
val
out
=
(
day
,
part
)
match
{
case
(
1
,
1
)
=>
day1
_1
(
s
.
map
(
_
.
toInt
).
toList
)
.
toString
case
(
1
,
2
)
=>
day
1_
2
(
s
.
map
(
_
.
toInt
).
toList
).
toString
val
out
=
day
match
{
case
1
=>
day1
(
s
.
map
(
_
.
toInt
).
toList
)
case
2
=>
day2
(
s
.
toList
)
}
println
(
out
)
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