mirror of
https://github.com/seigler/aoc2021
synced 2025-07-26 17:06:09 +00:00
day 13
This commit is contained in:
parent
0a46d00d1e
commit
b0bf232079
4 changed files with 113 additions and 12 deletions
|
@ -181,16 +181,16 @@
|
|||
},
|
||||
{
|
||||
"part1": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"solved": true,
|
||||
"result": "847",
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"time": 3.59
|
||||
},
|
||||
"part2": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"solved": true,
|
||||
"result": "BCZRCEAB",
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"time": 17.85
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
10
README.md
10
README.md
|
@ -23,7 +23,7 @@
|
|||
[](src/day10)
|
||||
[](src/day11)
|
||||
[](src/day12)
|
||||

|
||||
[](src/day13)
|
||||

|
||||

|
||||

|
||||
|
@ -153,9 +153,9 @@ Both parts: 602.51ms
|
|||
|
||||
```
|
||||
Day 13
|
||||
Time part 1: -
|
||||
Time part 1: 3.8ms
|
||||
Time part 2: -
|
||||
Both parts: -
|
||||
Both parts: 3.8ms
|
||||
```
|
||||
|
||||
```
|
||||
|
@ -243,8 +243,8 @@ Both parts: -
|
|||
```
|
||||
|
||||
```
|
||||
Total stars: 24/50
|
||||
Total time: 1056.76ms
|
||||
Total stars: 25/50
|
||||
Total time: 1060.56ms
|
||||
```
|
||||
|
||||
<!--/RESULTS-->
|
||||
|
|
9
src/day13/README.md
Normal file
9
src/day13/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 🎄 Advent of Code 2021 - day 13 🎄
|
||||
|
||||
## Info
|
||||
|
||||
Task description: [link](https://adventofcode.com/2021/day/13)
|
||||
|
||||
## Notes
|
||||
|
||||
...
|
92
src/day13/index.js
Normal file
92
src/day13/index.js
Normal file
|
@ -0,0 +1,92 @@
|
|||
import run from "aocrunner"
|
||||
|
||||
const parseInput = (rawInput) => {
|
||||
const [Sdots, Sfolds] = rawInput.trim().split`\n\n`
|
||||
return [
|
||||
new Set(Sdots.split`\n`),
|
||||
Sfolds.split`\n`.map((x) => x.split(/[ =]/).splice(2)),
|
||||
]
|
||||
}
|
||||
|
||||
function fold(dots, n, axis) {
|
||||
n = +n
|
||||
const nextSet = new Set()
|
||||
dots.forEach((dot) => {
|
||||
const [x, y] = dot.split`,`.map((x) => +x)
|
||||
if (axis == "x") {
|
||||
nextSet.add(x <= n ? dot : `${2 * n - x},${y}`)
|
||||
} else {
|
||||
nextSet.add(y <= n ? dot : `${x},${2 * n - y}`)
|
||||
}
|
||||
})
|
||||
return nextSet
|
||||
}
|
||||
|
||||
const part1 = (rawInput) => {
|
||||
const [dots, folds] = parseInput(rawInput)
|
||||
let currentDots = dots
|
||||
folds.slice(0, 1).forEach(([axis, n]) => {
|
||||
currentDots = fold(currentDots, n, axis)
|
||||
})
|
||||
return currentDots.size
|
||||
}
|
||||
|
||||
const part2 = (rawInput) => {
|
||||
const [dots, folds] = parseInput(rawInput)
|
||||
let currentDots = dots
|
||||
folds.forEach(([axis, n]) => {
|
||||
currentDots = fold(currentDots, n, axis)
|
||||
})
|
||||
const field = [...currentDots].map((d) => d.split`,`.map((x) => +x))
|
||||
const [maxX, maxY] = field.reduce(
|
||||
([maxX, maxY], [x, y]) => [Math.max(x, maxX), Math.max(y, maxY)],
|
||||
[0, 0],
|
||||
)
|
||||
for (let y = 0; y <= maxY; y++) {
|
||||
let line = ""
|
||||
for (let x = 0; x <= maxX; x++) {
|
||||
line += currentDots.has(`${x},${y}`) ? "█" : " "
|
||||
}
|
||||
console.log(line)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
run({
|
||||
part1: {
|
||||
tests: [
|
||||
{
|
||||
input: `6,10
|
||||
0,14
|
||||
9,10
|
||||
0,3
|
||||
10,4
|
||||
4,11
|
||||
6,0
|
||||
6,12
|
||||
4,1
|
||||
0,13
|
||||
10,12
|
||||
3,4
|
||||
3,0
|
||||
8,4
|
||||
1,10
|
||||
2,14
|
||||
8,10
|
||||
9,0
|
||||
|
||||
fold along y=7
|
||||
fold along x=5`,
|
||||
expected: 17,
|
||||
},
|
||||
],
|
||||
solution: part1,
|
||||
},
|
||||
part2: {
|
||||
tests: [
|
||||
// { input: ``, expected: "" },
|
||||
],
|
||||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue