diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-05-16 21:46:51 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-05-16 21:46:51 +0800 |
commit | 4c8cf61f3bd0d509791f46e0fbfc1f9f1ad6e9d9 (patch) | |
tree | 2b7ed37347e015b468541a8684e7eda6e9436c22 /src | |
parent | 450c678de3712a83b2259475f9a2711eb6340705 (diff) | |
download | advent-of-code-4c8cf61f3bd0d509791f46e0fbfc1f9f1ad6e9d9.tar.gz advent-of-code-4c8cf61f3bd0d509791f46e0fbfc1f9f1ad6e9d9.zip |
2021 day8 part1
Diffstat (limited to 'src')
-rw-r--r-- | src/2021/day8/README.md | 48 | ||||
-rw-r--r-- | src/2021/day8/aoc.cpp | 29 | ||||
-rw-r--r-- | src/2021/day8/aoc.h | 1 | ||||
-rw-r--r-- | src/2021/day8/input0 | 10 |
4 files changed, 88 insertions, 0 deletions
diff --git a/src/2021/day8/README.md b/src/2021/day8/README.md index a98cb6e..1830133 100644 --- a/src/2021/day8/README.md +++ b/src/2021/day8/README.md @@ -70,4 +70,52 @@ Because the digits 1, 4, 7, and 8 each use a unique number of segments, you shou In the output values, how many times do digits 1, 4, 7, or 8 appear? +--- Part Two --- +Through a little deduction, you should now be able to determine the remaining digits. Consider again the first example above: +acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab | +cdfeb fcadb cdfeb cdbaf +After some careful analysis, the mapping between signal wires and segments only make sense in the following configuration: + + dddd +e a +e a + ffff +g b +g b + cccc +So, the unique signal patterns would correspond to the following digits: + +acedgfb: 8 +cdfbe: 5 +gcdfa: 2 +fbcad: 3 +dab: 7 +cefabd: 9 +cdfgeb: 6 +eafb: 4 +cagedb: 0 +ab: 1 +Then, the four digits of the output value can be decoded: + +cdfeb: 5 +fcadb: 3 +cdfeb: 5 +cdbaf: 3 +Therefore, the output value for this entry is 5353. + +Following this same process for each entry in the second, larger example above, the output value of each entry can be determined: + +fdgacbe cefdb cefbgd gcbe: 8394 +fcgedb cgb dgebacf gc: 9781 +cg cg fdcagb cbg: 1197 +efabcd cedba gadfec cb: 9361 +gecf egdcabf bgf bfgea: 4873 +gebdcfa ecba ca fadegcb: 8418 +cefg dcbef fcge gbcadfe: 4548 +ed bcgafe cdgba cbgef: 1625 +gbdfcae bgc cg cgb: 8717 +fgae cfgab fg bagce: 4315 +Adding all of the output values in this larger example produces 61229. + +For each entry, determine all of the wire/segment connections and decode the four-digit output values. What do you get if you add up all of the output values? diff --git a/src/2021/day8/aoc.cpp b/src/2021/day8/aoc.cpp index 7a3c895..9f95991 100644 --- a/src/2021/day8/aoc.cpp +++ b/src/2021/day8/aoc.cpp @@ -1,5 +1,34 @@ #include "aoc.h" +#include <algorithm> namespace aoc2021 { +static void get_length(const char** pp, int* d) { + const char* p = *pp; + *d = 0; + while (*p >= 'a' && *p <= 'g') { + *d += 1; + p++; + } + *pp = p; } + +int day8(line_view file) { + // int digits[] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; + int total{0}; + int ls[4] = {0}; + per_line(file, [&ls, &total](line_view lv) { + const char* p = lv.contains("|") + 2; + int index{0}; + while (p < lv.line + lv.length) { + get_length(&p, &ls[index]); + total += int(ls[index] == 2 || ls[index] == 3 || ls[index] == 4 || ls[index] == 7); + index++; + p++; + } + return true; + }); + return total; +} + +} // namespace aoc2021 diff --git a/src/2021/day8/aoc.h b/src/2021/day8/aoc.h index cd205cd..cc4b869 100644 --- a/src/2021/day8/aoc.h +++ b/src/2021/day8/aoc.h @@ -3,4 +3,5 @@ namespace aoc2021 { +int day8(line_view); } diff --git a/src/2021/day8/input0 b/src/2021/day8/input0 new file mode 100644 index 0000000..c9f629b --- /dev/null +++ b/src/2021/day8/input0 @@ -0,0 +1,10 @@ +be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe +edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc +fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg +fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba gadfec cb +aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf bgf bfgea +fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba ca fadegcb +dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef fcge gbcadfe +bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbgef +egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb +gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce |