mirror of
https://github.com/seigler/aoc2021
synced 2025-07-27 01:16:09 +00:00
day 5
This commit is contained in:
parent
fa9f741f90
commit
7586d315fd
5 changed files with 97 additions and 12 deletions
|
@ -67,16 +67,16 @@
|
|||
},
|
||||
{
|
||||
"part1": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"solved": true,
|
||||
"result": "6283",
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"time": 86.45
|
||||
},
|
||||
"part2": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"solved": true,
|
||||
"result": "18864",
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"time": 131.26
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
12
README.md
12
README.md
|
@ -15,7 +15,7 @@
|
|||
[](src/day02)
|
||||
[](src/day03)
|
||||
[](src/day04)
|
||||

|
||||
[](src/day05)
|
||||

|
||||

|
||||

|
||||
|
@ -97,9 +97,9 @@ Both parts: 75.85ms
|
|||
|
||||
```
|
||||
Day 05
|
||||
Time part 1: -
|
||||
Time part 2: -
|
||||
Both parts: -
|
||||
Time part 1: 86.45ms
|
||||
Time part 2: 131.26ms
|
||||
Both parts: 217.70999999999998ms
|
||||
```
|
||||
|
||||
```
|
||||
|
@ -243,8 +243,8 @@ Both parts: -
|
|||
```
|
||||
|
||||
```
|
||||
Total stars: 8/50
|
||||
Total time: 89ms
|
||||
Total stars: 10/50
|
||||
Total time: 306.71ms
|
||||
```
|
||||
|
||||
<!--/RESULTS-->
|
||||
|
|
9
src/day05/README.md
Normal file
9
src/day05/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 🎄 Advent of Code 2021 - day 5 🎄
|
||||
|
||||
## Info
|
||||
|
||||
Task description: [link](https://adventofcode.com/2021/day/5)
|
||||
|
||||
## Notes
|
||||
|
||||
...
|
67
src/day05/index.js
Normal file
67
src/day05/index.js
Normal file
|
@ -0,0 +1,67 @@
|
|||
import run from "aocrunner"
|
||||
import { sequence } from "../utils/index.js"
|
||||
|
||||
const parseInput = (rawInput) =>
|
||||
rawInput.split`\n`.map((l) => l.split(/ -> |,/).map((x) => +x))
|
||||
|
||||
const part1 = (rawInput) => {
|
||||
const input = parseInput(rawInput)
|
||||
const visited = new Map()
|
||||
input.forEach(([ax, ay, bx, by]) => {
|
||||
const dx = Math.sign(bx - ax)
|
||||
const dy = Math.sign(by - ay)
|
||||
if (dx == 0 || dy == 0) {
|
||||
for (let x = ax, y = ay; x != bx + dx || y != by + dy; x += dx, y += dy) {
|
||||
const key = `${x},${y}`
|
||||
const value = visited.get(key) || 0
|
||||
visited.set(key, value + 1)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return [...visited.values()].filter((x) => x > 1).length
|
||||
}
|
||||
|
||||
const part2 = (rawInput) => {
|
||||
const input = parseInput(rawInput)
|
||||
const visited = new Map()
|
||||
input.forEach(([ax, ay, bx, by]) => {
|
||||
const dx = Math.sign(bx - ax)
|
||||
const dy = Math.sign(by - ay)
|
||||
for (let x = ax, y = ay; x != bx + dx || y != by + dy; x += dx, y += dy) {
|
||||
const key = `${x},${y}`
|
||||
const value = visited.get(key) || 0
|
||||
visited.set(key, value + 1)
|
||||
}
|
||||
})
|
||||
|
||||
return [...visited.values()].filter((x) => x > 1).length
|
||||
}
|
||||
|
||||
run({
|
||||
part1: {
|
||||
tests: [
|
||||
{
|
||||
input: `0,9 -> 5,9
|
||||
8,0 -> 0,8
|
||||
9,4 -> 3,4
|
||||
2,2 -> 2,1
|
||||
7,0 -> 7,4
|
||||
6,4 -> 2,0
|
||||
0,9 -> 2,9
|
||||
3,4 -> 1,4
|
||||
0,0 -> 8,8
|
||||
5,5 -> 8,2`,
|
||||
expected: 5,
|
||||
},
|
||||
],
|
||||
solution: part1,
|
||||
},
|
||||
part2: {
|
||||
tests: [
|
||||
// { input: ``, expected: "" },
|
||||
],
|
||||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
})
|
|
@ -25,3 +25,12 @@
|
|||
export function transpose(m) {
|
||||
return m[0].map((x, i) => m.map((x) => x[i]))
|
||||
}
|
||||
|
||||
export function sequence(a, b) {
|
||||
const s = []
|
||||
const increment = a < b ? 1 : -1
|
||||
for (let i = a; (i += increment); i !== b + increment) {
|
||||
s.push(i)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue