-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.ts
84 lines (72 loc) · 2.28 KB
/
12.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
75
76
77
78
79
80
81
82
83
84
import { getRunsFromIniNewlineSep } from '~/util/util';
import input from './12.txt';
import { timer } from '~/util/Timer';
interface ISimulation {
initial: string[];
notes: Map<string, string>;
startAt: number;
}
const getSimulations = () => getRunsFromIniNewlineSep(input).map(s => ({
initial: s.name.split(''),
notes: new Map(s.content.map(c => c.split(' => ') as [string, string])),
startAt: 0
}));
const makeWillGrowAt = ({initial, notes}: ISimulation) => (index: number) => {
const subset = initial.slice(
Math.max(index - 2, 0),
Math.max(index + 3, 0)
).join('');
if (index < 2)
return notes.get(subset.padStart(5, '.')) === '#';
else
return notes.get(subset.padEnd(5, '.')) === '#';
};
const toNextGeneration = (s: ISimulation) => {
const { initial, startAt, notes } = s;
const next: string[] = [];
const willGrowAt = makeWillGrowAt(s);
const nextStartAt = startAt + (willGrowAt(-2) ? -2 : willGrowAt(-1) ? -1 : 0);
if (willGrowAt(-2))
next.push('#');
if (willGrowAt(-1))
next.push('#');
if (willGrowAt(-2) && !willGrowAt(-1))
next.push('.');
for (let i = 0; i < initial.length; i++) {
next.push(willGrowAt(i) ? '#' : '.');
}
if (willGrowAt(initial.length))
next.push('#');
if (willGrowAt(initial.length + 1)) {
if (!willGrowAt(initial.length))
next.push('.');
next.push('#');
}
return {
notes,
initial: next,
startAt: nextStartAt
};
};
const getScore = (s: ISimulation) =>
s.initial.reduce((sc, p, i) => sc + (p === '#' ? (s.startAt + i) : 0), 0);
const afterNGenerations = (s: ISimulation, n: number) => {
let generation = s;
for (let i = 0; i < n; i++) {
console.log('score', getScore(generation));
generation = toNextGeneration(generation);
}
console.log('score', getScore(generation));
return generation;
};
const print = (s: ISimulation) => {
const lastSim = afterNGenerations(s, 20);
console.log(lastSim.initial.join(''));
console.log(lastSim.startAt);
const score = getScore(s);
console.log(score);
};
export const run = () => {
for (const s of getSimulations().slice(0, 1))
timer.run(print, s.initial.join(''), s);
};