Skip to content

Commit af3c378

Browse files
committed
Day 24 - parsing
1 parent bc708e8 commit af3c378

File tree

4 files changed

+137
-1
lines changed

4 files changed

+137
-1
lines changed

day-23-experimental-emergency-teleportation/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const nanobots = require('./nanobots');
88
const BoundingBox = nanobots.BoundingBox;
99
const Point = nanobots.Point;
1010

11-
describe.only('Day 23: Experimental Emergency Teleportation', () => {
11+
describe('Day 23: Experimental Emergency Teleportation', () => {
1212

1313
describe('Part One', () => {
1414
it('should calculate range of nanobots', () => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const enumify = require('enumify');
2+
3+
class AttackType extends enumify.Enum { }
4+
AttackType.initEnum(['SLASHING', 'RADIATION', 'FIRE', 'COLD', 'BLUDGEONING']);
5+
6+
function getAttack(string) {
7+
for (const d of AttackType.enumValues) {
8+
if (d.name.toLowerCase() === string.toLowerCase()) {
9+
return d;
10+
}
11+
}
12+
throw new Error(['Weird type', string]);
13+
}
14+
15+
class Group {
16+
constructor(groupId, units, hitPoints, weaknesses, immunities, attack, attackType, initiative) {
17+
this.id = groupId;
18+
this.units = units;
19+
this.hitPoints = hitPoints;
20+
this.weaknesses = weaknesses;
21+
this.immunities = immunities;
22+
this.attack = attack;
23+
this.attackType = attackType;
24+
this.initiative = initiative;
25+
}
26+
}
27+
28+
function part1(input) {
29+
let [immune, infection] = parseInput(input);
30+
31+
console.log('immune', immune);
32+
console.log('infection', infection);
33+
}
34+
35+
function parseInput(input) {
36+
let lines = input.split(/\r?\n/).map(line => line = line.trim());
37+
let immune = lines.slice(1,lines.indexOf(''));
38+
let infection = lines.slice(lines.indexOf('')+2);
39+
40+
let groupId = 1;
41+
immune = immune.map(line => createGroup(line, groupId++));
42+
groupId = 1;
43+
infection = infection.map(line => createGroup(line, groupId++));
44+
45+
return(immune, infection);
46+
}
47+
48+
function createGroup(line, groupId) {
49+
let result = line.match(/(\d+) units each with (\d+) hit points (\([^)]*\))? with an attack that does (\d+) (\w+) damage at initiative (\d+)/);
50+
let weaknesses = [];
51+
let immunities = [];
52+
if(result[3].length > 2) {
53+
let trimmed = result[3].substring(1, result[3].length-1).split(';');
54+
trimmed.map(part => part.trim()).forEach(part => {
55+
if (part.startsWith('weak to')) {
56+
weaknesses = parseTypes(part.substring(7).trim());
57+
} else if (part.startsWith('immune to')) {
58+
immunities = parseTypes(part.substring(9).trim());
59+
}
60+
});
61+
}
62+
return new Group(groupId, result[1],result[2], weaknesses, immunities, result[4], getAttack(result[5]),result[6]);
63+
}
64+
65+
function parseTypes(typeList) {
66+
return typeList.split(',').map(type => getAttack(type.trim()));
67+
}
68+
69+
module.exports.part1 = part1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Immune System:
2+
2208 units each with 6238 hit points (immune to slashing) with an attack that does 23 bludgeoning damage at initiative 20
3+
7603 units each with 6395 hit points (weak to radiation) with an attack that does 6 cold damage at initiative 15
4+
4859 units each with 5904 hit points (weak to fire) with an attack that does 12 cold damage at initiative 11
5+
1608 units each with 7045 hit points (weak to fire, cold; immune to bludgeoning, radiation) with an attack that does 31 radiation damage at initiative 10
6+
39 units each with 4208 hit points with an attack that does 903 radiation damage at initiative 7
7+
6969 units each with 9562 hit points (immune to slashing, cold) with an attack that does 13 slashing damage at initiative 3
8+
2483 units each with 6054 hit points (immune to fire) with an attack that does 20 cold damage at initiative 19
9+
506 units each with 3336 hit points with an attack that does 64 radiation damage at initiative 6
10+
2260 units each with 10174 hit points (weak to fire) with an attack that does 34 slashing damage at initiative 5
11+
2817 units each with 9549 hit points (immune to cold, fire; weak to bludgeoning) with an attack that does 31 cold damage at initiative 2
12+
13+
Infection:
14+
3650 units each with 25061 hit points (weak to fire, bludgeoning) with an attack that does 11 slashing damage at initiative 12
15+
508 units each with 48731 hit points (weak to bludgeoning) with an attack that does 172 cold damage at initiative 13
16+
724 units each with 27385 hit points with an attack that does 69 radiation damage at initiative 1
17+
188 units each with 41786 hit points with an attack that does 416 bludgeoning damage at initiative 4
18+
3045 units each with 36947 hit points (weak to slashing; immune to fire, bludgeoning) with an attack that does 24 slashing damage at initiative 9
19+
7006 units each with 42545 hit points (immune to cold, slashing, fire) with an attack that does 9 fire damage at initiative 16
20+
853 units each with 55723 hit points (weak to cold, fire) with an attack that does 114 bludgeoning damage at initiative 17
21+
3268 units each with 43027 hit points (immune to slashing, fire) with an attack that does 25 slashing damage at initiative 8
22+
1630 units each with 47273 hit points (weak to cold, bludgeoning) with an attack that does 57 slashing damage at initiative 14
23+
3383 units each with 12238 hit points with an attack that does 7 radiation damage at initiative 18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const expect = require('chai').expect;
2+
const fs = require('fs');
3+
4+
const immuneSystem = require('./immuneSystem');
5+
6+
describe.only('Day 24: Immune System Simulator 20XX', () => {
7+
8+
describe('Part One', () => {
9+
it('should calculate largest area', () => {
10+
const sample =
11+
`Immune System:
12+
17 units each with 5390 hit points (weak to radiation, bludgeoning) with an attack that does 4507 fire damage at initiative 2
13+
989 units each with 1274 hit points (immune to fire; weak to bludgeoning, slashing) with an attack that does 25 slashing damage at initiative 3
14+
15+
Infection:
16+
801 units each with 4706 hit points (weak to radiation) with an attack that does 116 bludgeoning damage at initiative 1
17+
4485 units each with 2961 hit points (immune to radiation; weak to fire, cold) with an attack that does 12 slashing damage at initiative 4`;
18+
expect(immuneSystem.part1(sample)).to.equal(17);
19+
});
20+
21+
it.skip('Input file should return', () => {
22+
const input = fs.readFileSync('day-06-chronal-coordinates/input.txt').toString();
23+
expect(immuneSystem.calculateLargestArea(input)).to.equal(5532);
24+
});
25+
});
26+
27+
describe.skip('Part Two', () => {
28+
it('should calculate largest area', () => {
29+
const coords =
30+
`1, 1
31+
1, 6
32+
8, 3
33+
3, 4
34+
5, 5
35+
8, 9`;
36+
expect(immuneSystem.calculateAreaInDistance(coords,32)).to.equal(16);
37+
});
38+
39+
it('Input file should return', () => {
40+
const input = fs.readFileSync('day-06-chronal-coordinates/input.txt').toString();
41+
expect(immuneSystem.calculateAreaInDistance(input, 10000)).to.equal(36216);
42+
});
43+
});
44+
});

0 commit comments

Comments
 (0)