Skip to content

Commit 82e4275

Browse files
committed
Solved day 22 part 2
1 parent a4574c0 commit 82e4275

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44

55
// project meta data
66
group 'de.havox_design.aoc2015'
7-
version '0.21.3'
7+
version '0.21.4'
88

99
// Switch to gradle "all" distribution.
1010
wrapper {

day22/src/main/java/de/havox_design/aoc2015/day22/RPGState.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
public class RPGState {
77

8+
final boolean hardMode;
9+
810
final RPGPlayer player;
911

1012
final RPGBoss boss;
@@ -13,10 +15,11 @@ public class RPGState {
1315

1416
private final int[] spellDuration;
1517

16-
public RPGState(RPGPlayer player, RPGBoss boss, int[] spellDuration, AtomicInteger best) {
18+
public RPGState(RPGPlayer player, RPGBoss boss, int[] spellDuration, boolean hardMode, AtomicInteger best) {
1719
this.player = player;
1820
this.boss = boss;
1921
this.spellDuration = spellDuration;
22+
this.hardMode = hardMode;
2023
this.best = best;
2124
}
2225

@@ -26,6 +29,29 @@ public RPGState apply(final RPGSpell spell) {
2629
int bossHitPoints = boss.hitPoints();
2730
int bossDamage = boss.damage();
2831

32+
if (hardMode) {
33+
playerHitPoints--;
34+
if (playerHitPoints <= 0) {
35+
return new RPGState
36+
(
37+
new RPGPlayer
38+
(
39+
playerHitPoints,
40+
playerMana,
41+
player.manaSpent() + spell.getCosts()
42+
),
43+
new RPGBoss
44+
(
45+
bossHitPoints,
46+
bossDamage
47+
),
48+
spellDuration,
49+
hardMode,
50+
best
51+
);
52+
}
53+
}
54+
2955
final int[] newSpellDuration = Arrays.copyOf(spellDuration, spellDuration.length);
3056

3157
// players turn
@@ -63,6 +89,7 @@ public RPGState apply(final RPGSpell spell) {
6389
bossDamage
6490
),
6591
newSpellDuration,
92+
hardMode,
6693
best
6794
);
6895
}
@@ -92,6 +119,7 @@ public RPGState apply(final RPGSpell spell) {
92119
bossDamage
93120
),
94121
newSpellDuration,
122+
hardMode,
95123
best
96124
);
97125
}
@@ -112,6 +140,7 @@ public RPGState apply(final RPGSpell spell) {
112140
bossDamage
113141
),
114142
newSpellDuration,
143+
hardMode,
115144
best
116145
);
117146
}

day22/src/main/java/de/havox_design/aoc2015/day22/RPGWizardFight.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@ public static int solvePart2(String fileName) {
2727
}
2828

2929
public int solvePart1() {
30-
return calc();
30+
return calc(false);
3131
}
3232

3333
public int solvePart2() {
34-
return 0;
34+
return calc(true);
3535
}
3636

37-
private Integer calc() {
37+
private Integer calc(boolean hardMode) {
3838
int hitPoints = Integer.parseInt(matchRegex("Hit Points: (\\d+)", input.get(0)).group(1));
3939
int damage = Integer.parseInt(matchRegex("Damage: (\\d+)", input.get(1)).group(1));
4040
RPGBoss boss = new RPGBoss(hitPoints, damage);
4141
RPGPlayer player = new RPGPlayer(50, 500, 0);
4242
AtomicInteger best = new AtomicInteger(Integer.MAX_VALUE);
43-
RPGState state = new RPGState(player, boss, new int[RPGSpell.values().length], best);
43+
RPGState state = new RPGState(player, boss, new int[RPGSpell.values().length], hardMode, best);
4444

4545
Algorithms.breadthFirstSearch(state, this::producer, (x -> {
4646
if (x.getState().player.hitPoints() >= 0 && x.getState().boss.hitPoints() <= 0) {

0 commit comments

Comments
 (0)