Browse Source

tweaks

main
Yanick Champoux 6 months ago
parent
commit
a262a49037
  1. 10
      2022/01/test.js
  2. 56
      2022/05/Part1.pm
  3. 4
      2022/05/input
  4. 21
      2022/05/part1.js
  5. 91
      2022/05/part1.js.orig
  6. 23
      2022/05/part2.js
  7. 73
      2022/05/puzzle.md
  8. 8
      2022/05/test.js
  9. 8
      2022/05/test.t
  10. 1
      2022/template/part2.js
  11. 9
      2022/template/test.js
  12. 7
      Taskfile.yaml

10
2022/01/test.js

@ -1,14 +1,17 @@ @@ -1,14 +1,17 @@
import * as R from "remeda";
import { test, expect } from "vitest";
import fs from "fs-extra";
import path from 'path';
import { expectSolution } from './main.js';
import path from "path";
import { expectSolution } from "./main.js";
const split = (splitter) => (text) => text.split(splitter);
const sum = R.sumBy(R.identity);
const input = R.pipe(
fs.readFileSync( path.join( path.dirname(import.meta.url), "input").replace('file:',''), "utf8"),
fs.readFileSync(
path.join(path.dirname(import.meta.url), "input").replace("file:", ""),
"utf8"
),
split("\n\n"),
R.map((x) =>
split("\n")(x)
@ -18,7 +21,6 @@ const input = R.pipe( @@ -18,7 +21,6 @@ const input = R.pipe(
R.map(sum)
);
test("part 1", () => {
const maxCalories = R.pipe(input, (calories) => Math.max(...calories));

56
2022/05/Part1.pm

@ -0,0 +1,56 @@ @@ -0,0 +1,56 @@
package Part1;
use 5.36.0;
our @ISA = 'Exporter';
our @EXPORT_OK = qw/ part1 $sample /;
use Path::Tiny;
our $sample = path('./sample')->slurp;
sub parse_heaps($heaps) {
my( $header, @crates ) = reverse split "\n", $heaps;
my @stacks;
while( $header !~ /^\s*$/ ) {
$header =~ s/^(\s+)\d//;
my $n = length $1;
@crates = map { no warnings; substr $_, $n } @crates;
my @stack = grep { /\w/ } map { no warnings; s/(.)//; $1 } @crates;
push @stacks, \@stack;
}
return @stacks;
}
sub parse_commands($text) {
return map { [/\d+/g] } split "\n", $text
}
sub move_stacks($stacks, $commands) {
for my $command ( @$commands ) {
for( 1..$command->[0] ) {
push $stacks->[$command->[2] - 1]->@*,
pop $stacks->[$command->[1]- 1]->@*;
}
}
return @$stacks;
}
sub part1($text) {
my( $heaps, $commands ) = split "\n\n", $text;
my @heaps = parse_heaps($heaps);
my @commands = parse_commands($commands);
my @stacks = move_stacks( \@heaps, \@commands );
return join '', map { pop @$_ } @stacks;
}
1;

4
2022/05/input

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
[C] [Q] [V]
[C] [Q] [V]
[D] [D] [S] [M] [Z]
[G] [P] [W] [M] [C] [G]
[F] [Z] [C] [D] [P] [S] [W]
@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
[G] [B] [V] [R] [L] [N] [G] [P] [F]
[R] [T] [S] [S] [S] [T] [D] [L] [P]
[N] [J] [M] [L] [P] [C] [H] [Z] [R]
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
move 2 from 4 to 6
move 4 from 5 to 3

21
2022/05/part1.js

@ -1,9 +1,14 @@ @@ -1,9 +1,14 @@
import * as R from "remeda";
import { readFile } from "../03/part1.js";
import fs from "fs-extra";
import path from "path";
import { fileURLToPath } from "url";
export const sample = readFile("2022", "05", "sample");
export const puzzleInput = readFile("2022", "05", "input");
export const readFile = (url, file) =>
fs.readFileSync(path.join(fileURLToPath(new URL(".", url)), file), "utf8");
export const sample = readFile(import.meta.url, "sample");
export const puzzleInput = readFile(import.meta.url, "input");
function parseHeaps(text) {
const lines = text.split("\n").filter(R.identity);
@ -54,16 +59,20 @@ function moveStacks([stacks, commands]) { @@ -54,16 +59,20 @@ function moveStacks([stacks, commands]) {
return stacks;
}
const spy = (x) => {
export const spy = (x) => {
console.log(x);
return x;
};
export const parseLines = ([heaps, commands]) => [
parseHeaps(heaps),
parseCommands(commands),
];
export const solutionPart1 = R.createPipe(
(text) => text.split("\n\n"),
([heaps, commands]) => [parseHeaps(heaps), parseCommands(commands)],
parseLines,
moveStacks,
spy,
R.map((x) => x.pop()),
(x) => x.join("")
);

91
2022/05/part1.js.orig

@ -0,0 +1,91 @@ @@ -0,0 +1,91 @@
import * as R from "remeda";
import fs from "fs-extra";
import path from "path";
import { fileURLToPath } from "url";
export const readFile = (url, file) =>
fs.readFileSync(path.join(fileURLToPath(new URL(".", url)), file), "utf8");
export const sample = readFile(import.meta.url, "sample");
export const puzzleInput = readFile(import.meta.url, "input");
function parseHeaps(text) {
const lines = text.split("\n").filter(R.identity);
lines.reverse();
let [header, ...crates] = lines;
const stacks = [];
while (header.trimEnd()) {
header = header.replace(/^(\s+)\d/, (...args) => {
crates = crates.map((c) => c.slice(args[1].length));
const stack = [];
crates = crates.map((l) =>
l.replace(/./, (c) => {
if (c !== " ") stack.push(c);
return "";
})
);
stacks.push(stack);
return "";
});
}
return stacks;
}
function parseCommands(text) {
return text
.split("\n")
.filter(R.identity)
.map((line) => line.match(/\d+/g).map((x) => parseInt(x)));
}
function moveStacks([stacks, commands]) {
for (let [move, from, to] of commands) {
console.log({ move, from, to });
while (move-- > 0) {
stacks[to - 1].push(stacks[from - 1].pop());
}
}
return stacks;
}
<<<<<<< HEAD
const spy = (x) => {
console.log(x);
return x;
};
export const solutionPart1 = R.createPipe(
(text) => text.split("\n\n"),
([heaps, commands]) => [parseHeaps(heaps), parseCommands(commands)],
moveStacks,
spy,
=======
export const spy = (x) => {
console.log(x);
return x;
};
export const parseLines = ([heaps, commands]) => [
parseHeaps(heaps),
parseCommands(commands),
];
export const solutionPart1 = R.createPipe(
(text) => text.split("\n\n"),
parseLines,
moveStacks,
>>>>>>> 2a1e7da (part 1)
R.map((x) => x.pop()),
(x) => x.join("")
);

23
2022/05/part2.js

@ -1 +1,24 @@ @@ -1 +1,24 @@
import * as R from "remeda";
import { parseLines, spy } from "./part1.js";
function moveStacks([stacks, commands]) {
for (let [move, from, to] of commands) {
console.log({ move, from, to });
stacks[to - 1].push(
...stacks[from - 1].splice(stacks[from - 1].length - move)
);
}
return stacks;
}
export default R.createPipe(
(text) => text.split("\n\n"),
parseLines,
moveStacks,
spy,
R.map((x) => x.pop()),
(x) => x.join("")
);

73
2022/05/puzzle.md

@ -70,8 +70,77 @@ The Elves just need to know _which crate will end up on top of each stack_; in t @@ -70,8 +70,77 @@ The Elves just need to know _which crate will end up on top of each stack_; in t
_After the rearrangement procedure completes, what crate ends up on top of each stack?_
To begin, [get your puzzle input](5/input).
Your puzzle answer was `VPCDMSLWJ`.
The first half of this puzzle is complete! It provides one gold star: \*
## \--- Part Two ---
As you watch the crane operator expertly rearrange the crates, you notice the process isn't following your prediction.
Some mud was covering the writing on the side of the crane, and you quickly wipe it away. The crane isn't a CrateMover 9000 - it's a _CrateMover 9001_.
The CrateMover 9001 is notable for many new and exciting features: air conditioning, leather seats, an extra cup holder, and _the ability to pick up and move multiple crates at once_.
Again considering the example above, the crates begin in the same configuration:
```
[D]
[N] [C]
[Z] [M] [P]
1 2 3
```
Moving a single crate from stack 2 to stack 1 behaves the same as before:
```
[D]
[N] [C]
[Z] [M] [P]
1 2 3
```
However, the action of moving three crates from stack 1 to stack 3 means that those three moved crates _stay in the same order_, resulting in this new configuration:
```
[D]
[N]
[C] [Z]
[M] [P]
1 2 3
```
Next, as both crates are moved from stack 2 to stack 1, they _retain their order_ as well:
```
[D]
[N]
[C] [Z]
[M] [P]
1 2 3
```
Finally, a single crate is still moved from stack 1 to stack 2, but now it's crate `C` that gets moved:
```
[D]
[N]
[Z]
[M] [C] [P]
1 2 3
```
In this example, the CrateMover 9001 has put the crates in a totally different order: `*MCD*`.
Before the rearrangement process finishes, update your simulation so that the Elves know where they should stand to be ready to unload the final supplies. _After the rearrangement procedure completes, what crate ends up on top of each stack?_
Answer:
You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=%22Supply+Stacks%22+%2D+Day+5+%2D+Advent+of+Code+2022&url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F5&related=ericwastl&hashtags=AdventOfCode) [Mastodon](<javascript:void(0);>)] this puzzle.
Although it hasn't changed, you can still [get your puzzle input](5/input).
You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=I%27ve+completed+Part+One+of+%22Supply+Stacks%22+%2D+Day+5+%2D+Advent+of+Code+2022&url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F5&related=ericwastl&hashtags=AdventOfCode) [Mastodon](<javascript:void(0);>)] this puzzle.

8
2022/05/test.js

@ -2,19 +2,19 @@ import { test, expect, describe } from "vitest"; @@ -2,19 +2,19 @@ import { test, expect, describe } from "vitest";
import { expectSolution } from "../01/main.js";
import { solutionPart1, puzzleInput, sample } from "./part1.js";
import { solutionPart2 } from "./part2.js";
import part2 from "./part2.js";
describe("part 1", () => {
test("sample", () => {
expect(solutionPart1(sample)).toEqual("CMZ");
});
test.only("solution", () => {
test("solution", () => {
expectSolution(solutionPart1(puzzleInput)).toEqual("VPCDMSLWJ");
});
});
describe("part 2", () => {
test.todo("solution", () => {
expectSolution(solutionPart2(puzzleInput)).toEqual("TODO");
test("solution", () => {
expectSolution(part2(puzzleInput)).toEqual("TPWCGNCCG");
});
});

8
2022/05/test.t

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
use 5.36.0;
use Test2::V0;
use Part1 qw/ part1 $sample /;
is part1($sample) => 'CMZ';

1
2022/template/part2.js

@ -1,2 +1 @@ @@ -1,2 +1 @@
import * as R from "remeda";

9
2022/template/test.js

@ -1,20 +1,17 @@ @@ -1,20 +1,17 @@
import { test, expect, describe } from "vitest";
import { expectSolution } from "../01/main.js";
import {
solutionPart1,
puzzleInput,
} from "./part1.js";
import { solutionPart1, puzzleInput } from "./part1.js";
import { solutionPart2 } from "./part2.js";
describe("part 1", () => {
test.todo("solution", () => {
expectSolution(solutionPart1(puzzleInput)).toEqual('TODO');
expectSolution(solutionPart1(puzzleInput)).toEqual("TODO");
});
});
describe("part 2", () => {
test.todo("solution", () => {
expectSolution(solutionPart2(puzzleInput)).toEqual('TODO');
expectSolution(solutionPart2(puzzleInput)).toEqual("TODO");
});
});

7
Taskfile.yaml

@ -3,6 +3,9 @@ @@ -3,6 +3,9 @@
version: "3"
vars:
YEAR: 2022
DAY:
sh: date '+%d' | perl -pe's/^0//'
GREETING: Hello, World!
tasks:
@ -18,6 +21,10 @@ tasks: @@ -18,6 +21,10 @@ tasks:
cmds:
- npx prettier --write {{.CLI_ARGS | default "." }}
page:
cmds:
- firefox https://adventofcode.com/{{.YEAR}}/day/{{.DAY}}
default:
cmds:
- echo "{{.GREETING}}"

Loading…
Cancel
Save