-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday1.sp
More file actions
85 lines (63 loc) · 1.68 KB
/
day1.sp
File metadata and controls
85 lines (63 loc) · 1.68 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
#include <sourcemod>
#pragma semicolon 1
#pragma newdecls required
#define PLUGIN_VERSION "1337"
public Plugin myinfo =
{
name = "aoc 2022 day 1",
author = "may",
description = "i love kaworu",
version = PLUGIN_VERSION,
url = "Your website URL/AlliedModders profile URL"
};
public void OnPluginStart()
{
RegConsoleCmd("sm_day1", Command_Day1, "");
char dir[PLATFORM_MAX_PATH];
BuildPath(Path_SM, dir, sizeof(dir), "data/aoc");
if (!DirExists(dir)) CreateDirectory(dir, 511);
}
public Action Command_Day1(int client, int args)
{
char path[PLATFORM_MAX_PATH];
BuildPath(Path_SM, path, sizeof(path), "data/aoc/day1.txt");
File file = OpenFile(path, "r");
PrintToChat(client, "Part A: %d\nPart B: %d", partA(file), partB(file));
CloseHandle(file);
}
int partA(File file)
{
int mostCalories, currentCalories;
char buffer[32];
file.Seek(0, 0);
while (file.ReadLine(buffer, sizeof(buffer)))
{
if (!StringToInt(buffer))
{
mostCalories = (currentCalories > mostCalories) ? currentCalories : mostCalories;
currentCalories = 0;
}
else
currentCalories += StringToInt(buffer);
}
return mostCalories;
}
int partB(File file)
{
int currentCalories;
char buffer[32];
ArrayList totalCookies = new ArrayList(1);
file.Seek(0, 0);
while (file.ReadLine(buffer, sizeof(buffer)))
{
if (!StringToInt(buffer))
{
totalCookies.Push(currentCalories);
currentCalories = 0;
}
else
currentCalories += StringToInt(buffer);
}
totalCookies.Sort(Sort_Descending, Sort_Integer);
return totalCookies.Get(0) + totalCookies.Get(1) + totalCookies.Get(2);
}