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
b1be6772
authored
Dec 08, 2021
by
Stuart Kurtz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Day 8 complete
parent
e2d69a60
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
33 deletions
advent-of-code.cabal
src/AOC-08b.hs
advent-of-code.cabal
View file @
b1be6772
...
...
@@ -83,6 +83,7 @@ executable aoc-08a
executable aoc-08b
import : aoc-common
main-is : AOC-08b.hs
build-depends : containers
executable aoc-09a
import : aoc-common
...
...
src/AOC-08b.hs
View file @
b1be6772
module
Main
where
import
Data.List
((
\\
))
-- import Data.Foldable (for_)
-- import Text.Printf
import
Data.List
((
\\
),
sort
)
import
qualified
Data.Map
as
M
import
Data.Map
((
!
))
import
Data.Tuple
(
swap
)
data
Segment
=
A
|
B
|
C
|
D
|
E
|
F
|
G
deriving
(
Eq
,
Show
)
deriving
(
Eq
,
Ord
,
Show
)
type
Code
=
[
Segment
]
repAssocs
::
[(
Char
,
Segment
)]
...
...
@@ -28,7 +29,7 @@ codeAssocs = fmap fromRep <$>
,
(
6
,
"abdefg"
)
,
(
7
,
"acf"
)
,
(
8
,
"abcdefg"
)
,
(
9
,
"abc
e
fg"
)
,
(
9
,
"abc
d
fg"
)
]
charCode
::
Int
->
Code
...
...
@@ -43,37 +44,39 @@ readNote :: String -> Note
readNote
str
=
Note
(
fromRep
<$>
take
10
ws
)
(
fromRep
<$>
drop
11
ws
)
where
ws
=
words
str
score
::
Note
->
Int
score
(
Note
_
out
)
=
length
.
filter
isUniqueCode
$
out
isUniqueCode
::
Code
->
Bool
isUniqueCode
code
=
length
code
`
elem
`
map
codeLength
[
1
,
4
,
7
,
8
]
where
codeLength
=
length
.
charCode
oneCode
::
[
Code
]
->
Code
oneCode
=
head
.
filter
((
==
2
)
.
length
)
fourCode
::
[
Code
]
->
Code
fourCode
=
head
.
filter
((
==
4
)
.
length
)
sevenCode
::
[
Code
]
->
Code
sevenCode
=
head
.
filter
((
==
3
)
.
length
)
eightCode
::
[
Code
]
->
Code
eightCode
=
head
.
filter
((
==
7
)
.
length
)
descramble
::
[
Code
]
->
[
Segment
]
descramble
cs
=
[
a
]
where
one
=
oneCode
cs
four
=
fourCode
cs
seven
=
sevenCode
cs
eight
=
eightCode
cs
a
=
head
$
seven
\\
one
descramble
cs
=
a
++
b
++
c
++
d
++
e
++
f
++
g
where
getLength
n
=
head
.
filter
((
==
n
)
.
length
)
abcdefg
=
[
A
,
B
,
C
,
D
,
E
,
F
,
G
]
-- '8'
compliment
=
(
abcdefg
\\
)
cf
=
getLength
2
cs
-- '1'
acf
=
getLength
3
cs
-- '7'
bcdf
=
getLength
4
cs
-- '4'
a
=
acf
\\
cf
eg
=
(
compliment
a
)
\\
bcdf
bd
=
bcdf
\\
cf
[
six1
,
six2
,
six3
]
=
filter
((
==
6
)
.
length
)
cs
cde
=
compliment
six1
++
compliment
six2
++
compliment
six3
b
=
bd
\\
cde
d
=
bd
\\
b
g
=
eg
\\
cde
e
=
eg
\\
g
c
=
(
cde
\\
e
)
\\
d
f
=
cf
\\
c
decode
::
[
Code
]
->
[
Segment
]
->
Int
decode
obs
=
(
intMap
!
)
.
sort
.
(
map
(
descrambleMap
!
))
where
descrambleMap
=
M
.
fromList
$
zip
(
descramble
obs
)
[
A
,
B
,
C
,
D
,
E
,
F
,
G
]
intMap
=
M
.
fromList
.
map
swap
$
codeAssocs
processNote
::
Note
->
Int
processNote
note
=
unRep
digits
where
decoder
=
decode
(
observations
note
)
digits
=
map
decoder
(
output
note
)
unRep
ds
=
sum
$
zipWith
(
*
)
(
iterate
(
*
10
)
1
)
(
reverse
ds
)
main
::
IO
()
main
=
do
notes
<-
map
readNote
.
lines
<$>
readFile
"data/seven-segment.txt"
print
.
sum
.
map
score
$
notes
print
.
sum
.
map
processNote
$
notes
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