8000 day 8 part 1 seems to work on example values · jamesjarvis/adventofcode@4660b44 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4660b44

Browse files
committed
day 8 part 1 seems to work on example values
1 parent f924a74 commit 4660b44

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

08/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/jamesjarvis/adventofcode/08
2+
3+
go 1.17

08/input.txt

Lines changed: 10 additions & 0 deletions
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

08/main.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package main
2+
3+
import (
4+
_ "embed"
5+
"fmt"
6+
"strings"
7+
)
8+
9+
//go:embed input.txt
10+
var input string
11+
12+
var digitsUnderTest = map[int]struct{}{
13+
1: {},
14+
4: {},
15+
7: {},
16+
8: {},
17+
}
18+
19+
func main() {
20+
if input == "" {
21+
panic("input cannot be empty")
22+
}
23+
24+
problems := []*Problem{}
25+
26+
inputstrarr := strings.Split(input, "\n")
27+
for _, line := range inputstrarr {
28+
lineSplit := strings.Split(line, " | ")
29+
segmentValues := strings.Split(lineSplit[0], " ")
30+
testValues := strings.Split(lineSplit[1], " ")
31+
32+
p := &Problem{
33+
segmentValues: segmentValues,
34+
testValues: testValues,
35+
}
36+
// fmt.Printf("%+v\n", p)
37+
problems = append(problems, p)
38+
}
39+
world := &World{
40+
problems: problems,
41+
}
42+
43+
HowManyTimesDoDigitsAppear := world.HowManyTimesDoDigitsAppear(digitsUnderTest)
44+
fmt.Printf("Answer: %d\n", HowManyTimesDoDigitsAppear)
45+
}
46+
47+
type World struct {
48+
problems []*Problem
49+
}
50+
51+
func (w *World) HowManyTimesDoDigitsAppear(digits map[int]struct{}) (count int) {
52+
for _, p := range w.problems {
53+
for _, test := range p.testValues {
54+
val := p.WhatIsSegmentValue(test)
55+
// fmt.Printf("%s = %d\n", test, val)
56+
if _, ok := digits[val]; ok {
57+
count++
58+
}
59+
}
60+
}
61+
return count
62+
}
63+
64+
type Problem struct {
65+
segmentValues []string
66+
testValues []string
67+
}
68+
69+
func (p *Problem) WhatIsSegmentValue(testValue string) int {
70+
// A simple test for getting values 1,4,7,8 is to check length
71+
// as the number of segments is unique for these values.
72+
switch len(testValue) {
73+
case 2:
74+
return 1
75+
case 4:
76+
return 4
77+
case 3:
78+
return 7
79+
case 7:
80+
return 8
81+
}
82+
83+
return 0
84+
}

0 commit comments

Comments
 (0)
0