1const std = @import("std");
2
3pub fn pt1(input: []const u8) u64 {
4 var pointer: i64 = 50;
5 var counter: u64 = 0;
6
7 var lines = std.mem.splitScalar(u8, input, '\n');
8
9 while (lines.next()) |l| {
10 if (l.len == 0) {
11 // this happens at the end so we are safe to return here.
12 return counter;
13 }
14
15 const op = l[0];
16 const rotation = std.fmt.parseInt(i64, l[1..], 10) catch undefined;
17
18 if (op == 'R') {
19 pointer = @mod(pointer + rotation, 100);
20 } else {
21 pointer = @mod(pointer - rotation, 100);
22 }
23
24 if (pointer == 0) {
25 counter += 1;
26 }
27 }
28
29 return counter;
30}
31
32
33const operator = enum { left, right };
34pub fn pt2(input: []const u8) u64 {
35 var pointer: i64 = 50;
36 var counter: u64 = 0;
37
38 var lines = std.mem.splitScalar(u8, input, '\n');
39
40 while (lines.next()) |l| {
41 if (l.len == 0) {
42 // this happens at the end so we are safe to return here.
43 return counter;
44 }
45
46 const op = l[0];
47 const rotation = std.fmt.parseInt(i64, l[1..], 10) catch undefined;
48
49 if (op == 'R') {
50 for (0..@intCast(rotation)) |_| {
51 pointer = step(pointer, .right);
52 if (pointer == 0) counter += 1;
53 }
54 } else {
55 for (0..@intCast(rotation)) |_| {
56 pointer = step(pointer, .left);
57 if (pointer == 0) counter += 1;
58 }
59 }
60 }
61
62 return counter;
63}
64
65fn step(p: i64, comptime o: operator) i64 {
66 switch (o) {
67 operator.left => {
68 if (p == 99) return 0;
69 return p + 1;
70 },
71 operator.right => {
72 if (p == 0) return 99;
73 return p - 1;
74 },
75 }
76
77 return p;
78}
79
80test "Day 1 part 1" {
81 const res = pt1(@embedFile("./input/day1"));
82 try std.testing.expect(res == 1165);
83}
84
85
86test "Day 1 part 2" {
87 const res = pt2(@embedFile("./input/day1"));
88 try std.testing.expect(res == 6496);
89}