Skip to content

Commit 26e41df

Browse files
committed
reorg cont'd
1 parent b2871f8 commit 26e41df

File tree

11 files changed

+97
-137
lines changed

11 files changed

+97
-137
lines changed

2015/day21/day21.rs

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! [Day 21: RPG Simulator 20XX](https://adventofcode.com/2015/day/21)
22
3-
//! [Day 21: RPG Simulator 20XX](https://adventofcode.com/2015/day/21)
43
use std::cmp::{max, min};
54
use std::str::FromStr;
65

@@ -153,41 +152,23 @@ Defense +3 80 0 3
153152
(min_win_cost, max_loose_cost)
154153
}
155154

156-
struct Puzzle {
157-
min_win_cost: i32,
158-
max_loose_cost: i32,
159-
}
155+
fn solve(data: &str) -> (i32, i32) {
156+
let mut boss = Character::new("boss", 0, 0, 0);
160157

161-
impl Puzzle {
162-
const fn new() -> Self {
163-
Self {
164-
min_win_cost: 0,
165-
max_loose_cost: 0,
158+
for line in data.lines() {
159+
let parts: Vec<&str> = line.split(": ").collect();
160+
match parts.first() {
161+
Some(&"Hit Points") => boss.hitpoints = parts[1].parse().unwrap(),
162+
Some(&"Damage") => boss.damage = parts[1].parse().unwrap(),
163+
Some(&"Armor") => boss.armor = parts[1].parse().unwrap(),
164+
_ => {}
166165
}
167166
}
168167

169-
/// Get the puzzle input.
170-
fn solve(&mut self, data: &str) {
171-
let mut boss = Character::new("boss", 0, 0, 0);
172-
173-
for line in data.lines() {
174-
let parts: Vec<&str> = line.split(": ").collect();
175-
match parts.first() {
176-
Some(&"Hit Points") => boss.hitpoints = parts[1].parse().unwrap(),
177-
Some(&"Damage") => boss.damage = parts[1].parse().unwrap(),
178-
Some(&"Armor") => boss.armor = parts[1].parse().unwrap(),
179-
_ => {}
180-
}
181-
}
182-
183-
(self.min_win_cost, self.max_loose_cost) = play(&boss);
184-
}
168+
play(&boss)
185169
}
186170

187171
fn main() {
188-
let args = aoc::parse_args();
189-
let mut puzzle = Puzzle::new();
190-
puzzle.solve(&args.input);
191-
println!("{}", puzzle.min_win_cost);
192-
println!("{}", puzzle.max_loose_cost);
172+
let mut args = aoc::parse_args();
173+
args.run(solve);
193174
}

2019/day13/day13.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/env python3
22
# [Day 13: Care Package](https://adventofcode.com/2019/day/13)
33

4+
import argparse
45
import sys
56
import time
67
from pathlib import Path
7-
import argparse
88

99
sys.path.append(Path(__file__).parent.parent.as_posix())
1010
from intcode.Intcode import Computer # noqa
@@ -124,6 +124,7 @@ def main():
124124

125125
parser = argparse.ArgumentParser()
126126
parser.add_argument("-v", "--verbose", action="store_true")
127+
parser.add_argument("--elapsed", action="store_true")
127128
parser.add_argument("-p", "--play", action="store_true", help="play the game")
128129
parser.add_argument("input", nargs="?", default="input.txt")
129130
args = parser.parse_args()

2019/day25/day25.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ def main():
176176

177177
parser = argparse.ArgumentParser()
178178
parser.add_argument("-v", "--verbose", action="store_true")
179+
parser.add_argument("--elapsed", action="store_true")
179180
parser.add_argument("input", nargs="?", default="input.txt")
180181
args = parser.parse_args()
181182

2023/day10/day10.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from shapely.geometry.polygon import Polygon
1010

1111
parser = argparse.ArgumentParser()
12+
parser.add_argument("--elapsed", action="store_true")
1213
parser.add_argument("--png", action="store_true")
1314
parser.add_argument("-o", "--output", type=Path, default="pipemaze.png")
1415
parser.add_argument("-s", "--scale", type=int, default=5)

2024/day1/day1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const fs = require('fs');
66
// Main function to handle file reading and calculations
77
async function main() {
88
// Default input file or the one provided as a command line argument
9-
const inputFile = process.argv.length === 3 ? process.argv[2] : 'input.txt';
9+
const inputFile = process.argv.length >= 3 ? process.argv[2] : 'input.txt';
1010

1111
let left = [];
1212
let right = [];

2024/day1/day1.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Foundation
33
// Main function to handle file reading and calculations
44
func main() {
55
// Default input file or the one provided as a command line argument
6-
let inputFile = CommandLine.arguments.count == 2 ? CommandLine.arguments[1] : "input.txt"
6+
let inputFile = CommandLine.arguments.count >= 2 ? CommandLine.arguments[1] : "input.txt"
77

88
var left: [Int] = []
99
var right: [Int] = []

2024/day14/day14.rs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,9 @@ struct Puzzle {
1717
}
1818

1919
impl Puzzle {
20-
const fn new() -> Self {
21-
Self {
22-
robots: Vec::new(),
23-
width: 101,
24-
height: 103,
25-
}
26-
}
20+
fn new(data: &str) -> Self {
21+
let mut robots = Vec::new();
2722

28-
/// Get the puzzle input.
29-
fn configure(&mut self, data: &str) {
3023
let re = Regex::new(r"p=(\d+),(\d+) v=(-?\d+),(-?\d+)").unwrap();
3124

3225
for line in data.lines() {
@@ -39,13 +32,14 @@ impl Puzzle {
3932
vy: caps.get(4).unwrap().as_str().parse().unwrap(),
4033
};
4134

42-
self.robots.push(robot);
35+
robots.push(robot);
4336
}
4437

45-
#[cfg(test)]
46-
{
47-
self.width = 11;
48-
self.height = 7;
38+
// #[cfg(not(test))]
39+
Self {
40+
robots,
41+
width: 101,
42+
height: 103,
4943
}
5044
}
5145

@@ -105,23 +99,25 @@ impl Puzzle {
10599
}
106100

107101
fn main() {
108-
let args = aoc::parse_args();
109-
let mut puzzle = Puzzle::new();
110-
puzzle.configure(&args.input);
111-
println!("{}", puzzle.part1());
112-
println!("{}", puzzle.part2());
102+
let mut args = aoc::parse_args();
103+
args.run(|data| {
104+
let puzzle = Puzzle::new(data);
105+
(puzzle.part1(), puzzle.part2())
106+
});
113107
}
114108

115109
/// Test from puzzle input
116110
#[cfg(test)]
117111
mod test {
118112
use super::*;
119113

114+
const TEST_INPUT: &str = include_str!("test.txt");
115+
120116
#[test]
121117
fn test01() {
122-
let mut puzzle = Puzzle::new();
123-
let data = aoc::load_input_data("test.txt");
124-
puzzle.configure(&data);
118+
let mut puzzle = Puzzle::new(TEST_INPUT);
119+
puzzle.width = 11;
120+
puzzle.height = 7;
125121
assert_eq!(puzzle.part1(), 12);
126122
}
127123
}

2024/day5/day5.rs

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

1010
impl Puzzle {
11-
fn new() -> Self {
12-
Self {
13-
ordering_rules: FxHashMap::default(),
14-
page_updates: Vec::new(),
15-
}
16-
}
11+
fn new(data: &str) -> Self {
12+
let mut ordering_rules: FxHashMap<i32, FxHashSet<i32>> = FxHashMap::default();
13+
let mut page_updates = Vec::new();
1714

18-
/// Get the puzzle input.
19-
fn configure(&mut self, data: &str) {
2015
let data = data.split_once("\n\n").unwrap();
2116

2217
//
@@ -26,13 +21,18 @@ impl Puzzle {
2621
let p1 = p1.parse().unwrap();
2722
let p2 = p2.parse().unwrap();
2823

29-
self.ordering_rules.entry(p1).or_default().insert(p2);
24+
ordering_rules.entry(p1).or_default().insert(p2);
3025
}
3126

3227
//
3328
for line in data.1.lines() {
3429
let v: Vec<_> = line.split(',').map(|x| x.parse::<i32>().unwrap()).collect();
35-
self.page_updates.push(v);
30+
page_updates.push(v);
31+
}
32+
33+
Self {
34+
ordering_rules,
35+
page_updates,
3636
}
3737
}
3838

@@ -105,31 +105,29 @@ impl Puzzle {
105105
}
106106

107107
fn main() {
108-
let args = aoc::parse_args();
109-
let mut puzzle = Puzzle::new();
110-
puzzle.configure(&args.input);
111-
println!("{}", puzzle.part1());
112-
println!("{}", puzzle.part2());
108+
let mut args = aoc::parse_args();
109+
args.run(|data| {
110+
let puzzle = Puzzle::new(data);
111+
(puzzle.part1(), puzzle.part2())
112+
});
113113
}
114114

115115
/// Test from puzzle input
116116
#[cfg(test)]
117117
mod test {
118118
use super::*;
119119

120+
const TEST_INPUT: &str = include_str!("test.txt");
121+
120122
#[test]
121123
fn test01() {
122-
let mut puzzle = Puzzle::new();
123-
let data = aoc::load_input_data("test.txt");
124-
puzzle.configure(&data);
124+
let puzzle = Puzzle::new(TEST_INPUT);
125125
assert_eq!(puzzle.part1(), 143);
126126
}
127127

128128
#[test]
129129
fn test02() {
130-
let mut puzzle = Puzzle::new();
131-
let data = aoc::load_input_data("test.txt");
132-
puzzle.configure(&data);
130+
let puzzle = Puzzle::new(TEST_INPUT);
133131
assert_eq!(puzzle.part2(), 123);
134132
}
135133
}

2024/day6/day6.rs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,11 @@ struct Puzzle {
1111
}
1212

1313
impl Puzzle {
14-
fn new() -> Self {
15-
Self {
16-
grid: Grid::default(),
17-
start: Coord::ZERO,
18-
}
19-
}
20-
21-
/// Get the puzzle input.
22-
fn configure(&mut self, data: &str) {
23-
self.grid = Grid::parse(data);
14+
fn new(data: &str) -> Self {
15+
let grid = Grid::parse(data);
16+
let start = grid.iter().find(|(_, c)| c == &&b'^').unwrap().0;
2417

25-
for (xy, p) in &self.grid {
26-
if p == &b'^' {
27-
self.start = xy;
28-
break;
29-
}
30-
}
18+
Self { grid, start }
3119
}
3220

3321
fn move_guard(
@@ -151,31 +139,29 @@ impl Puzzle {
151139
}
152140

153141
fn main() {
154-
let args = aoc::parse_args();
155-
let mut puzzle = Puzzle::new();
156-
puzzle.configure(&args.input);
157-
println!("{}", puzzle.part1());
158-
println!("{}", puzzle.part2());
142+
let mut args = aoc::parse_args();
143+
args.run(|data| {
144+
let puzzle = Puzzle::new(data);
145+
(puzzle.part1(), puzzle.part2())
146+
});
159147
}
160148

161149
/// Test from puzzle input
162150
#[cfg(test)]
163151
mod test {
164152
use super::*;
165153

154+
const TEST_INPUT: &str = include_str!("test.txt");
155+
166156
#[test]
167157
fn test01() {
168-
let mut puzzle = Puzzle::new();
169-
let data = aoc::load_input_data("test.txt");
170-
puzzle.configure(&data);
158+
let puzzle = Puzzle::new(TEST_INPUT);
171159
assert_eq!(puzzle.part1(), 41);
172160
}
173161

174162
#[test]
175163
fn test02() {
176-
let mut puzzle = Puzzle::new();
177-
let data = aoc::load_input_data("test.txt");
178-
puzzle.configure(&data);
164+
let puzzle = Puzzle::new(TEST_INPUT);
179165
assert_eq!(puzzle.part2(), 6);
180166
}
181167
}

0 commit comments

Comments
 (0)