Skip to content

Commit 3b89366

Browse files
committed
feat(puzzles): Add specs and tests for 2021/day08 puzzle
1 parent 62c15fa commit 3b89366

File tree

4 files changed

+247
-0
lines changed

4 files changed

+247
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Package day08 contains solution for https://adventofcode.com/2021/day/8 puzzle.
2+
package day08
3+
4+
import (
5+
"io"
6+
7+
"github.com/obalunenko/advent-of-code/internal/puzzles"
8+
)
9+
10+
func init() {
11+
puzzles.Register(solution{})
12+
}
13+
14+
type solution struct{}
15+
16+
func (solution) Day() string {
17+
return puzzles.Day08.String()
18+
}
19+
20+
func (solution) Year() string {
21+
return puzzles.Year2021.String()
22+
}
23+
24+
func (solution) Part1(input io.Reader) (string, error) {
25+
return "", puzzles.ErrNotImplemented
26+
}
27+
28+
func (solution) Part2(input io.Reader) (string, error) {
29+
return "", puzzles.ErrNotImplemented
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package day08
2+
3+
import (
4+
"errors"
5+
"io"
6+
"path/filepath"
7+
"testing"
8+
"testing/iotest"
9+
10+
"github.com/stretchr/testify/assert"
11+
12+
"github.com/obalunenko/advent-of-code/internal/puzzles/common/utils"
13+
)
14+
15+
func Test_solution_Year(t *testing.T) {
16+
var s solution
17+
18+
want := "2021"
19+
got := s.Year()
20+
21+
assert.Equal(t, want, got)
22+
}
23+
24+
func Test_solution_Day(t *testing.T) {
25+
var s solution
26+
27+
want := "8"
28+
got := s.Day()
29+
30+
assert.Equal(t, want, got)
31+
}
32+
33+
func Test_solution_Part1(t *testing.T) {
34+
var s solution
35+
36+
type args struct {
37+
input io.Reader
38+
}
39+
40+
tests := []struct {
41+
name string
42+
args args
43+
want string
44+
wantErr assert.ErrorAssertionFunc
45+
}{
46+
{
47+
name: "test example from description",
48+
args: args{
49+
input: utils.ReaderFromFile(t, filepath.Join("testdata", "input.txt")),
50+
},
51+
want: "26",
52+
wantErr: assert.NoError,
53+
},
54+
{
55+
name: "",
56+
args: args{
57+
input: iotest.ErrReader(errors.New("custom error")),
58+
},
59+
want: "",
60+
wantErr: assert.Error,
61+
},
62+
}
63+
64+
for _, tt := range tests {
65+
t.Run(tt.name, func(t *testing.T) {
66+
got, err := s.Part1(tt.args.input)
67+
if !tt.wantErr(t, err) {
68+
return
69+
}
70+
71+
assert.Equal(t, tt.want, got)
72+
})
73+
}
74+
}
75+
76+
func Test_solution_Part2(t *testing.T) {
77+
var s solution
78+
79+
type args struct {
80+
input io.Reader
81+
}
82+
83+
tests := []struct {
84+
name string
85+
args args
86+
want string
87+
wantErr assert.ErrorAssertionFunc
88+
}{
89+
{
90+
name: "test example from description",
91+
args: args{
92+
input: utils.ReaderFromFile(t, filepath.Join("testdata", "input.txt")),
93+
},
94+
want: "",
95+
wantErr: assert.NoError,
96+
},
97+
{
98+
name: "",
99+
args: args{
100+
input: iotest.ErrReader(errors.New("custom error")),
101+
},
102+
want: "",
103+
wantErr: assert.Error,
104+
},
105+
}
106+
107+
for _, tt := range tests {
108+
t.Run(tt.name, func(t *testing.T) {
109+
got, err := s.Part2(tt.args.input)
110+
if !tt.wantErr(t, err) {
111+
return
112+
}
113+
114+
assert.Equal(t, tt.want, got)
115+
})
116+
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# --- Day 8: Seven Segment Search ---
2+
3+
## --- Part One ---
4+
5+
You barely reach the safety of the cave when the whale smashes into the cave mouth, collapsing it.
6+
Sensors indicate another exit to this cave at a much greater depth, so you have no choice but to press on.
7+
8+
As your submarine slowly makes its way through the cave system, you notice that the four-digit seven-segment displays
9+
in your submarine are malfunctioning; they must have been damaged during the escape. You'll be in a lot of trouble
10+
without them, so you'd better figure out what's wrong.
11+
12+
Each digit of a seven-segment display is rendered by turning on or off any of seven segments named a through g:
13+
14+
```text
15+
0: 1: 2: 3: 4:
16+
aaaa .... aaaa aaaa ....
17+
b c . c . c . c b c
18+
b c . c . c . c b c
19+
.... .... dddd dddd dddd
20+
e f . f e . . f . f
21+
e f . f e . . f . f
22+
gggg .... gggg gggg ....
23+
24+
5: 6: 7: 8: 9:
25+
aaaa aaaa aaaa aaaa aaaa
26+
b . b . . c b c b c
27+
b . b . . c b c b c
28+
dddd dddd .... dddd dddd
29+
. f e f . f e f . f
30+
. f e f . f e f . f
31+
gggg gggg .... gggg gggg
32+
```
33+
34+
35+
So, to render a 1, only segments c and f would be turned on; the rest would be off. To render a 7, only segments a, c,
36+
and f would be turned on.
37+
38+
The problem is that the signals which control the segments have been mixed up on each display. The submarine is still
39+
trying to display numbers by producing output on signal wires a through g, but those wires are connected to segments
40+
randomly. Worse, the wire/segment connections are mixed up separately for each four-digit display!
41+
(All of the digits within a display use the same connections, though.)
42+
43+
So, you might know that only signal wires b and g are turned on, but that doesn't mean segments b and g are turned on:
44+
the only digit that uses two segments is 1, so it must mean segments c and f are meant to be on. With just that
45+
information, you still can't tell which wire (b/g) goes to which segment (c/f). For that, you'll need to collect more
46+
information.
47+
48+
For each display, you watch the changing signals for a while, make a note of all ten unique signal patterns you see,
49+
and then write down a single four digit output value (your puzzle input).
50+
Using the signal patterns, you should be able to work out which pattern corresponds to which digit.
51+
52+
### For example, here is what you might see in a single entry in your notes:
53+
54+
```text
55+
acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab |cdfeb fcadb cdfeb cdbaf
56+
```
57+
58+
(The entry is wrapped here to two lines so it fits; in your notes, it will all be on a single line.)
59+
60+
Each entry consists of ten unique signal patterns, a | delimiter, and finally the four digit output value.
61+
Within an entry, the same wire/segment connections are used (but you don't know what the connections actually are).
62+
The unique signal patterns correspond to the ten different ways the submarine tries to render a digit using the current
63+
wire/segment connections. Because 7 is the only digit that uses three segments, dab in the above example means that to
64+
render a 7, signal lines d, a, and b are on. Because 4 is the only digit that uses four segments, eafb means that to
65+
render a 4, signal lines e, a, f, and b are on.
66+
67+
Using this information, you should be able to work out which combination of signal wires corresponds to each of the ten
68+
digits. Then, you can decode the four digit output value. Unfortunately, in the above example,
69+
all of the digits in the output value (cdfeb fcadb cdfeb cdbaf) use five segments and are more difficult to deduce.
70+
71+
### For now, focus on the easy digits. Consider this larger example:
72+
73+
```text
74+
be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb |fdgacbe cefdb cefbgd gcbe
75+
edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec |fcgedb cgb dgebacf gc
76+
fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef |cg cg fdcagb cbg
77+
fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega |efabcd cedba gadfec cb
78+
aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga |gecf egdcabf bgf bfgea
79+
fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf |gebdcfa ecba ca fadegcb
80+
dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf |cefg dcbef fcge gbcadfe
81+
bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd |ed bcgafe cdgba cbgef
82+
egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg |gbdfcae bgc cg cgb
83+
gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc |fgae cfgab fg bagce
84+
```
85+
86+
Because the digits 1, 4, 7, and 8 each use a unique number of segments, you should be able to tell which combinations
87+
of signals correspond to those digits. Counting only digits in the output values (the part after | on each line),
88+
in the above example, there are 26 instances of digits that use a unique number of segments (highlighted above).
89+
90+
In the output values, how many times do digits 1, 4, 7, or 8 appear?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb |fdgacbe cefdb cefbgd gcbe
2+
edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec |fcgedb cgb dgebacf gc
3+
fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef |cg cg fdcagb cbg
4+
fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega |efabcd cedba gadfec cb
5+
aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga |gecf egdcabf bgf bfgea
6+
fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf |gebdcfa ecba ca fadegcb
7+
dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf |cefg dcbef fcge gbcadfe
8+
bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd |ed bcgafe cdgba cbgef
9+
egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg |gbdfcae bgc cg cgb
10+
gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc |fgae cfgab fg bagce

0 commit comments

Comments
 (0)