mirror of
https://github.com/seigler/aoc2021
synced 2025-07-27 01:16:09 +00:00
day 14
This commit is contained in:
parent
b0bf232079
commit
8c135affd7
4 changed files with 153 additions and 16 deletions
|
@ -195,16 +195,16 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"part1": {
|
"part1": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": "2657",
|
||||||
"attempts": [],
|
"attempts": [],
|
||||||
"time": null
|
"time": 1.6
|
||||||
},
|
},
|
||||||
"part2": {
|
"part2": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": "2911561572630",
|
||||||
"attempts": [],
|
"attempts": [],
|
||||||
"time": null
|
"time": 4.68
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
20
README.md
20
README.md
|
@ -23,8 +23,8 @@
|
||||||
[](src/day10)
|
[](src/day10)
|
||||||
[](src/day11)
|
[](src/day11)
|
||||||
[](src/day12)
|
[](src/day12)
|
||||||
[](src/day13)
|
[](src/day13)
|
||||||

|
[](src/day14)
|
||||||

|

|
||||||

|

|
||||||

|

|
||||||
|
@ -153,16 +153,16 @@ Both parts: 602.51ms
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 13
|
Day 13
|
||||||
Time part 1: 3.8ms
|
Time part 1: 3.59ms
|
||||||
Time part 2: -
|
Time part 2: 17.85ms
|
||||||
Both parts: 3.8ms
|
Both parts: 21.44ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 14
|
Day 14
|
||||||
Time part 1: -
|
Time part 1: 11.29ms
|
||||||
Time part 2: -
|
Time part 2: 5.6ms
|
||||||
Both parts: -
|
Both parts: 16.89ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -243,8 +243,8 @@ Both parts: -
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Total stars: 25/50
|
Total stars: 28/50
|
||||||
Total time: 1060.56ms
|
Total time: 1095.0899999999997ms
|
||||||
```
|
```
|
||||||
|
|
||||||
<!--/RESULTS-->
|
<!--/RESULTS-->
|
||||||
|
|
9
src/day14/README.md
Normal file
9
src/day14/README.md
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
# 🎄 Advent of Code 2021 - day 14 🎄
|
||||||
|
|
||||||
|
## Info
|
||||||
|
|
||||||
|
Task description: [link](https://adventofcode.com/2021/day/14)
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
...
|
128
src/day14/index.js
Normal file
128
src/day14/index.js
Normal file
|
@ -0,0 +1,128 @@
|
||||||
|
import run from "aocrunner"
|
||||||
|
|
||||||
|
const parseInput = (rawInput) => {
|
||||||
|
const [start, insertions] = rawInput.trim().split`\n\n`
|
||||||
|
return [start, new Map(insertions.split`\n`.map((i) => i.split` -> `))]
|
||||||
|
}
|
||||||
|
|
||||||
|
const part1 = (rawInput) => {
|
||||||
|
// const [start, insertions] = parseInput(rawInput)
|
||||||
|
// let p = start
|
||||||
|
// for (let i = 0; i < 10; i++) {
|
||||||
|
// let q = ""
|
||||||
|
// p.split("").forEach((c, j) => {
|
||||||
|
// if (j > 0) {
|
||||||
|
// const pair = p[j - 1] + p[j]
|
||||||
|
// q += insertions.get(pair) || ""
|
||||||
|
// }
|
||||||
|
// q += p[j]
|
||||||
|
// })
|
||||||
|
// p = q
|
||||||
|
// }
|
||||||
|
|
||||||
|
// let counts = {}
|
||||||
|
// p.split``.map((c) => (counts[c] = 1 + (counts[c] ?? 0)))
|
||||||
|
// counts = Object.entries(counts).sort((a, b) => a[1] - b[1])
|
||||||
|
// return counts[counts.length - 1][1] - counts[0][1]
|
||||||
|
return part2(rawInput, 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
const part2 = (rawInput, steps = 40) => {
|
||||||
|
let [start, insertions] = parseInput(rawInput)
|
||||||
|
let pairCounts = {}
|
||||||
|
start.split("").map((c, i) => {
|
||||||
|
if (i > 0) {
|
||||||
|
const pair = start[i - 1] + start[i]
|
||||||
|
pairCounts[pair] = 1 + (pairCounts[pair] ?? 0)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
for (let step = 0; step < steps; step++) {
|
||||||
|
const nextPairCount = {}
|
||||||
|
for (const [pair, count] of Object.entries(pairCounts)) {
|
||||||
|
const insertion = insertions.get(pair)
|
||||||
|
if (insertion !== undefined) {
|
||||||
|
const A = pair[0] + insertion,
|
||||||
|
B = insertion + pair[1]
|
||||||
|
nextPairCount[A] = (nextPairCount[A] ?? 0) + count
|
||||||
|
nextPairCount[B] = (nextPairCount[B] ?? 0) + count
|
||||||
|
} else {
|
||||||
|
nextPairCount[pair] = count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pairCounts = nextPairCount
|
||||||
|
}
|
||||||
|
|
||||||
|
let counts = {}
|
||||||
|
Object.entries(pairCounts).map(([pair, count]) => {
|
||||||
|
const A = pair[0],
|
||||||
|
B = pair[1]
|
||||||
|
counts[A] = (counts[A] ?? 0) + count
|
||||||
|
counts[B] = (counts[B] ?? 0) + count
|
||||||
|
})
|
||||||
|
/*
|
||||||
|
Since each pair overlaps with a neighbor, each letter will be
|
||||||
|
counted twice. To make the doubling perfect, the first and
|
||||||
|
last characters (which are unchanged) must be incremented as
|
||||||
|
well.
|
||||||
|
*/
|
||||||
|
counts[start[0]]++
|
||||||
|
counts[start[start.length - 1]]++
|
||||||
|
counts = Object.entries(counts).sort((a, b) => a[1] - b[1])
|
||||||
|
return (counts[counts.length - 1][1] - counts[0][1]) / 2
|
||||||
|
}
|
||||||
|
|
||||||
|
run({
|
||||||
|
part1: {
|
||||||
|
tests: [
|
||||||
|
{
|
||||||
|
input: `NNCB
|
||||||
|
|
||||||
|
CH -> B
|
||||||
|
HH -> N
|
||||||
|
CB -> H
|
||||||
|
NH -> C
|
||||||
|
HB -> C
|
||||||
|
HC -> B
|
||||||
|
HN -> C
|
||||||
|
NN -> C
|
||||||
|
BH -> H
|
||||||
|
NC -> B
|
||||||
|
NB -> B
|
||||||
|
BN -> B
|
||||||
|
BB -> N
|
||||||
|
BC -> B
|
||||||
|
CC -> N
|
||||||
|
CN -> C`,
|
||||||
|
expected: 1588,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
solution: part1,
|
||||||
|
},
|
||||||
|
part2: {
|
||||||
|
tests: [
|
||||||
|
{
|
||||||
|
input: `NNCB
|
||||||
|
|
||||||
|
CH -> B
|
||||||
|
HH -> N
|
||||||
|
CB -> H
|
||||||
|
NH -> C
|
||||||
|
HB -> C
|
||||||
|
HC -> B
|
||||||
|
HN -> C
|
||||||
|
NN -> C
|
||||||
|
BH -> H
|
||||||
|
NC -> B
|
||||||
|
NB -> B
|
||||||
|
BN -> B
|
||||||
|
BB -> N
|
||||||
|
BC -> B
|
||||||
|
CC -> N
|
||||||
|
CN -> C`,
|
||||||
|
expected: 2188189693529,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
solution: part2,
|
||||||
|
},
|
||||||
|
trimTestInputs: true,
|
||||||
|
})
|
Loading…
Add table
Add a link
Reference in a new issue