summaryrefslogtreecommitdiff
path: root/2018/aoc2018-d15.py
blob: 2911a4d13421e13445aab67cdb15208d19a537cc (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#advent of code 2018
#day 15
#part 1 and 2
#oop too hard for scriptkid

import heapq

class creature:
	def __init__(self, pos, team, unitID,health,attack):
		self.team = team;
		self.pos = pos;
		self.isAlive = True;
		self.health = health;
		self.attack = attack;
		self.unitID = unitID;
		
	def __str__(self):
		return f'{self.team} #{self.unitID}: {self.health}/200';
		
	def GetAttacked(self,dmg):
		self.health -= dmg;
		if self.health <= 0:
			self.isAlive = False;
			self.health = 0;
			
	def LookAround(self,enemyset):
		targets = [];
		p0,p1 = self.pos;
		for direction in ReadOrder:
			neighbor = (p0+direction[0],p1+direction[1]);
			if neighbor in enemyset:
				targets.append(neighbor);
		return targets;
	
	def pathfinding(self, enemyset, occupiedset):
		paths = [];
		pathlimit = len(grid);
		queue = [];
		heapq.heappush( queue,(0,[self.pos]) );
		visited = set();
		while queue:
			d, path = heapq.heappop(queue);
			if d > pathlimit:
				return paths;
			if path[-1] in enemyset:
				if pathlimit >= len(path):
					paths.append(path);
					pathlimit = len(path);
					continue;
			if path[-1] in visited: continue;
			visited.add(path[-1]);
			p0,p1 = path[-1];
			for direction in ReadOrder:
				neighbor = (p0+direction[0],p1+direction[1]);
				if grid.get(neighbor,"#") != ".":continue;
				if neighbor in occupiedset: continue;
				heapq.heappush(queue,(d+1,path+[neighbor]));
		return paths;
	
	def MakeMove(self, enemyset,occupiedset):
		PossiblePaths = self.pathfinding(enemyset,occupiedset);
		if len(PossiblePaths) != 0:
			for nextpath in sorted( PossiblePaths, key = lambda node: node[1]):
				self.pos = nextpath[1];
				return True;
				break;
		return False;


def run(ulist,att,part2):
	units = dict();
	u_hp = 200;
	for unit in ulist:
		u_pos, u_team, u_id = unit;
		u_att = 3;
		if u_team == "E": u_att = att;
		units[u_id] =  creature(u_pos,u_team,u_id,u_hp,u_att) ;
	
	TotalHP = dict();
	TotalHP["E"] = 1;
	TotalHP["G"] = 1;
	Round = 0;
	while TotalHP["E"] > 0 and TotalHP["G"] > 0:
		if part2 and len([1 for uid in units if units[uid].isAlive==False and units[uid].team=="E"]) != 0:
			return 0;
		
		for unit_id in sorted(units, key=lambda p: units[p].pos):
			if not units[unit_id].isAlive : continue;
			enemyLocation = dict();
			for Unit in units:
				if units[Unit].team != units[unit_id].team and units[Unit].isAlive:
					enemyLocation[units[Unit].pos] = Unit;
			
			occupied = set([units[Unit].pos for Unit in units if units[Unit].isAlive and units[Unit].team == units[unit_id].team]);
			InRangeTargets = units[unit_id].LookAround(enemyLocation);
			
			if len(InRangeTargets) != 0:
				TargetsOrdered = sorted([enemyLocation[tar] for tar in InRangeTargets], key = lambda en: (units[en].health,units[en].pos));
				targetEnemy = TargetsOrdered[0];
				units[targetEnemy].GetAttacked( units[unit_id].attack );
				continue;
			
			MadeMove = units[unit_id].MakeMove(enemyLocation,occupied);
			if not MadeMove: continue;
			InRangeTargets = units[unit_id].LookAround(enemyLocation);
			if len(InRangeTargets) != 0:
				TargetsOrdered = sorted([enemyLocation[tar] for tar in InRangeTargets], key = lambda en: (units[en].health,units[en].pos));
				targetEnemy = TargetsOrdered[0];
				units[targetEnemy].GetAttacked( units[unit_id].attack );
	
		TotalHP["E"] = sum([units[uid].health for uid in units if units[uid].team=="E"]);
		TotalHP["G"] = sum([units[uid].health for uid in units if units[uid].team=="G"]);
		Round += 1;
		###################
		#optional turn by turn battleground output
		'''
		print();
		locations = dict();
		for unitprint in units:
			if not units[unitprint].isAlive:
				locations[ units[unitprint].pos ] = units[unitprint].team.casefold() ;
			else:
				locations[ units[unitprint].pos ] = units[unitprint].team ; 
			print(units[unitprint]);
			
			
		print("Round",Round);
		for y in range(bgsizeY+1):
			for x in range(bgsizeX+1):
				c = locations.get((y,x),grid[(y,x)]);
				print(c,end="");
			print();
		#input();
		'''
		###################
	#part 2 requires all elves to be alive so it's a different check than in part 1
	if part2 and len([1 for u in units if units[u].isAlive==False and units[u].team=="E"]) != 0:
		return 0;
	else: 
		#"number of full rounds that were completed (not counting the round in which combat ends)"
		elfscore = (Round-1)*sum([units[uid].health for uid in units if units[uid].team=="E"]);
		gobscore = (Round-1)*sum([units[uid].health for uid in units if units[uid].team=="G"]);
		return(max(elfscore,gobscore));


bgsizeY = 0;
bgsizeX = 0;
grid = dict();
unitlist = [];
ReadOrder = [(-1,0),(0,-1),(0,1),(1,0)];
unit_counter = 0;
PuzzleInput = open("15.in","r");
for y,l in enumerate(PuzzleInput):
	l = l.replace("\n","");
	bgsizeY = max(bgsizeY,y);
	for x, c in enumerate(l):
		bgsizeX = max(bgsizeX,x);
		if c == "#":
			grid[(y,x)] = "#";
		elif c == ".":
			grid[(y,x)] = ".";
		else:
			unitlist.append(((y,x),c,unit_counter));
			unit_counter += 1;
			grid[(y,x)] = ".";
PuzzleInput.close();

AttackValue = 3;
p1 = run(unitlist,AttackValue,False);
print("part 1 =", p1);
p2 = 0;
while p2 == 0:
	AttackValue += 1;
	p2 = run(unitlist,AttackValue,True);
print("part 2 =", p2);