mirror of
https://github.com/seigler/aoc2022
synced 2025-07-26 06:26:09 +00:00
day 9, this one was fun
This commit is contained in:
parent
fed2b6ec18
commit
44ece06914
4 changed files with 118 additions and 15 deletions
|
@ -117,16 +117,16 @@
|
|||
},
|
||||
{
|
||||
"part1": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"solved": true,
|
||||
"result": "5619",
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"time": 12.601053
|
||||
},
|
||||
"part2": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"solved": true,
|
||||
"result": "2376",
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"time": 15.009934
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
18
README.md
18
README.md
|
@ -19,7 +19,7 @@
|
|||
[](src/day06)
|
||||
[](src/day07)
|
||||
[](src/day08)
|
||||

|
||||
[](src/day09)
|
||||

|
||||

|
||||

|
||||
|
@ -118,16 +118,16 @@ Both parts: 2.9718489999999997ms
|
|||
|
||||
```
|
||||
Day 08
|
||||
Time part 1: 7.541ms
|
||||
Time part 2: 8.005ms
|
||||
Both parts: 15.545933000000002ms
|
||||
Time part 1: 8.863ms
|
||||
Time part 2: 8.313ms
|
||||
Both parts: 17.175474ms
|
||||
```
|
||||
|
||||
```
|
||||
Day 09
|
||||
Time part 1: -
|
||||
Time part 2: -
|
||||
Both parts: -
|
||||
Time part 1: 12.601ms
|
||||
Time part 2: 15.01ms
|
||||
Both parts: 27.610987ms
|
||||
```
|
||||
|
||||
```
|
||||
|
@ -243,8 +243,8 @@ Both parts: -
|
|||
```
|
||||
|
||||
```
|
||||
Total stars: 16/50
|
||||
Total time: 39.454ms
|
||||
Total stars: 18/50
|
||||
Total time: 68.695ms
|
||||
```
|
||||
|
||||
<!--/RESULTS-->
|
||||
|
|
9
src/day09/README.md
Normal file
9
src/day09/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 🎄 Advent of Code 2022 - day 9 🎄
|
||||
|
||||
## Info
|
||||
|
||||
Task description: [link](https://adventofcode.com/2022/day/9)
|
||||
|
||||
## Notes
|
||||
|
||||
...
|
94
src/day09/index.ts
Normal file
94
src/day09/index.ts
Normal file
|
@ -0,0 +1,94 @@
|
|||
import run from "aocrunner"
|
||||
|
||||
type directionType = "R" | "L" | "D" | "U"
|
||||
const parseInput = (rawInput: string) =>
|
||||
rawInput.split("\n").map((x) => x.split(" ") as [directionType, number])
|
||||
|
||||
const deltas = {
|
||||
// row, col
|
||||
R: [0, 1],
|
||||
L: [0, -1],
|
||||
D: [1, 0],
|
||||
U: [-1, 0],
|
||||
}
|
||||
|
||||
const part1 = (rawInput: string) => {
|
||||
const input = parseInput(rawInput)
|
||||
let h = [0, 0]
|
||||
let t = [0, 0]
|
||||
const visited = new Set(["0/0"])
|
||||
|
||||
for (const [direction, steps] of input) {
|
||||
for (let i = 0; i < steps; i++) {
|
||||
h = [h[0] + deltas[direction][0], h[1] + deltas[direction][1]]
|
||||
const dr = t[0] - h[0]
|
||||
const dc = t[1] - h[1]
|
||||
if (Math.abs(dr) > 1 || Math.abs(dc) > 1) {
|
||||
t[0] -= Math.sign(dr)
|
||||
t[1] -= Math.sign(dc)
|
||||
}
|
||||
visited.add(t.join("/"))
|
||||
}
|
||||
}
|
||||
return visited.size
|
||||
}
|
||||
|
||||
const part2 = (rawInput: string) => {
|
||||
const input = parseInput(rawInput)
|
||||
const rope = new Array(10).fill(0).map((x) => [0, 0])
|
||||
const visited = new Set(["0/0"])
|
||||
|
||||
for (const [direction, steps] of input) {
|
||||
for (let i = 0; i < steps; i++) {
|
||||
rope[0][0] += deltas[direction][0]
|
||||
rope[0][1] += deltas[direction][1]
|
||||
for (let s = 1; s < rope.length; s++) {
|
||||
const dr = rope[s][0] - rope[s - 1][0]
|
||||
const dc = rope[s][1] - rope[s - 1][1]
|
||||
if (Math.abs(dr) > 1 || Math.abs(dc) > 1) {
|
||||
rope[s][0] -= Math.sign(dr)
|
||||
rope[s][1] -= Math.sign(dc)
|
||||
}
|
||||
}
|
||||
visited.add(rope.at(-1)!.join("/"))
|
||||
}
|
||||
}
|
||||
return visited.size
|
||||
}
|
||||
|
||||
run({
|
||||
part1: {
|
||||
tests: [
|
||||
{
|
||||
input: `R 4
|
||||
U 4
|
||||
L 3
|
||||
D 1
|
||||
R 4
|
||||
D 1
|
||||
L 5
|
||||
R 2`,
|
||||
expected: 13,
|
||||
},
|
||||
],
|
||||
solution: part1,
|
||||
},
|
||||
part2: {
|
||||
tests: [
|
||||
{
|
||||
input: `R 5
|
||||
U 8
|
||||
L 8
|
||||
D 3
|
||||
R 17
|
||||
D 10
|
||||
L 25
|
||||
U 20`,
|
||||
expected: 36,
|
||||
},
|
||||
],
|
||||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
onlyTests: false,
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue