Skip to content

Commit 86d7d0f

Browse files
markkovariclaude
andauthored
chore/refactor 2015 (#8)
* ci: add tests Signed-off-by: markkovari <kovarimarkofficial@gmail.com> * chore: update files paths Signed-off-by: markkovari <kovarimarkofficial@gmail.com> * chore: run 2015 with nexttest Signed-off-by: markkovari <kovarimarkofficial@gmail.com> * ci: use prebuilt cargo nexttest Signed-off-by: markkovari <kovarimarkofficial@gmail.com> * ci: setup correct action Signed-off-by: markkovari <kovarimarkofficial@gmail.com> * ci: dont fail when error Signed-off-by: markkovari <kovarimarkofficial@gmail.com> * chore: clean up README.md Remove unnecessary content from README 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> --------- Signed-off-by: markkovari <kovarimarkofficial@gmail.com> Co-authored-by: Claude <noreply@anthropic.com>
1 parent cd70d9f commit 86d7d0f

30 files changed

+763
-865
lines changed

.github/workflows/tests.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
name: Advent of Code Tests
22

3-
on: [push, pull_request]
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- 'chore/**'
8+
pull_request:
9+
branches:
10+
- main
411

512
jobs:
613
rust_tests:
714
runs-on: ubuntu-latest
15+
continue-on-error: true
816
strategy:
17+
fail-fast: false
918
matrix:
1019
project:
1120
- 2015
@@ -45,7 +54,9 @@ jobs:
4554

4655
go_tests:
4756
runs-on: ubuntu-latest
57+
continue-on-error: true
4858
strategy:
59+
fail-fast: false
4960
matrix:
5061
project:
5162
- 2017
@@ -77,7 +88,9 @@ jobs:
7788

7889
csharp_tests:
7990
runs-on: ubuntu-latest
91+
continue-on-error: true
8092
strategy:
93+
fail-fast: false
8194
matrix:
8295
project:
8396
- 2021/cs/1

2015/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ iter_tools = "0.1.4"
1010
lazy_static = "1.4.0"
1111
md5 = "0.7.0"
1212
onig = "6.4.0"
13+
rayon = "1.10"
1314
regex = "1.8.1"
1415
serde = "1.0.163"
1516
serde_json = "1.0.96"

2015/src/day01.rs

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,60 @@
1-
use crate::{Excercise, Solvable};
1+
use crate::{Exercise, Solvable};
22

33
struct FirstDay {
4-
exercise: Excercise,
4+
exercise: Exercise,
55
}
66

77
impl Solvable for FirstDay {
8-
fn solve_first(&self, is_prod: bool) -> i32 {
8+
fn solve_first(&self, is_prod: bool) -> i64 {
99
if is_prod {
10-
self.first(self.exercise.content.to_owned())
10+
self.first(&self.exercise.content)
1111
} else {
12-
self.first(self.exercise.example.to_owned())
12+
self.first(&self.exercise.example)
1313
}
1414
}
1515

16-
fn solve_second(&self, is_prod: bool) -> i32 {
16+
fn solve_second(&self, is_prod: bool) -> i64 {
1717
if is_prod {
18-
self.second(self.exercise.content.to_owned())
18+
self.second(&self.exercise.content)
1919
} else {
20-
self.second(self.exercise.example.to_owned())
20+
self.second(&self.exercise.example)
2121
}
2222
}
23-
fn first(&self, content: String) -> i32 {
24-
let mut result: i32 = 0;
25-
for c in content.chars() {
26-
if c == '(' {
27-
result += 1;
28-
} else if c == ')' {
29-
result -= 1;
30-
}
31-
}
32-
result
23+
24+
fn first(&self, content: &str) -> i64 {
25+
content.chars().fold(0, |acc, c| match c {
26+
'(' => acc + 1,
27+
')' => acc - 1,
28+
_ => acc,
29+
})
3330
}
3431

35-
fn second(&self, content: String) -> i32 {
36-
let mut result: i32 = 0;
37-
for (at, c) in content.chars().enumerate() {
38-
if c == '(' {
39-
result += 1;
40-
} else if c == ')' {
41-
result -= 1;
42-
}
43-
if result == -1 {
44-
return (at + 1) as i32;
32+
fn second(&self, content: &str) -> i64 {
33+
let mut floor = 0;
34+
for (pos, c) in content.chars().enumerate() {
35+
floor += match c {
36+
'(' => 1,
37+
')' => -1,
38+
_ => 0,
39+
};
40+
if floor == -1 {
41+
return (pos + 1) as i64;
4542
}
4643
}
47-
return (content.len() + 1) as i32;
44+
(content.len() + 1) as i64
4845
}
4946
}
5047

5148
#[cfg(test)]
5249
mod tests {
5350
use super::*;
54-
const EXAMPLE_1: &str = include_str!("1_test.txt");
55-
const PROD_1: &str = include_str!("1_prod.txt");
51+
const EXAMPLE_1: &str = include_str!("inputs/1_test.txt");
52+
const PROD_1: &str = include_str!("inputs/1_prod.txt");
5653

5754
#[test]
5855
fn first_test() {
59-
let first_excersise = FirstDay {
60-
exercise: Excercise {
56+
let first_exercise = FirstDay {
57+
exercise: Exercise {
6158
content: String::from(PROD_1),
6259
example: String::from(EXAMPLE_1),
6360
},
@@ -66,15 +63,15 @@ mod tests {
6663
let expected_example = 3;
6764
let expected_prod = 232;
6865

69-
let result_example = first_excersise.solve_first(false);
70-
let result_prod = first_excersise.solve_first(true);
66+
let result_example = first_exercise.solve_first(false);
67+
let result_prod = first_exercise.solve_first(true);
7168
assert_eq!(expected_example, result_example);
7269
assert_eq!(expected_prod, result_prod);
7370

7471
let expected_example = 1;
7572
let expected_prod = 1783;
76-
let result_example = first_excersise.solve_second(false);
77-
let result_prod = first_excersise.solve_second(true);
73+
let result_example = first_exercise.solve_second(false);
74+
let result_prod = first_exercise.solve_second(true);
7875
assert_eq!(expected_example, result_example);
7976
assert_eq!(expected_prod, result_prod);
8077
}

2015/src/day02.rs

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
use super::{Excercise, Solvable};
1+
use super::{Exercise, Solvable};
2+
use rayon::prelude::*;
23

34
pub struct SecondDay {
4-
exercise: Excercise,
5+
exercise: Exercise,
56
}
67

7-
fn calculate_wrapper_needed_materials(line: &str) -> (i32, i32) {
8+
/// Calculate wrapping paper and ribbon needed for a present
9+
/// Returns (wrapping_paper_area, ribbon_length)
10+
fn calculate_wrapper_needed_materials(line: &str) -> (i64, i64) {
811
let dimensions = line
912
.split("x")
10-
.map(|x| x.parse::<i32>().unwrap())
11-
.collect::<Vec<i32>>();
13+
.map(|x| x.parse::<i64>().unwrap())
14+
.collect::<Vec<i64>>();
1215
let l = dimensions[0];
1316
let w = dimensions[1];
1417
let h = dimensions[2];
@@ -30,57 +33,49 @@ fn calculate_wrapper_needed_materials(line: &str) -> (i32, i32) {
3033
}
3134

3235
impl Solvable for SecondDay {
33-
fn solve_first(&self, is_prod: bool) -> i32 {
36+
fn solve_first(&self, is_prod: bool) -> i64 {
3437
if is_prod {
35-
self.first(self.exercise.content.to_owned())
38+
self.first(&self.exercise.content)
3639
} else {
37-
self.first(self.exercise.example.to_owned())
40+
self.first(&self.exercise.example)
3841
}
3942
}
4043

41-
fn solve_second(&self, is_prod: bool) -> i32 {
44+
fn solve_second(&self, is_prod: bool) -> i64 {
4245
if is_prod {
43-
self.second(self.exercise.content.to_owned())
46+
self.second(&self.exercise.content)
4447
} else {
45-
self.second(self.exercise.example.to_owned())
48+
self.second(&self.exercise.example)
4649
}
4750
}
4851

49-
fn first(&self, content: String) -> i32 {
50-
let materials = content
51-
.lines()
52+
fn first(&self, content: &str) -> i64 {
53+
content
54+
.par_lines()
5255
.map(calculate_wrapper_needed_materials)
53-
.collect::<Vec<_>>();
54-
let mut wrapper = 0;
55-
for (w, _) in materials {
56-
wrapper += w;
57-
}
58-
wrapper
56+
.map(|(wrapping, _)| wrapping)
57+
.sum()
5958
}
6059

61-
fn second(&self, content: String) -> i32 {
62-
let materials = content
63-
.lines()
60+
fn second(&self, content: &str) -> i64 {
61+
content
62+
.par_lines()
6463
.map(calculate_wrapper_needed_materials)
65-
.collect::<Vec<_>>();
66-
let mut ribbon = 0;
67-
for (_, r) in materials {
68-
ribbon += r;
69-
}
70-
ribbon
64+
.map(|(_, ribbon)| ribbon)
65+
.sum()
7166
}
7267
}
7368

7469
#[cfg(test)]
7570
mod tests {
7671
use super::*;
77-
const EXAMPLE_1: &str = include_str!("2_test.txt");
78-
const PROD_1: &str = include_str!("2_prod.txt");
72+
const EXAMPLE_1: &str = include_str!("inputs/2_test.txt");
73+
const PROD_1: &str = include_str!("inputs/2_prod.txt");
7974

8075
#[test]
8176
fn first_test() {
82-
let first_excersise = SecondDay {
83-
exercise: Excercise {
77+
let first_exercise = SecondDay {
78+
exercise: Exercise {
8479
content: String::from(PROD_1),
8580
example: String::from(EXAMPLE_1),
8681
},
@@ -89,15 +84,15 @@ mod tests {
8984
let expected_example = 58;
9085
let expected_prod = 1588178;
9186

92-
let result_example = first_excersise.solve_first(false);
93-
let result_prod = first_excersise.solve_first(true);
87+
let result_example = first_exercise.solve_first(false);
88+
let result_prod = first_exercise.solve_first(true);
9489
assert_eq!(expected_example, result_example);
9590
assert_eq!(expected_prod, result_prod);
9691

9792
let expected_example = 34;
9893
let expected_prod = 3783758;
99-
let result_example = first_excersise.solve_second(false);
100-
let result_prod = first_excersise.solve_second(true);
94+
let result_example = first_exercise.solve_second(false);
95+
let result_prod = first_exercise.solve_second(true);
10196
assert_eq!(expected_example, result_example);
10297
assert_eq!(expected_prod, result_prod);
10398
}

2015/src/day03.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::HashMap;
22

3-
use crate::{Excercise, Solvable};
3+
use crate::{Exercise, Solvable};
44

55
type Position = (i32, i32);
66

@@ -49,7 +49,7 @@ impl Santa {
4949
}
5050

5151
struct ThirdDay {
52-
exercise: Excercise,
52+
exercise: Exercise,
5353
}
5454

5555
fn get_visited_multiple_times(first: Santa, second: Santa) -> usize {
@@ -64,30 +64,30 @@ fn get_visited_multiple_times(first: Santa, second: Santa) -> usize {
6464
}
6565

6666
impl Solvable for ThirdDay {
67-
fn solve_first(&self, is_prod: bool) -> i32 {
67+
fn solve_first(&self, is_prod: bool) -> i64 {
6868
if is_prod {
69-
self.first(self.exercise.content.to_owned())
69+
self.first(&self.exercise.content)
7070
} else {
71-
self.first(self.exercise.example.to_owned())
71+
self.first(&self.exercise.example)
7272
}
7373
}
7474

75-
fn solve_second(&self, is_prod: bool) -> i32 {
75+
fn solve_second(&self, is_prod: bool) -> i64 {
7676
if is_prod {
77-
self.second(self.exercise.content.to_owned())
77+
self.second(&self.exercise.content)
7878
} else {
79-
self.second(self.exercise.example.to_owned())
79+
self.second(&self.exercise.example)
8080
}
8181
}
82-
fn first(&self, content: String) -> i32 {
82+
fn first(&self, content: &str) -> i64 {
8383
let mut santa = Santa::new();
8484
for direction in content.chars() {
8585
santa.move_to(direction);
8686
}
87-
santa.get_visited_multiple() as i32
87+
santa.get_visited_multiple() as i64
8888
}
8989

90-
fn second(&self, content: String) -> i32 {
90+
fn second(&self, content: &str) -> i64 {
9191
let mut santa = Santa::new();
9292
let mut robo_santa = Santa::new();
9393
for direction in content.chars().enumerate() {
@@ -97,20 +97,20 @@ impl Solvable for ThirdDay {
9797
robo_santa.move_to(direction.1);
9898
}
9999
}
100-
get_visited_multiple_times(santa, robo_santa) as i32
100+
get_visited_multiple_times(santa, robo_santa) as i64
101101
}
102102
}
103103

104104
#[cfg(test)]
105105
mod tests {
106106
use super::*;
107-
const EXAMPLE: &str = include_str!("3_test.txt");
108-
const PROD: &str = include_str!("3_prod.txt");
107+
const EXAMPLE: &str = include_str!("inputs/3_test.txt");
108+
const PROD: &str = include_str!("inputs/3_prod.txt");
109109

110110
#[test]
111111
fn first_test() {
112-
let first_excersise = ThirdDay {
113-
exercise: Excercise {
112+
let first_exercise = ThirdDay {
113+
exercise: Exercise {
114114
content: String::from(PROD),
115115
example: String::from(EXAMPLE),
116116
},
@@ -119,15 +119,15 @@ mod tests {
119119
let expected_example = 4;
120120
let expected_prod = 2572;
121121

122-
let result_example = first_excersise.solve_first(false);
123-
let result_prod = first_excersise.solve_first(true);
122+
let result_example = first_exercise.solve_first(false);
123+
let result_prod = first_exercise.solve_first(true);
124124
assert_eq!(expected_example, result_example);
125125
assert_eq!(expected_prod, result_prod);
126126

127127
let expected_example = 3;
128128
let expected_prod = 2631;
129-
let result_example = first_excersise.solve_second(false);
130-
let result_prod = first_excersise.solve_second(true);
129+
let result_example = first_exercise.solve_second(false);
130+
let result_prod = first_exercise.solve_second(true);
131131
assert_eq!(expected_example, result_example);
132132
assert_eq!(expected_prod, result_prod);
133133
}

0 commit comments

Comments
 (0)