tangled
alpha
login
or
join now
bwc9876.dev
/
advent
0
fork
atom
Advent of Code solutions
0
fork
atom
overview
issues
pulls
pipelines
Day 6
bwc9876.dev
3 months ago
db044414
d031c0be
verified
This commit was signed with the committer's
known signature
.
bwc9876.dev
SSH Key Fingerprint:
SHA256:DanMEP/RNlSC7pAVbnXO6wzQV00rqyKj053tz4uH5gQ=
+106
-6
3 changed files
expand all
collapse all
unified
split
years
2025
src
day_6.rs
examples
day_6
1.txt
2.txt
+98
-6
years/2025/src/day_6.rs
···
1
1
-
2
1
use advent_core::{Day, day_stuff, ex_for_day};
3
2
4
3
pub struct Day6;
4
4
+
5
5
+
#[derive(Debug, Clone)]
6
6
+
pub enum Prob {
7
7
+
Add(Vec<usize>),
8
8
+
Mul(Vec<usize>),
9
9
+
}
5
10
6
11
impl Day for Day6 {
12
12
+
day_stuff!(6, "4277556", "3263827");
7
13
8
8
-
day_stuff!(6, "", "");
14
14
+
fn part_1(input: Self::Input) -> Option<String> {
15
15
+
let uno = input.lines().collect::<Vec<_>>();
16
16
+
let dos = uno
17
17
+
.iter()
18
18
+
.take(uno.len() - 1)
19
19
+
.map(|l| {
20
20
+
l.split_whitespace()
21
21
+
.map(|p| p.parse::<usize>().unwrap())
22
22
+
.collect::<Vec<_>>()
23
23
+
})
24
24
+
.collect::<Vec<_>>();
25
25
+
let ops = uno
26
26
+
.iter()
27
27
+
.skip(uno.len() - 1)
28
28
+
.next()
29
29
+
.unwrap()
30
30
+
.split_whitespace()
31
31
+
.collect::<Vec<_>>();
32
32
+
let mut problems = Vec::with_capacity(uno.len());
9
33
10
10
-
fn part_1(_input: Self::Input) -> Option<String> {
11
11
-
None
34
34
+
for i in 0..dos[0].len() {
35
35
+
let all_operands = dos.iter().map(|d| d[i]).collect::<Vec<_>>();
36
36
+
let operator = ops[i];
37
37
+
let prob = if operator == "*" {
38
38
+
Prob::Mul(all_operands)
39
39
+
} else {
40
40
+
Prob::Add(all_operands)
41
41
+
};
42
42
+
43
43
+
problems.push(prob);
44
44
+
}
45
45
+
46
46
+
let ans = problems
47
47
+
.into_iter()
48
48
+
.map(|p| match p {
49
49
+
Prob::Add(l) => l.iter().sum::<usize>(),
50
50
+
Prob::Mul(l) => l.iter().copied().reduce(|a, b| a * b).unwrap(),
51
51
+
})
52
52
+
.sum::<usize>();
53
53
+
54
54
+
Some(ans.to_string())
12
55
}
13
56
14
14
-
fn part_2(_input: Self::Input) -> Option<String> {
15
15
-
None
57
57
+
fn part_2(input: Self::Input) -> Option<String> {
58
58
+
let mut cols: Vec<Vec<_>> = vec![];
59
59
+
60
60
+
for (_, row) in input.lines().enumerate() {
61
61
+
for (j, c) in row.chars().enumerate() {
62
62
+
if let Some(r) = cols.get_mut(j) {
63
63
+
r.push(c);
64
64
+
} else {
65
65
+
cols.push(vec![c]);
66
66
+
}
67
67
+
}
68
68
+
}
69
69
+
70
70
+
let mut curr_val = 0;
71
71
+
let mut curr_op: Option<char> = None;
72
72
+
let mut tot = 0;
73
73
+
74
74
+
for col in cols {
75
75
+
if let Some(op) = curr_op {
76
76
+
if col.iter().all(|c| *c == ' ') {
77
77
+
tot += curr_val;
78
78
+
curr_val = 0_usize;
79
79
+
curr_op = None;
80
80
+
} else {
81
81
+
let num = col
82
82
+
.iter()
83
83
+
.collect::<String>()
84
84
+
.trim()
85
85
+
.parse::<usize>()
86
86
+
.unwrap();
87
87
+
if op == '+' {
88
88
+
curr_val += num;
89
89
+
} else {
90
90
+
curr_val *= num;
91
91
+
}
92
92
+
}
93
93
+
} else {
94
94
+
curr_op = Some(col.last().copied().unwrap());
95
95
+
curr_val = col
96
96
+
.iter()
97
97
+
.take(col.len() - 1)
98
98
+
.collect::<String>()
99
99
+
.trim()
100
100
+
.parse::<usize>()
101
101
+
.unwrap();
102
102
+
}
103
103
+
}
104
104
+
105
105
+
tot += curr_val;
106
106
+
107
107
+
Some(tot.to_string())
16
108
}
17
109
}
+4
years/2025/src/examples/day_6/1.txt
···
1
1
+
123 328 51 64
2
2
+
45 64 387 23
3
3
+
6 98 215 314
4
4
+
* + * +
+4
years/2025/src/examples/day_6/2.txt
···
1
1
+
123 328 51 64
2
2
+
45 64 387 23
3
3
+
6 98 215 314
4
4
+
* + * +