Skip to content

Commit 3b24b23

Browse files
committed
clippy nursery 🍼
1 parent b42d4e1 commit 3b24b23

File tree

27 files changed

+340
-121
lines changed

27 files changed

+340
-121
lines changed

2015/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

22
[workspace]
33
members = ["day*"]
4-
exclude = ["day21"]
54
resolver = "2"

2015/day10/day10.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ fn solve(start_sequence: &str, turns: u32) {
1616

1717
let mut count = 0;
1818
let mut previous = '\0';
19-
for current in look {
20-
if previous != '\0' && previous != current {
19+
for current in &look {
20+
if previous != '\0' && previous != *current {
2121
say.extend(count.to_string().chars());
2222
say.push(previous);
2323
count = 0;
2424
}
2525
count += 1;
26-
previous = current;
26+
previous = *current;
2727
}
2828

2929
say.extend(count.to_string().chars());
3030
say.push(previous);
3131

32-
look = say.clone();
32+
look.clone_from(&say);
3333
}
3434
println!("{}", look.len());
3535
}

2015/day13/day13.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn calc(names: &HashSet<String>, happiness: &HashMap<(String, String), i32>) ->
88
let perm_names = &mut names.iter().collect::<Vec<&String>>();
99
let permutator = HeapPermutationIterator::new(perm_names);
1010

11-
let mut happiness_max = std::i32::MIN;
11+
let mut happiness_max = i32::MIN;
1212

1313
for permutated in permutator {
1414
let mut happiness_sum = 0;

2015/day19/day19.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ struct Puzzle {
88
}
99

1010
impl Puzzle {
11-
fn new() -> Puzzle {
12-
Puzzle {
11+
const fn new() -> Self {
12+
Self {
1313
replacements: vec![],
1414
medicine_molecule: String::new(),
1515
}
@@ -23,7 +23,7 @@ impl Puzzle {
2323
if let Some((a, b)) = line.split_once(" => ") {
2424
self.replacements.push((a.to_string(), b.to_owned()));
2525
} else if !line.is_empty() {
26-
self.medicine_molecule = line.to_owned();
26+
line.clone_into(&mut self.medicine_molecule);
2727
}
2828
}
2929
}

2015/day20/day20.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ struct Puzzle {
77
}
88

99
impl Puzzle {
10-
fn new() -> Puzzle {
11-
Puzzle { house_present: 0 }
10+
const fn new() -> Self {
11+
Self { house_present: 0 }
1212
}
1313

1414
fn configure(&mut self, filename: &str) {

2015/day21/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "day21"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
aoc = { path = "../../aoc" }
8+
9+
[[bin]]
10+
name = "day21"
11+
path = "day21.rs"

2015/day21/day21.rs

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
//! [Day 21: RPG Simulator 20XX](https://adventofcode.com/2015/day/21)
2+
3+
//! [Day 21: RPG Simulator 20XX](https://adventofcode.com/2015/day/21)
4+
use std::cmp::{max, min};
5+
use std::str::FromStr;
6+
7+
#[derive(Debug, Clone)]
8+
struct Character {
9+
_name: String,
10+
hitpoints: i32,
11+
damage: i32,
12+
armor: i32,
13+
}
14+
15+
impl Character {
16+
fn new(name: &str, hitpoints: i32, damage: i32, armor: i32) -> Self {
17+
Self {
18+
_name: name.to_string(),
19+
hitpoints,
20+
damage,
21+
armor,
22+
}
23+
}
24+
25+
fn attack(&self, enemy: &mut Self) {
26+
let damage = max(0, self.damage - enemy.armor);
27+
enemy.hitpoints -= damage;
28+
29+
if enemy.hitpoints < 0 {
30+
enemy.hitpoints = 0;
31+
}
32+
33+
// Optional: Uncomment for debugging
34+
// println!(
35+
// "The {} deals {}-{} = {} damage; the {} goes down to {} hit points.",
36+
// self.name, self.damage, enemy.armor, damage, enemy.name, enemy.hitpoints
37+
// );
38+
}
39+
}
40+
41+
fn combat(c1: &mut Character, c2: &mut Character) -> i32 {
42+
loop {
43+
c1.attack(c2);
44+
if c2.hitpoints == 0 {
45+
return 1;
46+
}
47+
48+
c2.attack(c1);
49+
if c1.hitpoints == 0 {
50+
return 2;
51+
}
52+
}
53+
}
54+
55+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
56+
struct Item {
57+
cost: i32,
58+
damage: i32,
59+
armor: i32,
60+
}
61+
62+
fn parse_items(input: &str) -> Vec<Item> {
63+
input
64+
.lines()
65+
.filter(|line| !line.trim().is_empty())
66+
.map(|line| {
67+
let parts: Vec<&str> = line.split_whitespace().collect();
68+
let cost = i32::from_str(parts[parts.len() - 3]).unwrap();
69+
let damage = i32::from_str(parts[parts.len() - 2]).unwrap();
70+
let armor = i32::from_str(parts[parts.len() - 1]).unwrap();
71+
Item {
72+
cost,
73+
damage,
74+
armor,
75+
}
76+
})
77+
.collect()
78+
}
79+
80+
fn play(boss: &Character) -> (i32, i32) {
81+
let weapons = parse_items(
82+
r"
83+
Dagger 8 4 0
84+
Shortsword 10 5 0
85+
Warhammer 25 6 0
86+
Longsword 40 7 0
87+
Greataxe 74 8 0
88+
",
89+
);
90+
91+
let mut armors = parse_items(
92+
r"
93+
Leather 13 0 1
94+
Chainmail 31 0 2
95+
Splintmail 53 0 3
96+
Bandedmail 75 0 4
97+
Platemail 102 0 5
98+
",
99+
);
100+
101+
let mut rings = parse_items(
102+
r"
103+
Damage +1 25 1 0
104+
Damage +2 50 2 0
105+
Damage +3 100 3 0
106+
Defense +1 20 0 1
107+
Defense +2 40 0 2
108+
Defense +3 80 0 3
109+
",
110+
);
111+
112+
// Add "no armor" and "no ring" options
113+
armors.push(Item {
114+
cost: 0,
115+
damage: 0,
116+
armor: 0,
117+
});
118+
rings.push(Item {
119+
cost: 0,
120+
damage: 0,
121+
armor: 0,
122+
});
123+
124+
let mut min_win_cost = i32::MAX;
125+
let mut max_loose_cost = 0;
126+
127+
for w in &weapons {
128+
for a in &armors {
129+
for r1 in &rings {
130+
for r2 in &rings {
131+
// Cannot buy two of the same rings
132+
if r1 == r2 {
133+
continue;
134+
}
135+
136+
let cost = w.cost + a.cost + r1.cost + r2.cost;
137+
let damage = w.damage + a.damage + r1.damage + r2.damage;
138+
let armor = w.armor + a.armor + r1.armor + r2.armor;
139+
140+
let mut player = Character::new("player", 100, damage, armor);
141+
let mut boss_copy = boss.clone();
142+
143+
if combat(&mut player, &mut boss_copy) == 1 {
144+
min_win_cost = min(min_win_cost, cost);
145+
} else {
146+
max_loose_cost = max(max_loose_cost, cost);
147+
}
148+
}
149+
}
150+
}
151+
}
152+
153+
(min_win_cost, max_loose_cost)
154+
}
155+
156+
struct Puzzle {
157+
min_win_cost: i32,
158+
max_loose_cost: i32,
159+
}
160+
161+
impl Puzzle {
162+
const fn new() -> Self {
163+
Self {
164+
min_win_cost: 0,
165+
max_loose_cost: 0,
166+
}
167+
}
168+
169+
/// Get the puzzle input.
170+
fn solve(&mut self, path: &str) {
171+
let data = std::fs::read_to_string(path).unwrap_or_else(|_| {
172+
eprintln!("cannot read input file {path}");
173+
std::process::exit(1);
174+
});
175+
176+
let mut boss = Character::new("boss", 0, 0, 0);
177+
178+
for line in data.lines() {
179+
let parts: Vec<&str> = line.split(": ").collect();
180+
match parts.first() {
181+
Some(&"Hit Points") => boss.hitpoints = parts[1].parse().unwrap(),
182+
Some(&"Damage") => boss.damage = parts[1].parse().unwrap(),
183+
Some(&"Armor") => boss.armor = parts[1].parse().unwrap(),
184+
_ => {}
185+
}
186+
}
187+
188+
(self.min_win_cost, self.max_loose_cost) = play(&boss);
189+
}
190+
}
191+
192+
fn main() {
193+
let args = aoc::parse_args();
194+
let mut puzzle = Puzzle::new();
195+
puzzle.solve(args.path.as_str());
196+
println!("{}", puzzle.min_win_cost);
197+
println!("{}", puzzle.max_loose_cost);
198+
}

2015/day22/day22.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct State {
3131
}
3232

3333
impl State {
34-
fn new(boss_dmg: i32, boss_hp: i32, hp: i32, mana: i32) -> Self {
34+
const fn new(boss_dmg: i32, boss_hp: i32, hp: i32, mana: i32) -> Self {
3535
Self {
3636
boss_dmg,
3737
boss_hp,
@@ -149,8 +149,8 @@ struct Puzzle {
149149
}
150150

151151
impl Puzzle {
152-
fn new() -> Puzzle {
153-
Puzzle {
152+
const fn new() -> Self {
153+
Self {
154154
boss_hp: 0,
155155
boss_dmg: 0,
156156
}

2015/day23/day23.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ struct Puzzle {
3333
}
3434

3535
impl Puzzle {
36-
fn new() -> Puzzle {
37-
Puzzle { program: vec![] }
36+
const fn new() -> Self {
37+
Self { program: vec![] }
3838
}
3939

4040
/// Get the puzzle input.

2015/day24/day24.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ struct Puzzle {
77
}
88

99
impl Puzzle {
10-
fn new() -> Puzzle {
11-
Puzzle { packages: vec![] }
10+
const fn new() -> Self {
11+
Self { packages: vec![] }
1212
}
1313

1414
/// Get the puzzle input.

0 commit comments

Comments
 (0)