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
63368c39
Commit
63368c39
authored
2 years ago
by
Felix Valentini
Browse files
Options
Downloads
Patches
Plain Diff
day 7.1 working for sample only, bad performance
parent
bcad0f95
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
6/solve.hs
+4
-0
4 additions, 0 deletions
6/solve.hs
7/solve.hs
+35
-24
35 additions, 24 deletions
7/solve.hs
with
39 additions
and
24 deletions
6/solve.hs
+
4
−
0
View file @
63368c39
...
...
@@ -35,6 +35,10 @@ solve2 :: String -> Int
solve2
s
=
fst
$
head
$
filter
(
\
(
x
,
_
)
->
x
>
0
)
$
map
(
\
x
->
evalTuple
$
takeLeftRight
x
14
i
)
[
1
..
(
length
s
)
-
1
]
where
i
=
zip
[
1
..
]
s
-- found on reddit
-- part1 = fst . head . filter (((==) =<< nub) . snd) . zip [ 4..] . map (take 4) . tails
-- part2 = fst . head . filter (((==) =<< nub) . snd) . zip [14..] . map (take 14)
main
::
IO
()
main
=
do
input
<-
head
<$>
lines
<$>
readFile
"./input.txt"
...
...
This diff is collapsed.
Click to expand it.
7/solve.hs
+
35
−
24
View file @
63368c39
-- Day 7
{-# LANGUAGE
MultiWayIf
#-}
{-# LANGUAGE
BangPatterns
#-}
import
Data.List
import
Data.Char
while
::
(
a
->
Bool
)
->
(
a
->
a
)
->
a
->
a
while
p
f
=
go
where
go
!
x
=
if
p
x
then
go
(
f
x
)
else
x
cleanUpNav
::
[
String
]
->
[
String
]
cleanUpNav
=
filter
(
\
x
->
not
$
"$ cd .."
`
isInfixOf
`
x
)
cleanUpNav
=
filter
(
\
x
->
not
$
"$ cd .."
`
isInfixOf
`
x
||
(
x
==
"$ ls"
)
)
getFilesInDir
::
[
String
]
->
[(
String
,
[
String
])]
getFilesInDir
[
x
]
=
[]
getFilesInDir
(
x1
:
_
:
xs
)
=
if
"cd"
`
isInfixOf
`
x1
then
(
getDirName
x1
"$ cd "
,
getUntilCommand
xs
)
:
getFilesInDir
xs
else
getFilesInDir
xs
getDirName
::
String
->
String
->
String
getDirName
x
prefix
=
if
prefix
`
isPrefixOf
`
x
then
last
$
words
x
else
""
getUntilCommand
::
[
String
]
->
[
String
]
getUntilCommand
[
x
]
=
[]
getUntilCommand
[]
=
[]
getUntilCommand
(
x
:
xs
)
=
if
head
x
/=
'$'
then
x
:
getUntilCommand
xs
else
[]
get
FileSize
::
String
->
Int
get
FileSize
=
read
.
head
.
words
nestedDirName
::
String
->
String
nestedDirName
=
last
.
word
s
get
DirNameContent
::
[
String
]
->
[(
String
,
[
String
])]
get
DirNameContent
[]
=
[]
getDirNameContent
(
x
:
xs
)
=
if
"$ cd "
`
isPrefixOf
`
x
then
(
getDirName
x
"$ cd "
,
getUntilCommand
xs
)
:
getDirNameContent
xs
else
getDirNameContent
x
s
findDir
::
String
->
[(
String
,
[
String
])]
->
(
String
,
[
String
])
findDir
n
=
head
.
filter
(
\
(
x
,
_
)
->
x
==
n
)
...
...
@@ -38,19 +34,34 @@ unwrapDir :: String -> [(String, [String])] -> [String]
unwrapDir
dirName
navStack
=
snd
$
findDir
dirName
navStack
filesInNestedDir
::
String
->
[(
String
,
[
String
])]
->
[
String
]
filesInNestedDir
x
stack
=
if
isAlpha
(
head
x
)
filesInNestedDir
x
stack
=
if
"dir"
`
isPrefixOf
`
x
then
unwrapDir
(
getDirName
x
"dir "
)
stack
else
[
x
]
getTotalFilesForDir
::
[
String
]
->
[(
String
,
[
String
])]
->
[[
String
]]
getTotalFilesForDir
[
x
]
stack
=
[
filesInNestedDir
x
stack
]
getTotalFilesForDir
(
x
:
xs
)
stack
=
filesInNestedDir
x
stack
:
getTotalFilesForDir
xs
stack
filesForDir
::
[
String
]
->
[(
String
,
[
String
])]
->
[[
String
]]
filesForDir
[
x
]
stack
=
[
filesInNestedDir
x
stack
]
filesForDir
(
x
:
xs
)
stack
=
filesInNestedDir
x
stack
:
filesForDir
xs
stack
containsDirs
::
[
String
]
->
Bool
containsDirs
l
=
filter
(
"dir"
`
isPrefixOf
`)
l
/=
[]
goUntilNoDir
::
[
String
]
->
[(
String
,
[
String
])]
->
[
String
]
goUntilNoDir
currentDir
stack
=
if
containsDirs
currentDir
then
goUntilNoDir
(
concat
(
filesForDir
currentDir
stack
))
stack
else
currentDir
getFilesInDirs
::
[(
String
,
[
String
])]
->
[(
String
,
[
String
])]
getFilesInDirs
stack
=
map
(
\
(
x
,
y
)
->
(
x
,
goUntilNoDir
y
stack
))
stack
getFileSize
::
String
->
Int
getFileSize
=
read
.
head
.
words
test
::
[(
String
,
[
String
])]
->
[(
String
,
[[
String
]])]
test
stack
=
map
(
\
(
x
,
y
)
->
(
x
,
getTotalFilesForDir
y
stack
))
stack
solve1
::
[(
String
,
[
String
])]
->
Int
solve1
s
=
sum
$
filter
(
\
x
->
x
<=
100000
)
$
map
(
\
(
_
,
y
)
->
sum
$
map
(
getFileSize
)
y
)
s
parse
Input
::
IO
[(
String
,
[
String
])]
parse
Input
=
readFile
"./sample.txt"
>>=
return
.
get
FilesInDir
.
cleanUpNav
.
lines
read
Input
::
IO
[(
String
,
[
String
])]
read
Input
=
readFile
"./sample.txt"
>>=
return
.
get
DirNameContent
.
cleanUpNav
.
lines
main
::
IO
()
main
=
undefined
main
=
readInput
>>=
print
.
solve1
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