Skip to content

Commit 1e3dfea

Browse files
committed
reorg+optim
1 parent d5c9e27 commit 1e3dfea

File tree

40 files changed

+1050
-920
lines changed

40 files changed

+1050
-920
lines changed

2015/day1/day1.rs

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,57 @@
11
//! [Day 1: Not Quite Lisp](https://adventofcode.com/2015/day/1)
22
3-
/// main function
4-
fn main() {
5-
let args = aoc::parse_args();
6-
let data = &args.input;
7-
8-
for line in data.lines() {
9-
let mut floor = 0;
10-
let mut position = 0;
11-
let mut enter = 0;
12-
for c in line.chars() {
13-
position += 1;
14-
match c {
15-
'(' => floor += 1,
16-
')' => floor -= 1,
17-
_ => panic!("invalid character"),
18-
}
19-
if floor == -1 && enter == 0 {
20-
enter = position;
21-
}
3+
/// Solve the puzzle.
4+
fn solve(data: &str) -> (i32, i32) {
5+
let mut floor = 0;
6+
let mut position = 0;
7+
let mut enter = 0;
8+
9+
for c in data.chars() {
10+
position += 1;
11+
match c {
12+
'(' => floor += 1,
13+
')' => floor -= 1,
14+
_ => {}
15+
}
16+
if floor == -1 && enter == 0 {
17+
enter = position;
2218
}
23-
println!("{floor}");
24-
println!("{enter}");
19+
}
20+
21+
(floor, enter)
22+
}
23+
24+
/// Main function.
25+
fn main() {
26+
let mut args = aoc::parse_args();
27+
28+
args.run(solve);
29+
}
30+
31+
#[cfg(test)]
32+
mod test {
33+
use super::*;
34+
35+
#[test]
36+
fn test01() {
37+
assert_eq!(solve("(())").0, 0);
38+
assert_eq!(solve("()()").0, 0);
39+
40+
assert_eq!(solve("(((").0, 3);
41+
assert_eq!(solve("(()(()(").0, 3);
42+
43+
assert_eq!(solve("))(((((").0, 3);
44+
45+
assert_eq!(solve("())").0, -1);
46+
assert_eq!(solve("))(").0, -1);
47+
48+
assert_eq!(solve(")))").0, -3);
49+
assert_eq!(solve(")())())").0, -3);
50+
}
51+
52+
#[test]
53+
fn test02() {
54+
assert_eq!(solve(")").1, 1);
55+
assert_eq!(solve("()())").1, 5);
2556
}
2657
}

2015/day10/day10.rs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,55 @@
22
33
/// main function
44
fn main() {
5-
let args = aoc::parse_args();
6-
let data = args
7-
.input
8-
.lines()
9-
.map(std::string::ToString::to_string)
10-
.collect::<Vec<String>>();
11-
12-
solve(&data[0], 40);
13-
solve(&data[0], 50);
5+
let mut args = aoc::parse_args();
6+
7+
args.run(|data| {
8+
let data = data.trim_ascii();
9+
10+
(solve(data, 40), solve(data, 50))
11+
});
1412
}
1513

16-
fn solve(start_sequence: &str, turns: u32) {
17-
let mut look = start_sequence.chars().collect::<Vec<char>>();
14+
fn solve(start_sequence: &str, turns: u32) -> usize {
15+
let mut look = start_sequence.bytes().collect::<Vec<_>>();
1816

1917
for _ in 0..turns {
20-
let mut say: Vec<char> = Vec::new();
18+
let mut say: Vec<_> = Vec::new();
2119

2220
let mut count = 0;
23-
let mut previous = '\0';
21+
let mut previous = 0;
2422
for current in &look {
25-
if previous != '\0' && previous != *current {
26-
say.extend(count.to_string().chars());
23+
if previous != 0 && previous != *current {
24+
extend(&mut say, count);
2725
say.push(previous);
2826
count = 0;
2927
}
3028
count += 1;
3129
previous = *current;
3230
}
3331

34-
say.extend(count.to_string().chars());
32+
extend(&mut say, count);
3533
say.push(previous);
3634

3735
look.clone_from(&say);
3836
}
39-
println!("{}", look.len());
37+
38+
look.len()
39+
}
40+
41+
fn extend(say: &mut Vec<u8>, num: u32) {
42+
let mut tmp: u32 = num;
43+
let mut base = 1;
44+
loop {
45+
tmp /= 10;
46+
if tmp == 0 {
47+
break;
48+
}
49+
base *= 10;
50+
}
51+
52+
while base != 0 {
53+
say.push(b'0' + ((num / base) % 10) as u8);
54+
base /= 10;
55+
}
4056
}

2015/day2/day2.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
//! [Day 2: I Was Told There Would Be No Math](https://adventofcode.com/2015/day/2)
22
3-
/// main function
4-
fn main() {
5-
let args = aoc::parse_args();
6-
let data = args
7-
.input
8-
.lines()
9-
.map(std::string::ToString::to_string)
10-
.collect::<Vec<String>>();
11-
3+
fn solve(data: &str) -> (u32, u32) {
124
let mut total_paper = 0;
135
let mut total_ribbon = 0;
146

15-
for line in data {
7+
for line in data.lines() {
168
let mut dimensions = line
179
.split('x')
1810
.map(|s| s.parse::<u32>().unwrap())
@@ -47,6 +39,28 @@ fn main() {
4739
total_ribbon += ribbon + bow;
4840
}
4941

50-
println!("{total_paper}");
51-
println!("{total_ribbon}");
42+
(total_paper, total_ribbon)
43+
}
44+
45+
/// main function
46+
fn main() {
47+
let mut args = aoc::parse_args();
48+
args.run(solve);
49+
}
50+
51+
#[cfg(test)]
52+
mod test {
53+
use super::*;
54+
55+
#[test]
56+
fn test01() {
57+
assert_eq!(solve("2x3x4").0, 58);
58+
assert_eq!((solve("1x1x10").0), 43);
59+
}
60+
61+
#[test]
62+
fn test02() {
63+
assert_eq!(solve("2x3x4").1, 34);
64+
assert_eq!((solve("1x1x10").1), 14);
65+
}
5266
}

2015/day2/demo.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

2015/day20/day20.rs

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ struct Puzzle {
55
}
66

77
impl Puzzle {
8-
const fn new() -> Self {
9-
Self { house_present: 0 }
10-
}
11-
12-
fn configure(&mut self, data: &str) {
13-
self.house_present = data.trim_ascii().parse::<usize>().unwrap();
8+
fn new(data: &str) -> Self {
9+
Self {
10+
house_present: data.trim_ascii().parse::<usize>().unwrap(),
11+
}
1412
}
1513

1614
fn part1(&self) -> usize {
@@ -70,96 +68,81 @@ impl Puzzle {
7068
}
7169

7270
fn main() {
73-
let args = aoc::parse_args();
74-
75-
let mut puzzle = Puzzle::new();
76-
77-
puzzle.configure(&args.input);
78-
79-
let result = puzzle.part1();
80-
println!("{result}");
81-
82-
let result = puzzle.part2();
83-
println!("{result}");
71+
let mut args = aoc::parse_args();
72+
args.run(|data| {
73+
let puzzle = Puzzle::new(data);
74+
(puzzle.part1(), puzzle.part2())
75+
});
8476
}
8577

8678
/// Test from puzzle input
8779
#[test]
8880
fn test01() {
89-
let mut puzzle = Puzzle::new();
90-
puzzle.house_present = 10;
81+
let puzzle = Puzzle::new("10");
9182
assert_eq!(puzzle.part1(), 1);
9283
assert_eq!(puzzle.part2(), 1);
9384
}
9485

9586
/// Test from puzzle input
9687
#[test]
9788
fn test02() {
98-
let mut puzzle = Puzzle::new();
99-
puzzle.house_present = 30;
89+
let puzzle = Puzzle::new("30");
10090
assert_eq!(puzzle.part1(), 2);
10191
assert_eq!(puzzle.part2(), 2);
10292
}
10393

10494
/// Test from puzzle input
10595
#[test]
10696
fn test03() {
107-
let mut puzzle = Puzzle::new();
108-
puzzle.house_present = 40;
97+
let puzzle = Puzzle::new("40");
10998
assert_eq!(puzzle.part1(), 3);
11099
assert_eq!(puzzle.part2(), 3);
111100
}
112101

113102
/// Test from puzzle input
114103
#[test]
115104
fn test04() {
116-
let mut puzzle = Puzzle::new();
117-
puzzle.house_present = 60;
105+
let puzzle = Puzzle::new("60");
118106
assert_eq!(puzzle.part1(), 4);
119107
assert_eq!(puzzle.part2(), 4);
120108
}
121109

122110
/// Test from puzzle input
123111
#[test]
124112
fn test05() {
125-
let mut puzzle = Puzzle::new();
126-
puzzle.house_present = 70;
113+
let puzzle = Puzzle::new("70");
127114
assert_eq!(puzzle.part1(), 4);
128115
assert_eq!(puzzle.part2(), 4);
129116
}
130117

131118
/// Test from puzzle input
132119
#[test]
133120
fn test06() {
134-
let mut puzzle = Puzzle::new();
135-
puzzle.house_present = 80;
121+
let puzzle = Puzzle::new("80");
136122
assert_eq!(puzzle.part1(), 6);
137123
assert_eq!(puzzle.part2(), 6);
138124
}
139125

140126
/// Test from puzzle input
141127
#[test]
142128
fn test07() {
143-
let mut puzzle = Puzzle::new();
144-
puzzle.house_present = 120;
129+
let puzzle = Puzzle::new("120");
145130
assert_eq!(puzzle.part1(), 6);
146131
assert_eq!(puzzle.part2(), 6);
147132
}
148133

149134
/// Test from puzzle input
150135
#[test]
151136
fn test08() {
152-
let mut puzzle = Puzzle::new();
153-
puzzle.house_present = 130;
137+
let puzzle = Puzzle::new("130");
154138
assert_eq!(puzzle.part1(), 8);
155139
assert_eq!(puzzle.part2(), 6);
156140
}
157141

158142
/// Test from puzzle input
159143
#[test]
160144
fn test09() {
161-
let mut puzzle = Puzzle::new();
162-
puzzle.house_present = 150;
145+
let puzzle = Puzzle::new("150");
163146
assert_eq!(puzzle.part1(), 8);
164147
assert_eq!(puzzle.part2(), 8);
165148
}

2015/day3/day3.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ use rustc_hash::FxHashSet;
44

55
/// main function
66
fn main() {
7-
let args = aoc::parse_args();
8-
let data = args
9-
.input
10-
.lines()
11-
.map(std::string::ToString::to_string)
12-
.collect::<Vec<String>>();
13-
14-
println!("{}", part1(&data[0]));
15-
println!("{}", part2(&data[0]));
7+
let mut args = aoc::parse_args();
8+
9+
args.run(|data| {
10+
let line = data.trim_ascii();
11+
12+
(part1(line), part2(line))
13+
});
1614
}
1715

1816
fn part2(line: &str) -> usize {

0 commit comments

Comments
 (0)