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
3be41036
authored
Dec 11, 2021
by
Stuart Kurtz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Day 11, minor cleanups
parent
74880223
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
10 deletions
src/AOC-11a.hs
src/AOC-11b.hs
src/AOC-11a.hs
View file @
3be41036
module
Main
where
import
Data.Array
(
Ix
(
..
),
(
!
),
accum
,
array
,
bounds
,
Array
)
import
qualified
Data.Set
as
S
type
Position
=
(
Int
,
Int
)
...
...
@@ -38,11 +39,11 @@ step (as,nflashes) = iter S.empty . incr $ as where
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
might
Flash
$
accum
(
+
)
arr
|
otherwise
=
iter
will
Flash
$
accum
(
+
)
arr
[
(
n
,
1
)
|
p
<-
S
.
toList
newFlash
,
n
<-
neighbors
arr
p
]
where
might
Flash
=
S
.
fromList
[
p
|
p
<-
range
(
bounds
arr
),
arr
!
p
>
9
]
newFlash
=
might
Flash
S
.\\
hasFlashed
will
Flash
=
S
.
fromList
[
p
|
p
<-
range
(
bounds
arr
),
arr
!
p
>
9
]
newFlash
=
will
Flash
S
.\\
hasFlashed
discharge
::
Array
Position
Int
->
Array
Position
Int
discharge
=
(
reset
<$>
)
where
...
...
@@ -50,11 +51,8 @@ discharge = (reset <$>) where
|
v
>
9
=
0
|
otherwise
=
v
allDischarged
::
Array
Position
Int
->
Bool
allDischarged
=
all
(
==
0
)
.
elems
main
::
IO
()
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
let
score
=
snd
.
(
!!
100
)
.
iterate
step
$
(
arr
,
0
)
print
score
\ No newline at end of file
src/AOC-11b.hs
View file @
3be41036
module
Main
where
import
Data.Array
(
Ix
(
..
),
(
!
),
accum
,
array
,
bounds
,
elems
,
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
willFlash
$
accum
(
+
)
arr
[
(
n
,
1
)
|
p
<-
S
.
toList
newFlash
,
n
<-
neighbors
arr
p
]
where
willFlash
=
S
.
fromList
[
p
|
p
<-
range
(
bounds
arr
),
arr
!
p
>
9
]
newFlash
=
willFlash
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
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