|
| 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; |
0 commit comments