Skip to content

Commit e9f8677

Browse files
committed
Day 12
1 parent e80dd3d commit e9f8677

File tree

4 files changed

+163
-1
lines changed

4 files changed

+163
-1
lines changed

day-11-chronal-change/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const expect = require('chai').expect;
22

33
const fuelcells = require('./fuelcells');
44

5-
describe.only('Day 11: Chronal Charge', () => {
5+
describe('Day 11: Chronal Charge', () => {
66

77
describe('Calculate fuel cell value', () => {
88
it('fuel cell at 3,5 in a grid with serial number 8', () => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
initial state: ###.......##....#.#.#..###.##..##.....#....#.#.....##.###...###.#...###.###.#.###...#.####.##.#....#
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+
.#..# => .
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
class Transformation {
2+
constructor(matcher, result) {
3+
this.matcher = matcher;
4+
this.result = result;
5+
}
6+
}
7+
8+
class State {
9+
constructor(initialState, firstIndex =-2) {
10+
//TODO - optimise and only add if nothing at the end
11+
this.current = initialState;
12+
this.firstIndex = firstIndex + 2;
13+
if (!this.current.startsWith('....')) {
14+
this.current = '....' + this.current;
15+
this.firstIndex = this.firstIndex - 4;
16+
}
17+
18+
if (!this.current.endsWith('....')) {
19+
this.current = this.current + '....';
20+
}
21+
}
22+
23+
evolve(transformations) {
24+
let nextString = '';
25+
//console.log('currLEngth:',this.current.length - 4);
26+
for(let i = 0; i <this.current.length - 4; i++) {
27+
let replacementChar = '.';
28+
transformations.forEach((transformation) => {
29+
if (this.current.substring(i,i+5) === transformation.matcher) {
30+
//console.log(this.current.substring(i,i+5),'matches',transformation.matcher);
31+
replacementChar = transformation.result;
32+
}
33+
});
34+
//console.log(i,'replacementChar',replacementChar);
35+
nextString = nextString + replacementChar;
36+
}
37+
//console.log('next String',nextString);
38+
return new State(nextString, this.firstIndex);
39+
}
40+
41+
calculateSum() {
42+
let sum = 0;
43+
for(let i = 0; i <this.current.length; i++) {
44+
if (this.current.charAt(i) == '#') {
45+
//console.log('Adding',i+this.firstIndex);
46+
sum += i+this.firstIndex;
47+
}
48+
}
49+
return sum;
50+
}
51+
}
52+
53+
function calculateSum(input, iterations = 20) {
54+
let inputLines = input.split(/\r?\n/);
55+
let state = parseInitialState(inputLines.shift());
56+
inputLines.shift();
57+
58+
let transformations = [];
59+
inputLines
60+
.map((line) => {
61+
let result = line.trim().match(/(.+) => (.+)/);
62+
transformations.push(new Transformation(result[1],result[2]));
63+
});
64+
65+
let actualIt = iterations;
66+
if (iterations > 1000) {
67+
iterations = 1000;
68+
}
69+
for(let i=0;i<iterations;i++) {
70+
state = state.evolve(transformations);
71+
}
72+
73+
if (actualIt > 1000) {
74+
let currSum = state.calculateSum();
75+
let diff = state.evolve(transformations).calculateSum() - currSum;
76+
return (actualIt - iterations) * diff + currSum;
77+
}
78+
79+
return state.calculateSum();
80+
}
81+
82+
function parseInitialState(input) {
83+
return new State(input.replace('initial state: ',''));
84+
}
85+
86+
module.exports.calculateSum = calculateSum;
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const expect = require('chai').expect;
2+
const fs = require('fs');
3+
4+
const plants = require('./plants');
5+
6+
describe('Day 12: Subterranean Sustainability', () => {
7+
8+
describe('Part One', () => {
9+
it('should calculate the sum of all pots after 20 iterations', () => {
10+
const input =
11+
`initial state: #..#.#..##......###...###
12+
13+
...## => #
14+
..#.. => #
15+
.#... => #
16+
.#.#. => #
17+
.#.## => #
18+
.##.. => #
19+
.#### => #
20+
#.#.# => #
21+
#.### => #
22+
##.#. => #
23+
##.## => #
24+
###.. => #
25+
###.# => #
26+
####. => #`;
27+
expect(plants.calculateSum(input)).to.equal(325);
28+
});
29+
30+
it('Input file should return after 20 iterations', () => {
31+
const input = fs.readFileSync('day-12-subterranean-sustainability/input.txt').toString();
32+
expect(plants.calculateSum(input)).to.equal(3221);
33+
});
34+
});
35+
36+
describe('Part Two', () => {
37+
it('Input file should return after 50000000000 iterations', () => {
38+
const input = fs.readFileSync('day-12-subterranean-sustainability/input.txt').toString();
39+
expect(plants.calculateSum(input,50000000000)).to.equal(2600000001872);
40+
});
41+
});
42+
});

0 commit comments

Comments
 (0)