This commit is contained in:
Joshua Seigler 2021-12-08 01:31:14 -05:00
parent 64bea87901
commit ac0d71598c
4 changed files with 145 additions and 12 deletions

View file

@ -111,16 +111,16 @@
},
{
"part1": {
"solved": false,
"result": null,
"solved": true,
"result": "539",
"attempts": [],
"time": null
"time": 6.01
},
"part2": {
"solved": false,
"result": null,
"solved": true,
"result": "1084606",
"attempts": [],
"time": null
"time": 13.63
}
},
{

View file

@ -18,7 +18,7 @@
[![Day](https://badgen.net/badge/05/%E2%98%85%E2%98%85/green)](src/day05)
[![Day](https://badgen.net/badge/06/%E2%98%85%E2%98%85/green)](src/day06)
[![Day](https://badgen.net/badge/07/%E2%98%85%E2%98%85/green)](src/day07)
![Day](https://badgen.net/badge/08/%E2%98%86%E2%98%86/gray)
[![Day](https://badgen.net/badge/08/%E2%98%85%E2%98%85/green)](src/day08)
![Day](https://badgen.net/badge/09/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/10/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/11/%E2%98%86%E2%98%86/gray)
@ -118,9 +118,9 @@ Both parts: 46.89ms
```
Day 08
Time part 1: -
Time part 2: -
Both parts: -
Time part 1: 6.01ms
Time part 2: 13.63ms
Both parts: 19.64ms
```
```
@ -243,8 +243,8 @@ Both parts: -
```
```
Total stars: 14/50
Total time: 378.67999999999995ms
Total stars: 16/50
Total time: 398.31999999999994ms
```
<!--/RESULTS-->

9
src/day08/README.md Normal file
View file

@ -0,0 +1,9 @@
# 🎄 Advent of Code 2021 - day 8 🎄
## Info
Task description: [link](https://adventofcode.com/2021/day/8)
## Notes
...

124
src/day08/index.js Normal file
View file

@ -0,0 +1,124 @@
import run from "aocrunner"
const parseInput = (rawInput) =>
rawInput
.split("\n")
.map((x) =>
x
.split(" | ")
.map((x) => x.split(" ").map((x) => [...x].sort().join(""))),
)
const part1 = (rawInput) => {
const input = parseInput(rawInput)
return input.reduce(
(acc, [samples, outputs]) =>
acc + outputs.filter((x) => [2, 4, 3, 7].includes(x.length)).length,
0,
)
}
const part2 = (rawInput) => {
function frequencies(str) {
return str.split("").reduce((total, letter) => {
total[letter] ? total[letter]++ : (total[letter] = 1)
return total
}, {})
}
const input = parseInput(rawInput)
return input.reduce((acc, [samples, outputs]) => {
const freq = Object.entries(frequencies(samples.join("")))
const segments = Array(7)
segments[4] = freq.find((x) => x[1] == 4)[0]
const one = samples.find((x) => x.length == 2)
const four = samples.find((x) => x.length == 4)
const seven = samples.find((x) => x.length == 3)
const eight = samples.find((x) => x.length == 7)
segments[0] = [...seven].filter((x) => one.indexOf(x) < 0)[0]
const two = samples.find(
(x) => x.length == 5 && x.indexOf(segments[4]) >= 0,
)
segments[2] = freq.find((x) => x[1] == 8 && x[0] != segments[0])[0]
const five = samples.find(
(x) => x.length == 5 && x.indexOf(segments[2]) < 0,
)
const three = samples.find((x) => x.length == 5 && x != two && x != five)
const six = samples.find((x) => x.length == 6 && x.indexOf(segments[2]) < 0)
const nine = samples.find(
(x) => x.length == 6 && x.indexOf(segments[4]) < 0,
)
const zero = samples.find((x) => x.length == 6 && x != six && x != nine)
const key = [zero, one, two, three, four, five, six, seven, eight, nine]
return (
acc +
parseInt(
outputs.reduce((acc, digit) => acc + key.indexOf(digit), ""),
10,
)
)
}, 0)
}
run({
part1: {
tests: [
{
input: `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`,
expected: 26,
},
],
solution: part1,
},
part2: {
tests: [
{
input: `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`,
expected: 61229,
},
],
solution: part2,
},
trimTestInputs: true,
})
/*
number of segments
0: 6
1: 2
2: 5
3: 5
4: 4
5: 5
6: 6
7: 3
8: 7
9: 6
segment # usage count
0: 8
1: 6
2: 8
3: 7
4: 4
5: 9
6: 7
*/