Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AoC2022
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Valentini, Felix
AoC2022
Commits
c3e71b58
Commit
c3e71b58
authored
2 years ago
by
flex99
Browse files
Options
Downloads
Patches
Plain Diff
solved part 2 of day 5
parent
0dbe7d9a
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
5/solve.hs
+28
-24
28 additions, 24 deletions
5/solve.hs
with
28 additions
and
24 deletions
5/solve.hs
+
28
−
24
View file @
c3e71b58
...
...
@@ -3,11 +3,9 @@
import
Data.Char
import
Data.List
-- input depth: 8
-- sample depth: 3
parseStack
::
Int
->
IO
[
String
]
parseStack
depth
=
map
(
filter
(
/=
' '
))
.
filter
(
any
isUpper
)
.
(
transpose
<$>
take
depth
)
.
lines
parseStack
::
IO
[
String
]
parseStack
=
map
(
filter
(
/=
' '
))
.
filter
(
any
isUpper
)
.
(
transpose
<$>
take
8
)
.
lines
<$>
readFile
"./input.txt"
-- newer copy shit from the internet fucked up my parser!!
...
...
@@ -24,46 +22,52 @@ numbers' = map (read :: String -> Int) . words . filter (not . isAlpha)
tuplify3
::
[
a
]
->
(
a
,
a
,
a
)
tuplify3
(
x1
:
x2
:
x3
:
_
)
=
(
x1
,
x2
,
x3
)
dropFirst
::
Int
->
[
String
]
->
[
String
]
dropFirst
::
Int
->
[
a
]
->
[
a
]
dropFirst
0
xs
=
xs
dropFirst
n
(
_
:
xs
)
=
dropFirst
(
n
-
1
)
xs
-- input depth: 10
-- sample depth: 5
parseMoves
::
Int
->
IO
[(
Int
,
Int
,
Int
)]
parseMoves
depth
=
readFile
"./input.txt"
>>=
(
return
.
map
(
adjustToListIndices
.
tuplify3
.
numbers'
))
.
(
dropFirst
depth
<$>
lines
)
parseMoves
::
IO
[(
Int
,
Int
,
Int
)]
parseMoves
=
readFile
"./input.txt"
>>=
(
return
.
map
(
adjustToListIndices
.
tuplify3
.
numbers'
))
.
(
dropFirst
10
<$>
lines
)
adjustToListIndices
::
(
Int
,
Int
,
Int
)
->
(
Int
,
Int
,
Int
)
adjustToListIndices
(
times
,
from
,
to
)
=
(
times
,
(
from
-
1
)
,
(
to
-
1
)
)
adjustToListIndices
(
times
,
from
,
to
)
=
(
times
,
from
-
1
,
to
-
1
)
move
::
Int
->
Int
->
[
String
]
->
[
String
]
move
from
to
l
=
map
(
\
x
->
if
|
x
==
from
->
(
tail
(
l
!!
from
))
|
x
==
to
->
prepend
((
l
!!
from
)
!!
0
)
(
l
!!
to
)
|
otherwise
->
l
!!
x
)
[
0
..
(
length
l
)
-
1
]
|
x
==
to
->
prepend
(
head
(
l
!!
from
))
(
l
!!
to
)
|
otherwise
->
l
!!
x
)
[
0
..
length
l
-
1
]
nMove
::
(
Int
,
Int
,
Int
)
->
[
String
]
->
[
String
]
nMove
(
1
,
from
,
to
)
l
=
move
from
to
l
nMove
(
times
,
from
,
to
)
l
=
nMove
(
times
-
1
,
from
,
to
)
$
move
from
to
l
move1
::
(
Int
,
Int
,
Int
)
->
[
String
]
->
[
String
]
move1
(
times
,
from
,
to
)
l
=
map
(
\
x
->
if
|
x
==
from
->
dropFirst
times
(
l
!!
from
)
|
x
==
to
->
take
times
(
l
!!
from
)
++
(
l
!!
to
)
|
otherwise
->
(
l
!!
x
))
[
0
..
length
l
-
1
]
moves
::
[(
Int
,
Int
,
Int
)]
->
[
String
]
->
[
String
]
moves
[
x
]
l
=
nMove
x
l
moves
(
x
:
xs
)
l
=
moves
xs
(
nMove
x
l
)
solution1
::
[
String
]
->
String
solution1
=
map
(
head
)
moves1
::
[(
Int
,
Int
,
Int
)]
->
[
String
]
->
[
String
]
moves1
[
x
]
l
=
move1
x
l
moves1
(
x
:
xs
)
l
=
moves1
xs
(
move1
x
l
)
prepend
::
a
->
[
a
]
->
[
a
]
prepend
a
xs
=
a
:
xs
fib
::
Int
->
Int
fib
0
=
1
fib
n
=
n
*
(
n
-
1
)
part1
::
IO
()
part1
=
pure
(
moves
)
<*>
parseMoves
<*>
parseStack
>>=
print
.
map
head
-- To solve I need a foldr
-- e.x. nMove (1, 0, 1) (nMove (2, 1, 0) (nMove (3, 0, 2) (nMove (1, 1, 0) stack)))
part2
::
IO
()
part2
=
pure
(
moves1
)
<*>
parseMoves
<*>
parseStack
>>=
print
.
map
head
main
::
IO
()
main
=
pure
(
moves
)
<*>
parseMoves
10
<*>
parseStack
8
>>=
print
main
=
do
part1
part2
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment