Browse Source

part 1

main
Yanick Champoux 6 months ago
parent
commit
ceb92bd589
  1. 2000
      2022/09/input
  2. 70
      2022/09/part1.js
  3. 4
      2022/09/part2.js
  4. 272
      2022/09/puzzle.md
  5. 8
      2022/09/sample
  6. 40
      2022/09/test.js

2000
2022/09/input

File diff suppressed because it is too large Load Diff

70
2022/09/part1.js

@ -0,0 +1,70 @@ @@ -0,0 +1,70 @@
import * as R from "remeda";
import Victor from "@a-robu/victor";
import { readFile } from "../05/part1.js";
import { passthru } from '../08/part1.js'
const directions = {
R: new Victor(1,0),
L: new Victor(-1,0),
U: new Victor(0,1),
D: new Victor(0,-1),
}
const parseLine = R.createPipe(
line => line.split(" "),
([direction,times]) => Array(parseInt(times)).fill(directions[direction])
);
const readInput = (...args) => R.pipe(
readFile(...args),
text => text.split("\n"),
R.compact,
R.map( parseLine ),
R.flatten,
)
export const puzzleInput = readInput(import.meta.url, "input");
export const sample = readInput(import.meta.url, "sample");
const touching = (v1,v2) => [v1.distanceX(v2), v1.distanceY(v2)].every(
x => x*x <= 1
);
export function moveOnce( head, tail, d ) {
head.add(d);
if( touching(head,tail) ) return;
const delta = head.clone().subtract(tail).normalize();
if( delta.x * delta.y ) {
delta.x = delta.x > 0 ? 1 : -1;
delta.y = delta.y > 0 ? 1 : -1;
}
tail.add(delta);
}
const moveAround = (directives) => {
const head = new Victor(0,0);
const tail = new Victor(0,0);
const visited = [ tail.toString() ]
for ( const d of directives ) {
moveOnce(head,tail,d);
visited.push( tail.toString() );
}
return visited;
};
export default R.createPipe(
moveAround,
passthru( x => console.log(x) ),
R.uniq,
R.countBy( R.identity )
);

4
2022/09/part2.js

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
import * as R from "remeda";
export default () => {};

272
2022/09/puzzle.md

@ -0,0 +1,272 @@ @@ -0,0 +1,272 @@
\--- Day 9: Rope Bridge ---
----------
This rope bridge creaks as you walk along it. You aren't sure how old it is, or whether it can even support your weight.
It seems to support the Elves just fine, though. The bridge spans a gorge which was carved out by the massive river far below you.
You step carefully; as you do, the ropes stretch and twist. You decide to distract yourself by modeling rope physics; maybe you can even figure out where *not* to step.
Consider a rope with a knot at each end; these knots mark the *head* and the *tail* of the rope. If the head moves far enough away from the tail, the tail is pulled toward the head.
Due to nebulous reasoning involving [Planck lengths](https://en.wikipedia.org/wiki/Planck_units#Planck_length), you should be able to model the positions of the knots on a two-dimensional grid. Then, by following a hypothetical *series of motions* (your puzzle input) for the head, you can determine how the tail will move.
Due to the aforementioned Planck lengths, the rope must be quite short; in fact, the head (`H`) and tail (`T`) must *always be touching* (diagonally adjacent and even overlapping both count as touching):
```
....
.TH.
....
....
.H..
..T.
....
...
.H. (H covers T)
...
```
If the head is ever two steps directly up, down, left, or right from the tail, the tail must also move one step in that direction so it remains close enough:
```
..... ..... .....
.TH.. -> .T.H. -> ..TH.
..... ..... .....
... ... ...
.T. .T. ...
.H. -> ... -> .T.
... .H. .H.
... ... ...
```
Otherwise, if the head and tail aren't touching and aren't in the same row or column, the tail always moves one step diagonally to keep up:
```
..... ..... .....
..... ..H.. ..H..
..H.. -> ..... -> ..T..
.T... .T... .....
..... ..... .....
..... ..... .....
..... ..... .....
..H.. -> ...H. -> ..TH.
.T... .T... .....
..... ..... .....
```
You just need to work out where the tail goes as the head follows a series of motions. Assume the head and the tail both start at the same position, overlapping.
For example:
```
R 4
U 4
L 3
D 1
R 4
D 1
L 5
R 2
```
This series of motions moves the head *right* four steps, then *up* four steps, then *left* three steps, then *down* one step, and so on. After each step, you'll need to update the position of the tail if the step means the head is no longer adjacent to the tail. Visually, these motions occur as follows (`s` marks the starting position as a reference point):
```
== Initial State ==
......
......
......
......
H..... (H covers T, s)
== R 4 ==
......
......
......
......
TH.... (T covers s)
......
......
......
......
sTH...
......
......
......
......
s.TH..
......
......
......
......
s..TH.
== U 4 ==
......
......
......
....H.
s..T..
......
......
....H.
....T.
s.....
......
....H.
....T.
......
s.....
....H.
....T.
......
......
s.....
== L 3 ==
...H..
....T.
......
......
s.....
..HT..
......
......
......
s.....
.HT...
......
......
......
s.....
== D 1 ==
..T...
.H....
......
......
s.....
== R 4 ==
..T...
..H...
......
......
s.....
..T...
...H..
......
......
s.....
......
...TH.
......
......
s.....
......
....TH
......
......
s.....
== D 1 ==
......
....T.
.....H
......
s.....
== L 5 ==
......
....T.
....H.
......
s.....
......
....T.
...H..
......
s.....
......
......
..HT..
......
s.....
......
......
.HT...
......
s.....
......
......
HT....
......
s.....
== R 2 ==
......
......
.H.... (H covers T)
......
s.....
......
......
.TH...
......
s.....
```
After simulating the rope, you can count up all of the positions the *tail visited at least once*. In this diagram, `s` again marks the starting position (which the tail also visited) and `#` marks other positions the tail visited:
```
..##..
...##.
.####.
....#.
s###..
```
So, there are `*13*` positions the tail visited at least once.
Simulate your complete hypothetical series of motions. *How many positions does the tail of the rope visit at least once?*
To begin, [get your puzzle input](9/input).
Answer:
You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=%22Rope+Bridge%22+%2D+Day+9+%2D+Advent+of+Code+2022&url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F9&related=ericwastl&hashtags=AdventOfCode) [Mastodon](javascript:void(0);)] this puzzle.

8
2022/09/sample

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
R 4
U 4
L 3
D 1
R 4
D 1
L 5
R 2

40
2022/09/test.js

@ -0,0 +1,40 @@ @@ -0,0 +1,40 @@
import { test, expect, describe } from "vitest";
import Victor from "@a-robu/victor";
const V = (...args) => new Victor(...args);
import { expectSolution } from "../01/main.js";
import part1, { moveOnce, sample, puzzleInput } from "./part1.js";
import part2 from "./part2.js";
describe("part 1", () => {
test( "moveOnce", () => {
let head = V(1,0);
let tail = V(0,0);
moveOnce(head,tail,V(1,0));
expect(tail).toMatchObject({x:1,y:0});
moveOnce(head,tail,V(0,1));
expect(tail).toMatchObject({x:1,y:0});
moveOnce(head,tail,V(0,1));
expect(tail).toMatchObject({x:2,y:1});
} );
test( "sample", () => {
expect(part1(sample)).toEqual(13);
} )
test("solution", () => {
expectSolution(part1(puzzleInput)).toEqual(6030);
});
});
describe("part 2", () => {
test.todo("solution", () => {
expectSolution(part2(puzzleInput)).toEqual("TODO");
});
});
Loading…
Cancel
Save