Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Stuart A Kurtz
/
advent-of-code-2021
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Pipelines
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
c1e5f94f
authored
Dec 22, 2021
by
Stuart Kurtz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Day 22, part a
parent
423aa87b
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
82 additions
and
1 deletions
data/reactor.txt
src/AOC-22a.hs
test/reactor.txt
data/reactor.txt
0 → 100644
View file @
c1e5f94f
This diff is collapsed.
Click to expand it.
src/AOC-22a.hs
View file @
c1e5f94f
module
Main
where
import
Data.Char
(
isDigit
)
import
Data.Functor
(
void
,
(
$>
)
)
import
Data.Ix
(
Ix
(
range
)
)
import
qualified
Data.Set
as
S
import
Data.Set
(
Set
)
import
Text.ParserCombinators.ReadP
(
ReadP
,
(
<++
),
char
,
munch1
,
option
,
readP_to_S
,
string
)
type
Range
=
(
Int
,
Int
)
data
Instruction
=
On
Range
Range
Range
|
Off
Range
Range
Range
deriving
Show
type
Point
=
(
Int
,
Int
,
Int
)
parseInt
::
ReadP
Int
parseInt
=
do
sn
<-
option
1
(
char
'-'
$>
(
-
1
))
digits
<-
read
<$>
munch1
isDigit
pure
$
sn
*
digits
parseRange
::
ReadP
Range
parseRange
=
do
mn
<-
parseInt
void
$
string
".."
mx
<-
parseInt
pure
$
(
mn
,
mx
)
parseInstruction
::
ReadP
Instruction
parseInstruction
=
do
op
<-
(
string
"on"
$>
On
)
<++
(
string
"off"
$>
Off
)
void
$
string
" x="
xr
<-
parseRange
void
$
string
",y="
yr
<-
parseRange
void
$
string
",z="
zr
<-
parseRange
pure
$
op
xr
yr
zr
instance
Read
Instruction
where
readsPrec
_
=
readP_to_S
parseInstruction
intersectRange
::
Range
->
Range
->
Range
intersectRange
(
aMin
,
aMax
)
(
bMin
,
bMax
)
=
(
max
aMin
bMin
,
min
aMax
bMax
)
interpret
::
[
Instruction
]
->
Set
Point
interpret
=
foldl
oneInstruction
S
.
empty
where
oneInstruction
s
(
On
xr
yr
zr
)
=
s
`
S
.
union
`
pts
xr
yr
zr
oneInstruction
s
(
Off
xr
yr
zr
)
=
s
`
S
.
difference
`
pts
xr
yr
zr
pts
xr
yr
zr
=
S
.
fromList
$
range
((
xMin
,
yMin
,
zMin
),(
xMax
,
yMax
,
zMax
))
where
constrain
r
=
r
`
intersectRange
`
(
-
50
,
50
)
(
xMin
,
xMax
)
=
constrain
xr
(
yMin
,
yMax
)
=
constrain
yr
(
zMin
,
zMax
)
=
constrain
zr
main
::
IO
()
main
=
pure
()
main
=
do
instrs
<-
map
(
read
@
Instruction
)
.
lines
<$>
readFile
"data/reactor.txt"
print
.
S
.
size
.
interpret
$
instrs
test/reactor.txt
0 → 100644
View file @
c1e5f94f
on x=-20..26,y=-36..17,z=-47..7
on x=-20..33,y=-21..23,z=-26..28
on x=-22..28,y=-29..23,z=-38..16
on x=-46..7,y=-6..46,z=-50..-1
on x=-49..1,y=-3..46,z=-24..28
on x=2..47,y=-22..22,z=-23..27
on x=-27..23,y=-28..26,z=-21..29
on x=-39..5,y=-6..47,z=-3..44
on x=-30..21,y=-8..43,z=-13..34
on x=-22..26,y=-27..20,z=-29..19
off x=-48..-32,y=26..41,z=-47..-37
on x=-12..35,y=6..50,z=-50..-2
off x=-48..-32,y=-32..-16,z=-15..-5
on x=-18..26,y=-33..15,z=-7..46
off x=-40..-22,y=-38..-28,z=23..41
on x=-16..35,y=-41..10,z=-47..6
off x=-32..-23,y=11..30,z=-14..3
on x=-49..-5,y=-3..45,z=-29..18
off x=18..30,y=-20..-8,z=-3..13
on x=-41..9,y=-7..43,z=-33..15
on x=-54112..-39298,y=-85059..-49293,z=-27449..7877
on x=967..23432,y=45373..81175,z=27513..53682
\ No newline at end of file
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