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
74880223
authored
Dec 11, 2021
by
Stuart Kurtz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Day 11
parent
382124ee
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
80 additions
and
1 deletions
data/energy.txt
src/AOC-11a.hs
test/energy.txt
data/energy.txt
0 → 100644
View file @
74880223
4438624262
6263251864
2618812434
2134264565
1815131247
2612457325
8585767584
7217134556
2825456563
8248473584
\ No newline at end of file
src/AOC-11a.hs
View file @
74880223
module
Main
where
import
Data.Array
import
qualified
Data.Set
as
S
type
Position
=
(
Int
,
Int
)
loadArray
::
String
->
Array
Position
Int
loadArray
s
=
array
bds
[
((
cx
,
rx
),
read
[
val
])
|
(
rx
,
row
)
<-
zip
[
1
..
]
rows
,
(
cx
,
val
)
<-
zip
[
1
..
]
row
]
where
rows
=
lines
s
nx
=
length
(
head
rows
)
ny
=
length
rows
bds
=
((
1
,
1
),(
nx
,
ny
))
showArray
::
Array
Position
Int
->
String
showArray
arr
=
unlines
[
[
head
.
show
$
arr
!
(
x
,
y
)
|
x
<-
[
xMin
..
xMax
]
]
|
y
<-
[
yMin
..
yMax
]
]
where
((
xMin
,
yMin
),(
xMax
,
yMax
))
=
bounds
arr
neighbors
::
Array
Position
a
->
Position
->
[
Position
]
neighbors
arr
(
x
,
y
)
=
[
(
x'
,
y'
)
|
x'
<-
[
x
-
1
..
x
+
1
],
y'
<-
[
y
-
1
..
y
+
1
]
,
inRange
(
bounds
arr
)
(
x'
,
y'
)
,
(
x'
,
y'
)
/=
(
x
,
y
)
]
step
::
(
Array
Position
Int
,
Int
)
->
(
Array
Position
Int
,
Int
)
step
(
as
,
nflashes
)
=
iter
S
.
empty
.
incr
$
as
where
incr
=
((
+
1
)
<$>
)
iter
::
S
.
Set
Position
->
Array
Position
Int
->
(
Array
Position
Int
,
Int
)
iter
hasFlashed
arr
|
S
.
null
newFlash
=
(
discharge
arr
,
nflashes
+
S
.
size
hasFlashed
)
|
otherwise
=
iter
mightFlash
$
accum
(
+
)
arr
[
(
n
,
1
)
|
p
<-
S
.
toList
newFlash
,
n
<-
neighbors
arr
p
]
where
mightFlash
=
S
.
fromList
[
p
|
p
<-
range
(
bounds
arr
),
arr
!
p
>
9
]
newFlash
=
mightFlash
S
.\\
hasFlashed
discharge
::
Array
Position
Int
->
Array
Position
Int
discharge
=
(
reset
<$>
)
where
reset
v
|
v
>
9
=
0
|
otherwise
=
v
allDischarged
::
Array
Position
Int
->
Bool
allDischarged
=
all
(
==
0
)
.
elems
main
::
IO
()
main
=
pure
()
main
=
do
arr
<-
loadArray
<$>
readFile
"data/energy.txt"
let
sync
=
length
.
takeWhile
(
not
.
allDischarged
.
fst
)
.
iterate
step
$
(
arr
,
0
)
print
sync
\ No newline at end of file
test/energy.txt
0 → 100644
View file @
74880223
5483143223
2745854711
5264556173
6141336146
6357385478
4167524645
2176841721
6882881134
4846848554
5283751526
\ 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