-
Notifications
You must be signed in to change notification settings - Fork 1
/
CalorieCounting.gd
55 lines (42 loc) · 1.02 KB
/
CalorieCounting.gd
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
tool
extends Node2D
func elf_inventories(fname = "input.txt"):
var content = Util.file_content(str("res://src/2022/01/", fname))
content = content.split("\n\n", false)
var tories = []
for x in content:
var cals = []
for y in x.split("\n", false):
cals.append(int(y))
tories.append(cals)
return tories
func total(inventory):
var sum = 0
for i in inventory:
sum += i
return sum
class Sorter:
static func sort_descending(a, b):
if a >= b:
return true
return false
func largest_inventory(inventories, n = 1):
var totals = []
for inv in inventories:
totals.append(total(inv))
totals.sort_custom(Sorter, "sort_descending")
var ret = []
for i in range(n):
ret.append(totals[i])
return total(ret)
func _ready():
if Engine.editor_hint:
request_ready()
# var invs = elf_inventories()
# print(invs)
# var largest = largest_inventory(invs, 3)
# print(largest)
func run_part_one():
return largest_inventory(elf_inventories())
func run_part_two():
return largest_inventory(elf_inventories(), 3)