summaryrefslogtreecommitdiff
path: root/2025/d1.rs
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-12-03 22:51:44 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-12-04 09:08:47 +0800
commit4250e02b0f08e3389fdef0500de08ab74ffa2611 (patch)
tree063b75d1c0eca72bf7419b304df1735e8e6a11bc /2025/d1.rs
parentc8780d06a13dd08bccc1ce29e0b7cd60ff65f259 (diff)
refactor: structure of each day
Diffstat (limited to '2025/d1.rs')
-rw-r--r--2025/d1.rs96
1 files changed, 96 insertions, 0 deletions
diff --git a/2025/d1.rs b/2025/d1.rs
new file mode 100644
index 0000000..48209ef
--- /dev/null
+++ b/2025/d1.rs
@@ -0,0 +1,96 @@
+use aoc::fetch::fetch;
+
+#[derive(Debug)]
+struct Ring {
+ inner: usize,
+ counter: usize,
+}
+
+impl Ring {
+ fn new() -> Self {
+ Self {
+ inner: 50,
+ counter: Default::default(),
+ }
+ }
+
+ const MAX: usize = 99;
+
+ fn left(&mut self, num: usize, count_each_step: bool) {
+ for _ in 0..num {
+ if self.inner == 0 {
+ self.inner = Self::MAX;
+ } else {
+ self.inner -= 1;
+ }
+ if count_each_step {
+ self.check_counter();
+ }
+ }
+ if !count_each_step {
+ self.check_counter();
+ }
+ }
+
+ fn right(&mut self, num: usize, count_each_step: bool) {
+ for _ in 0..num {
+ if self.inner == Self::MAX {
+ self.inner = 0;
+ } else {
+ self.inner += 1;
+ }
+ if count_each_step {
+ self.check_counter();
+ }
+ }
+ if !count_each_step {
+ self.check_counter();
+ }
+ }
+
+ fn check_counter(&mut self) {
+ if self.inner == 0 {
+ self.counter += 1;
+ }
+ }
+}
+
+fn solve(count_each_step: bool) -> usize {
+ let mut ring = Ring::new();
+ let input = fetch(
+ 1, '\n',
+ );
+ for i in input {
+ if i.is_empty() {
+ continue;
+ }
+ let dir = i
+ .chars()
+ .next()
+ .unwrap();
+ let num = i[1..]
+ .parse()
+ .unwrap();
+
+ match dir {
+ 'L' => ring.left(
+ num,
+ count_each_step,
+ ),
+ 'R' => ring.right(
+ num,
+ count_each_step,
+ ),
+ _ => {}
+ }
+ }
+ ring.counter
+}
+
+pub fn part1() -> usize {
+ solve(false)
+}
+
+pub fn part2() -> usize {
+ solve(true)
+}