tangled
alpha
login
or
join now
tymek.me
/
aoc25
1
fork
atom
this repo has no description
1
fork
atom
overview
issues
pulls
pipelines
repo: create structure for next days
Tim Chmielecki
3 months ago
97aedc02
45db0f75
+68
-22
3 changed files
expand all
collapse all
unified
split
src
main.rs
solutions
day1.rs
mod.rs
+4
-22
src/main.rs
···
1
-
use std::fs;
2
3
-
struct Result {
4
-
sum: i32,
5
-
times_crossed: i32,
6
-
}
7
8
fn main() {
9
-
let content = fs::read_to_string("inputs/1.txt").expect("failed to read input file");
10
-
let res = content.lines().fold(
11
-
Result {
12
-
sum: 50,
13
-
times_crossed: 0,
14
-
},
15
-
|mut acc, line| {
16
-
let n: i32 = line[1..].parse().unwrap();
17
-
let old = acc.sum;
18
-
acc.sum += if &line[..1] == "L" { -n } else { n };
19
-
acc.times_crossed += (old > 0 && acc.sum < 0) as i32
20
-
+ if acc.sum == 0 { 1 } else { (acc.sum / 100).abs() };
21
-
acc.sum = acc.sum.rem_euclid(100);
22
-
acc
23
-
},
24
-
);
25
-
println!("result: {}", res.times_crossed);
26
}
···
1
+
mod solutions;
2
3
+
use solutions::Day1;
0
0
0
4
5
fn main() {
6
+
let day1 = Day1::new("inputs/1.txt");
7
+
println!("Part 1: {}, Part 2: {}", day1.part1(), day1.part2());
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
8
}
+62
src/solutions/day1.rs
···
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
···
1
+
use std::fs;
2
+
3
+
pub struct Day1 {
4
+
data: String,
5
+
}
6
+
7
+
struct Result {
8
+
sum: i32,
9
+
times_crossed: i32,
10
+
}
11
+
12
+
impl Day1 {
13
+
pub fn new(path: &str) -> Self {
14
+
Day1 {
15
+
data: fs::read_to_string(path).expect("failed to read input file"),
16
+
}
17
+
}
18
+
19
+
pub fn part1(&self) -> i32 {
20
+
self.data
21
+
.lines()
22
+
.fold(
23
+
Result {
24
+
sum: 50,
25
+
times_crossed: 0,
26
+
},
27
+
|mut acc, line| {
28
+
let n: i32 = line[1..].parse().unwrap();
29
+
acc.sum += if &line[..1] == "L" { -n } else { n };
30
+
acc.sum = acc.sum.rem_euclid(100);
31
+
acc.times_crossed += if acc.sum == 0 { 1 } else { 0 };
32
+
acc
33
+
},
34
+
)
35
+
.times_crossed
36
+
}
37
+
38
+
pub fn part2(&self) -> i32 {
39
+
self.data
40
+
.lines()
41
+
.fold(
42
+
Result {
43
+
sum: 50,
44
+
times_crossed: 0,
45
+
},
46
+
|mut acc, line| {
47
+
let n: i32 = line[1..].parse().unwrap();
48
+
let old = acc.sum;
49
+
acc.sum += if &line[..1] == "L" { -n } else { n };
50
+
acc.times_crossed += (old > 0 && acc.sum < 0) as i32;
51
+
acc.times_crossed += if acc.sum == 0 {
52
+
1
53
+
} else {
54
+
(acc.sum / 100).abs()
55
+
};
56
+
acc.sum = acc.sum.rem_euclid(100);
57
+
acc
58
+
},
59
+
)
60
+
.times_crossed
61
+
}
62
+
}
+2
src/solutions/mod.rs
···
0
0
···
1
+
pub mod day1;
2
+
pub use day1::Day1;