Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
Advent of Code 2024
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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Regler
Advent of Code 2024
Commits
09f0901c
Commit
09f0901c
authored
5 months ago
by
Sebastian Banert
Browse files
Options
Downloads
Patches
Plain Diff
Sebastian's day 14.
parent
6635afb6
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
day 14/day_14_sebastian.hs
+62
-0
62 additions, 0 deletions
day 14/day_14_sebastian.hs
with
62 additions
and
0 deletions
day 14/day_14_sebastian.hs
0 → 100644
+
62
−
0
View file @
09f0901c
-- 13 + c * 101 = 65 + d * 103
-- i mod 101 = 13
-- i mod 103 = 65
module
Main
where
import
qualified
Data.Set
as
S
wh
::
(
Int
,
Int
)
-- wh = (11, 7)
wh
=
(
101
,
103
)
nimg
::
Int
nimg
=
head
$
filter
((
==
13
)
.
(`
mod
`
101
))
[
65
,
168
..
]
main
::
IO
()
main
=
do
s
<-
getContents
putStrLn
(
"Part 1: "
++
part1
s
)
-- mapM_ (\i -> writeFile (show i ++ ".pbm") (toPbm $ map (simulate i) $ getRobots s)) [0 .. 999]
writeFile
(
show
nimg
++
".pbm"
)
(
toPbm
$
map
(
simulate
nimg
)
$
getRobots
s
)
toPbm
::
[(
Int
,
Int
)]
->
String
toPbm
l
=
unlines
[
"P1"
,
unwords
[
show
w
,
show
h
]]
++
img
where
(
w
,
h
)
=
wh
pixels
=
S
.
fromList
l
indices
=
[[(
i
,
j
)
|
i
<-
[
0
..
w
-
1
]]
|
j
<-
[
0
..
h
-
1
]]
img
=
unlines
$
map
(
unwords
.
map
toPixel
)
indices
toPixel
(
i
,
j
)
=
if
(
i
,
j
)
`
S
.
member
`
pixels
then
"1"
else
"0"
-- solve :: String -> String
-- solve s = unlines ["Part 1: " ++ part1 s, "Part 2: " ++ part2 s]
part1
::
String
->
String
part1
=
show
.
safetyFactor
.
map
(
quadrant
.
simulate
100
)
.
getRobots
-- part2 :: String -> String
-- part2 = const ""
parseRobot
::
String
->
((
Int
,
Int
),
(
Int
,
Int
))
parseRobot
s
=
((
read
x'
,
read
y'
),
(
read
dx'
,
read
dy'
))
where
r1
=
drop
2
s
(
x'
,
r2
)
=
span
(
/=
','
)
r1
(
y'
,
r3
)
=
span
(
/=
' '
)
(
tail
r2
)
(
dx'
,
r4
)
=
span
(
/=
','
)
(
drop
3
r3
)
dy'
=
tail
r4
getRobots
::
String
->
[((
Int
,
Int
),
(
Int
,
Int
))]
getRobots
=
map
parseRobot
.
lines
simulate
::
Int
->
((
Int
,
Int
),
(
Int
,
Int
))
->
(
Int
,
Int
)
simulate
n
((
x
,
y
),
(
dx
,
dy
))
=
((
x
+
n
*
dx
)
`
mod
`
w
,
(
y
+
n
*
dy
)
`
mod
`
h
)
where
(
w
,
h
)
=
wh
quadrant
::
(
Int
,
Int
)
->
Maybe
Int
quadrant
(
x
,
y
)
|
x
<
w
`
div
`
2
&&
y
<
h
`
div
`
2
=
Just
1
|
x
>
w
`
div
`
2
&&
y
<
h
`
div
`
2
=
Just
2
|
x
<
w
`
div
`
2
&&
y
>
h
`
div
`
2
=
Just
3
|
x
>
w
`
div
`
2
&&
y
>
h
`
div
`
2
=
Just
4
|
otherwise
=
Nothing
where
(
w
,
h
)
=
wh
safetyFactor
::
[
Maybe
Int
]
->
Int
safetyFactor
l
=
product
$
map
(
\
i
->
length
(
filter
(
==
Just
i
)
l
))
[
1
,
2
,
3
,
4
]
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