This repository was archived by the owner on Aug 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay21.java
More file actions
147 lines (133 loc) · 4.37 KB
/
Copy pathDay21.java
File metadata and controls
147 lines (133 loc) · 4.37 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package aoc2015;
import java.util.ArrayList;
import java.util.List;
public class Day21
{
static class Item
{
String name;
int cost;
int damage;
int armor;
public Item(String name, int cost, int damage, int armor)
{
this.name = name;
this.cost = cost;
this.damage = damage;
this.armor = armor;
}
}
static class Player
{
int maxhp;
int damage;
int armor;
int currenthp;
int goldSpent;
public Player( int maxhp)
{
this.maxhp = maxhp;
this.currenthp = maxhp;
}
void hit( Player p2 )
{
p2.currenthp -= ( this.damage - p2.armor );
}
void reset()
{
resetHP();
this.damage = 0;
this.armor = 0;
this.goldSpent = 0;
}
void resetHP()
{
this.currenthp = this.maxhp;
}
void addItem( Item item )
{
this.damage += item.damage;
this.armor += item.armor;
this.goldSpent += item.cost;
}
}
private static boolean playerWinsFight( Player player, Player boss )
{
while( true )
{
player.hit( boss );
if( boss.currenthp <= 0 )
return true;
boss.hit( player );
if( player.currenthp <= 0 )
return false;
}
}
private static void simulateFights( List<Item> weapons, List<Item> armor, List<Item> rings, Player boss )
{
int minGoldSpentForAWin = Integer.MAX_VALUE;
int maxGoldSpentForALoss = Integer.MIN_VALUE;
Player player = new Player( 100 );
for( Item weapon : weapons )
{
for( int i=-1; i<armor.size(); i++ )
{
for( int j = -1; j<rings.size(); j++ )
{
for( int k = -1; k<rings.size(); k++ )
{
if(j == k && k >= 0 )
continue;
player.addItem( weapon );
if( i >= 0 )
player.addItem( armor.get( i ) );
if( j >= 0 )
player.addItem( rings.get( j ));
if( k >= 0 )
player.addItem( rings.get( k ));
if( playerWinsFight( player, boss))
{
if( player.goldSpent < minGoldSpentForAWin )
minGoldSpentForAWin = player.goldSpent;
}
else
{
if( player.goldSpent > maxGoldSpentForALoss )
maxGoldSpentForALoss = player.goldSpent;
}
player.reset();
boss.resetHP();
}
}
}
}
System.out.println("Min gold for a win - " + minGoldSpentForAWin );
System.out.println("Max gold for a loss - " + maxGoldSpentForALoss );
}
public static void main(String[] args)
{
List<Item> weapons = new ArrayList<>();
weapons.add( new Item("Dagger", 8, 4, 0));
weapons.add( new Item("Shortsword", 10, 5, 0));
weapons.add( new Item("Warhammer", 25, 6, 0));
weapons.add( new Item("Longsword", 40, 7, 0));
weapons.add( new Item("Greataxe", 74, 8, 0));
List<Item> armor = new ArrayList<>();
armor.add( new Item("Leather", 13, 0, 1));
armor.add( new Item("Chainmail", 31, 0, 2));
armor.add( new Item("Splintmail", 53, 0, 3));
armor.add( new Item("Bandedmail", 75, 0, 4));
armor.add( new Item("Platemail", 102, 0, 5));
List<Item> rings = new ArrayList<>();
rings.add( new Item("Damage+1", 25, 1, 0));
rings.add( new Item("Damage+2", 50, 2, 0));
rings.add( new Item("Damage+3", 100, 3, 0));
rings.add( new Item("Damage+1", 20, 0, 1));
rings.add( new Item("Damage+2", 40, 0, 2));
rings.add( new Item("Damage+3", 80, 0, 3));
Player boss = new Player(103);
boss.damage = 9;
boss.armor = 2;
simulateFights( weapons, armor, rings, boss );
}
}