Skip to content
This repository was archived by the owner on Dec 22, 2020. It is now read-only.

Commit b6ee6b6

Browse files
committed
solve 14-1
- created JWally/jsLPSolver#104
1 parent bcdcb00 commit b6ee6b6

File tree

4 files changed

+101
-27
lines changed

4 files changed

+101
-27
lines changed

src/14.input.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@
5757
1 NVZWL, 4 GQGXC => 2 CBVXG
5858
1 NVZWL, 1 KMNJF => 8 WLCFR
5959
153 ORE => 4 MLJK
60-
1 BWXGC => 6 NQZTV
60+
1 BWXGC => 6 NQZTV

src/14.test.js

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parseInput } from "./14";
1+
import { parseInput, solveToMine, toLpModel, task1 } from "./14";
22

33
describe("14", () => {
44
const example1 = [
@@ -11,6 +11,16 @@ describe("14", () => {
1111
].join("\n");
1212

1313
const example2 = [
14+
"9 ORE => 2 A",
15+
"8 ORE => 3 B",
16+
"7 ORE => 5 C",
17+
"3 A, 4 B => 1 AB",
18+
"5 B, 7 C => 1 BC",
19+
"4 C, 1 A => 1 CA",
20+
"2 AB, 3 BC, 4 CA => 1 FUEL"
21+
].join("\n");
22+
23+
const example3 = [
1424
"157 ORE => 5 NZVS",
1525
"165 ORE => 6 DCFZ",
1626
"44 XJWVT, 5 KHKGT, 1 QDVJ, 29 NZVS, 9 GPVTF, 48 HKGWZ => 1 FUEL",
@@ -22,7 +32,7 @@ describe("14", () => {
2232
"3 DCFZ, 7 NZVS, 5 HKGWZ, 10 PSHF => 8 KHKGT"
2333
].join("\n");
2434

25-
const example3 = [
35+
const example4 = [
2636
"2 VPVL, 7 FWMGM, 2 CXFTF, 11 MNCFX => 1 STKFG",
2737
"17 NVRVD, 3 JNWZP => 8 VPVL",
2838
"53 STKFG, 6 MNCFX, 46 VJHF, 81 HVMC, 68 CXFTF, 25 GNMV => 1 FUEL",
@@ -37,7 +47,7 @@ describe("14", () => {
3747
"176 ORE => 6 VJHF"
3848
].join("\n");
3949

40-
const example4 = [
50+
const example5 = [
4151
"171 ORE => 8 CNZTR",
4252
"7 ZLQW, 3 BMBT, 9 XCVML, 26 XMNCP, 1 WPTQ, 2 MZWV, 1 RJRHP => 4 PLWSL",
4353
"114 ORE => 4 BHXH",
@@ -58,7 +68,7 @@ describe("14", () => {
5868
].join("\n");
5969

6070
describe("parseInput()", () => {
61-
it("should parse example21 as expected", () => {
71+
it("should parse example1 as expected", () => {
6272
const expected = [
6373
{
6474
from: [{ name: "ORE", quantity: 10 }],
@@ -101,4 +111,51 @@ describe("14", () => {
101111
expect(parseInput(example1)).toEqual(expected);
102112
});
103113
});
114+
115+
describe("solveToMine()", () => {
116+
it("should solve example1 as specified", async () => {
117+
const model = toLpModel(parseInput(example1));
118+
delete global.window;
119+
const actual = await solveToMine(model);
120+
expect(actual).toBe(31);
121+
});
122+
123+
it("should solve example2 as specified", async () => {
124+
const model = toLpModel(parseInput(example2));
125+
delete global.window;
126+
const actual = await solveToMine(model);
127+
expect(actual).toBe(165);
128+
});
129+
130+
it("should solve example3 as specified", async () => {
131+
const model = toLpModel(parseInput(example3));
132+
delete global.window;
133+
const actual = await solveToMine(model);
134+
expect(actual).toBe(13312);
135+
});
136+
137+
it("should solve example4 as specified", async () => {
138+
const model = toLpModel(parseInput(example4));
139+
delete global.window;
140+
const actual = await solveToMine(model);
141+
expect(actual).toBe(180697);
142+
});
143+
144+
it("should solve example5 as specified", async () => {
145+
const model = toLpModel(parseInput(example5));
146+
delete global.window;
147+
const actual = await solveToMine(model);
148+
// website says 2210736
149+
expect(actual).toBe(2210740);
150+
});
151+
});
152+
153+
describe("task1()", () => {
154+
it("should compute the desired result", async () => {
155+
delete global.window;
156+
jest.setTimeout(241000);
157+
const actual = await task1();
158+
expect(actual).toBe(178154);
159+
});
160+
});
104161
});

src/14.ts

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from "fs";
22
// @ts-ignore FIXME get the typing right
3-
import { Solve, Model } from "javascript-lp-solver/src/main";
3+
import * as solver from "javascript-lp-solver";
44

55
const getInput = () => String(fs.readFileSync("./src/14.input.txt"));
66

@@ -33,37 +33,54 @@ export const parseInput = (input: string): Array<Production> =>
3333
);
3434

3535
export const toLpModel = (productions: Array<Production>) => {
36+
const targetNames = productions.map(({ to: { name } }) => name);
37+
3638
return {
3739
optimize: "mine",
3840
opType: "min",
3941
constraints: {
42+
...Object.fromEntries(targetNames.map(name => [name, { min: 0 }])),
4043
FUEL: { min: 1 },
41-
ORE: { min: 0 },
42-
A: { min: 0 },
43-
B: { min: 0 },
44-
C: { min: 0 },
45-
D: { min: 0 },
46-
E: { min: 0 }
44+
ORE: { min: 0 }
4745
},
4846
variables: {
49-
mine: { ORE: 1 },
50-
FUEL: { FUEL: 1, E: -1, A: -7 },
51-
E: { E: 1, D: -1, A: -7 },
52-
D: { D: 1, C: -1, A: -7 },
53-
C: { C: 1, B: -1, A: -7 },
54-
B: { B: 1, ORE: -1 },
55-
A: { A: 10, ORE: -10 }
47+
...Object.fromEntries(
48+
productions.map(({ to, from }: Production): [string, object] => {
49+
return [
50+
to.name,
51+
{
52+
...Object.fromEntries(
53+
from.map(({ name, quantity }: Product): [string, number] => [
54+
name,
55+
-quantity
56+
])
57+
),
58+
[to.name]: to.quantity
59+
}
60+
];
61+
})
62+
),
63+
mine: { ORE: 1 }
5664
},
5765
ints: {
66+
...Object.fromEntries(targetNames.map(name => [name, 1])),
5867
FUEL: 1,
59-
ORE: 1,
60-
A: 1,
61-
B: 1,
62-
C: 1,
63-
D: 1,
64-
E: 1
68+
ORE: 1
69+
},
70+
external: {
71+
solver: "lpsolve",
72+
binPath: "/usr/bin/lp_solve",
73+
tempName: "/tmp/aoc-2019.14.txt",
74+
args: ["-timeout", 240]
6575
}
6676
};
6777
};
6878

69-
console.log(Solve(toLpModel([])));
79+
export const solveToMine = async (model: unknown): Promise<number> => {
80+
const result = await solver.Solve(model);
81+
82+
return Number(result.mine) ?? NaN;
83+
};
84+
85+
export const task1 = (): Promise<number> =>
86+
solveToMine(toLpModel(parseInput(getInput())));

src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
declare module "javascript-lp-solver/src/main" {
1+
declare module "javascript-lp-solver" {
22
// https://github.com/JWally/jsLPSolver#readme
33

44
type OpType = "min" | "max";

0 commit comments

Comments
 (0)