/
1-1.ts
74 lines (61 loc) · 2 KB
/
1-1.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import chalk from 'chalk'
import { readFileSync } from 'fs'
import { Combinator, Map } from '../../lib/combinator'
import { Library } from '../../lib/library'
import { Max, Op, Parse, Split, Sum } from '../../lib/op'
import { deriveProgramV2, runProgramV2 } from '../../lib/program-v2'
import { eq } from '../../lib/util'
// Construct intermediate steps to help guide program synthesis
const steps: any[] = []
steps[0] = '1000\r\n2000\r\n3000\r\n\r\n4000\r\n\r\n5000\r\n6000\r\n\r\n7000\r\n8000\r\n9000\r\n\r\n10000'
// STEP 1: `split`
steps[1] = [
'1000\r\n2000\r\n3000',
`4000`,
`5000\r\n6000`,
`7000\r\n8000\r\n9000`,
`10000`
]
// STEP 2: `map(split)`
steps[2] = [
['1000', '2000', '3000'],
['4000'],
['5000', '6000'],
['7000', '8000', '9000'],
['10000'],
]
// STEP 3: `map(map(parse))`
steps[3] = [
[1000, 2000, 3000],
[4000],
[5000, 6000],
[7000, 8000, 9000],
[10000],
]
// STEP 4: `map(sum)`
steps[4] = [6000, 4000, 11000, 24000, 10000]
// STEP 5: `max`
steps[5] = 24000
// Minimal library of functions to use
const ops: Op[] = [Split, Parse, Sum, Max]
const combinators: Combinator[] = [Map]
const library = new Library(ops, combinators)
// Pre-seed the library with augmented types (2 levels deep)
for (let i = 0; i < combinators.length * ops.length * 2; i++)
library.deriveNextOp()
console.log('Library 📚 = ')
console.log(library.toString())
// Training input/output (translated the problem description)
// In this case, no intermediate stepping stones were needed!
const input = steps[0]
const output = steps[5]
// Derive a program that solves the simplified example problem
const program = deriveProgramV2(steps[0], steps.at(-1), library) ?? process.exit(1)
// Run the derived program on the real problem input
const realInput = readFileSync('days/1/1.txt').toString()
const realAnswer = 71124
const answer = runProgramV2(program, realInput)
if (realAnswer !== undefined && eq(answer, realAnswer))
console.log(chalk.bgGreen(' CORRECT ⭐ '))
else
console.log(chalk.bgRed(' WRONG 💩 '))