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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Regler
Advent of Code 2024
Commits
3bd013aa
Commit
3bd013aa
authored
8 months ago
by
Sebastian Banert
Browse files
Options
Downloads
Patches
Plain Diff
Sebastian's day 8.
parent
965da887
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 8/day_8_sebastian.hs
+54
-0
54 additions, 0 deletions
day 8/day_8_sebastian.hs
with
54 additions
and
0 deletions
day 8/day_8_sebastian.hs
0 → 100644
+
54
−
0
View file @
3bd013aa
module
Main
where
import
Data.Array
import
Data.List
(
transpose
)
import
qualified
Data.Map.Strict
as
M
import
qualified
Data.Set
as
S
main
::
IO
()
main
=
interact
solve
solve
::
String
->
String
solve
s
=
unlines
[
"Part 1: "
++
part1
s
,
"Part 2: "
++
part2
s
]
part1
::
String
->
String
part1
s
=
show
(
S
.
size
(
S
.
filter
(
inRange
(
bounds
m
))
(
getAllAntinodes
(
getAntennas
m
))))
where
m
=
getMap
s
part2
::
String
->
String
part2
s
=
show
(
S
.
size
(
getAllAntinodes'
(
bounds
m
)
(
getAntennas
m
)))
where
m
=
getMap
s
getMap
::
String
->
Array
(
Int
,
Int
)
Char
getMap
s
=
listArray
((
0
,
0
),
(
w
-
1
,
h
-
1
))
(
concat
ls
)
where
ls
=
transpose
(
lines
s
)
w
=
length
(
head
ls
)
h
=
length
ls
getAntennas
::
Array
(
Int
,
Int
)
Char
->
M
.
Map
Char
(
S
.
Set
(
Int
,
Int
))
getAntennas
a
=
foldl
addCharToMap
M
.
empty
(
filter
((
/=
'.'
)
.
snd
)
(
assocs
a
))
addCharToMap
::
M
.
Map
Char
(
S
.
Set
(
Int
,
Int
))
->
((
Int
,
Int
),
Char
)
->
M
.
Map
Char
(
S
.
Set
(
Int
,
Int
))
addCharToMap
m
(
i
,
c
)
=
M
.
alter
(
addPosToSet
i
)
c
m
addPosToSet
::
(
Int
,
Int
)
->
Maybe
(
S
.
Set
(
Int
,
Int
))
->
Maybe
(
S
.
Set
(
Int
,
Int
))
addPosToSet
i
Nothing
=
Just
(
S
.
singleton
i
)
addPosToSet
i
(
Just
s
)
=
Just
(
S
.
insert
i
s
)
getAntinodes
::
S
.
Set
(
Int
,
Int
)
->
S
.
Set
(
Int
,
Int
)
getAntinodes
as
=
S
.
map
(
\
((
x1
,
y1
),
(
x2
,
y2
))
->
(
2
*
x1
-
x2
,
2
*
y1
-
y2
))
(
S
.
filter
(
uncurry
(
/=
))
(
as
`
S
.
cartesianProduct
`
as
))
getAllAntinodes
::
M
.
Map
Char
(
S
.
Set
(
Int
,
Int
))
->
S
.
Set
(
Int
,
Int
)
getAllAntinodes
=
M
.
foldl'
(
\
acc
antennas
->
acc
`
S
.
union
`
getAntinodes
antennas
)
S
.
empty
getAllAntinodes'
::
((
Int
,
Int
),
(
Int
,
Int
))
->
M
.
Map
Char
(
S
.
Set
(
Int
,
Int
))
->
S
.
Set
(
Int
,
Int
)
getAllAntinodes'
bs
=
M
.
foldl'
(
\
acc
antennas
->
acc
`
S
.
union
`
getAntinodes'
bs
antennas
)
S
.
empty
getAntinodes'
::
((
Int
,
Int
),
(
Int
,
Int
))
->
S
.
Set
(
Int
,
Int
)
->
S
.
Set
(
Int
,
Int
)
getAntinodes'
bs
as
=
S
.
foldl
S
.
union
S
.
empty
antinodeSets
where
pairs
=
S
.
filter
(
uncurry
(
/=
))
(
as
`
S
.
cartesianProduct
`
as
)
antinodeSequences
=
S
.
map
(
\
((
x1
,
y1
),
(
x2
,
y2
))
->
antinodeSequence
(
x1
,
y1
)
(
x2
-
x1
,
y2
-
y1
))
pairs
antinodeSets
=
S
.
map
(
S
.
fromList
.
takeWhile
(
inRange
bs
))
antinodeSequences
antinodeSequence
::
(
Int
,
Int
)
->
(
Int
,
Int
)
->
[(
Int
,
Int
)]
antinodeSequence
(
x
,
y
)
(
dx
,
dy
)
=
(
x
,
y
)
:
antinodeSequence
(
x
+
dx
,
y
+
dy
)
(
dx
,
dy
)
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