-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.c3
108 lines (97 loc) · 1.71 KB
/
day2.c3
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
import std::io;
enum Choice
{
ROCK,
PAPER,
SCISSORS
}
enum Result
{
LOSE,
DRAW,
WIN
}
struct Select
{
Choice c;
Result r;
}
fault InvalidData
{
FAILED
}
fn Choice[2]! choicesFromString(String line)
{
if (line.len != 3) return InvalidData.FAILED?;
char c1 = line[0];
if (c1 < 'A' || c1 > 'C') return InvalidData.FAILED?;
char c2 = line[2];
if (c2 < 'X' || c2 > 'Z') return InvalidData.FAILED?;
return { (Choice)(c1 - 'A'), (Choice)(c2 - 'X') };
}
fn Select! selectFromString(String line)
{
if (line.len != 3) return InvalidData.FAILED?;
char c1 = line[0];
if (c1 < 'A' || c1 > 'C') return InvalidData.FAILED?;
char c2 = line[2];
if (c2 < 'X' || c2 > 'Z') return InvalidData.FAILED?;
return { (Choice)(c1 - 'A'), (Result)(c2 - 'X') };
}
fn void part1()
{
File f = file::open("solution.txt", "rb")!!;
defer (void)f.close();
int points = 0;
while (!f.eof())
{
@pool()
{
String line = io::treadline(&f)!!;
Choice[2] c = choicesFromString(line)!!;
points += c[1].ordinal + 1;
// Draw
if (c[0] == c[1])
{
points += 3;
continue;
}
// Loss
if ((c[1].ordinal + 1) % 3 == c[0].ordinal)
{
continue;
}
points += 6;
};
}
io::printfn("The total score is: %d", points);
}
fn void part2()
{
File f = file::open("solution.txt", "rb")!!;
defer (void)f.close();
int points = 0;
while (!f.eof())
{
@pool()
{
String line = io::treadline(&f)!!;
Select s = selectFromString(line)!!;
switch (s.r)
{
case WIN:
points += 6 + 1 + (s.c.ordinal + 1) % 3;
case LOSE:
points += 1 + (s.c.ordinal + 2) % 3;
case DRAW:
points += 4 + s.c.ordinal;
}
};
}
io::printfn("The real score is: %d", points);
}
fn void main()
{
part1();
part2();
}