Skip to content

Commit 5052a7a

Browse files
committed
reorg cont'd
1 parent 9eddb53 commit 5052a7a

File tree

246 files changed

+1652
-1510
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

246 files changed

+1652
-1510
lines changed

2015/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# https://adventofcode.com/2015
12

23
[workspace]
34
members = ["day*"]

2015/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Puzzle
2626
[Day 15: Science for Hungry People](https://adventofcode.com/2015/day/15) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2015/day15/day15.rs)
2727
[Day 16: Aunt Sue](https://adventofcode.com/2015/day/16) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2015/day16/day16.rs) [![Python](../scripts/assets/python.png)](../2015/day16/day16.py)
2828
[Day 17: No Such Thing as Too Much](https://adventofcode.com/2015/day/17) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2015/day17/day17.rs)
29-
[Day 18: Like a GIF For Your Yard](https://adventofcode.com/2015/day/18) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2015/day18/day18.rs)
29+
[Day 18: Like a GIF For Your Yard](https://adventofcode.com/2015/day/18) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2015/day18/day18.rs) [🎁](../2015/day18/README.md)
3030
[Day 19: Medicine for Rudolph](https://adventofcode.com/2015/day/19) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2015/day19/day19.rs)
3131
[Day 20: Infinite Elves and Infinite Houses](https://adventofcode.com/2015/day/20) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2015/day20/day20.rs)
3232
[Day 21: RPG Simulator 20XX](https://adventofcode.com/2015/day/21) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2015/day21/day21.rs) [![Python](../scripts/assets/python.png)](../2015/day21/day21.py)

2015/day11/day11.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::fmt;
44

5+
#[derive(Clone)]
56
struct Password {
67
pwd: Vec<char>,
78
loops: usize,
@@ -68,11 +69,12 @@ impl Password {
6869
false
6970
}
7071

71-
fn next_valid(&mut self) {
72+
fn next_valid(&mut self) -> Self {
7273
self.next();
7374
while !self.is_valid() {
7475
self.next();
7576
}
77+
self.clone()
7678
}
7779
}
7880

@@ -88,18 +90,32 @@ impl fmt::Display for Password {
8890
}
8991
}
9092

93+
fn solve(data: &str) -> (Password, Password) {
94+
let mut pwd: Password = Password::new(data.trim_ascii());
95+
96+
(pwd.next_valid(), pwd.next_valid())
97+
}
98+
9199
/// main function
92100
fn main() {
93-
let args = aoc::parse_args();
94-
let data = args.input.trim_ascii();
95-
96-
let mut pwd: Password = Password::new(data);
101+
let mut args = aoc::parse_args();
102+
args.run(solve);
103+
}
97104

98-
// println!("init: {}", pwd);
105+
#[cfg(test)]
106+
mod test {
107+
use super::*;
99108

100-
pwd.next_valid();
101-
println!("{pwd}");
109+
#[test]
110+
fn test1() {
111+
assert_eq!(
112+
format!("{}", Password::new("abcdefgh").next_valid()),
113+
"abcdffaa"
114+
);
102115

103-
pwd.next_valid();
104-
println!("{pwd}");
116+
assert_eq!(
117+
format!("{}", Password::new("ghijklmn").next_valid()),
118+
"ghjaabcc"
119+
);
120+
}
105121
}

2015/day12/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ edition = "2021"
55

66
[dependencies]
77
aoc = { path = "../../aoc" }
8-
regex = "1"
9-
serde_json = "1.0"
8+
regex = "*"
9+
serde_json = "*"
1010

1111
[[bin]]
1212
name = "day12"

2015/day12/day12.rs

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
//! [Day 12: JSAbacusFramework.io](https://adventofcode.com/2015/day/12)
22
33
use regex::Regex;
4+
use serde_json::Value;
45

5-
fn sum(v: &serde_json::Value) -> i32 {
6+
/// main function
7+
fn main() {
8+
let mut args = aoc::parse_args();
9+
args.run(|data| (part1(data), part2(data)));
10+
}
11+
12+
fn part1(data: &str) -> i32 {
13+
let re = Regex::new(r"(\-?\d+)").unwrap();
14+
data.lines()
15+
.map(|line| {
16+
re.find_iter(line)
17+
.map(|m| m.as_str().parse::<i32>().unwrap())
18+
.sum::<i32>()
19+
})
20+
.sum::<i32>()
21+
}
22+
23+
fn sum(v: &Value) -> i32 {
624
match v {
7-
serde_json::Value::Number(n) => n.as_i64().unwrap().try_into().unwrap(),
8-
serde_json::Value::Array(a) => a.iter().map(sum).sum(),
9-
serde_json::Value::Object(o) => {
25+
Value::Number(n) => n.as_i64().unwrap().try_into().unwrap(),
26+
Value::Array(a) => a.iter().map(sum).sum(),
27+
Value::Object(o) => {
1028
// Ignore any object (and all of its children) which has any property with the value "red".
1129
for v in o.values() {
1230
if v.as_str() == Some("red") {
@@ -19,29 +37,35 @@ fn sum(v: &serde_json::Value) -> i32 {
1937
}
2038
}
2139

22-
/// main function
23-
fn main() {
24-
let args = aoc::parse_args();
25-
let data = args
26-
.input
27-
.lines()
28-
.map(std::string::ToString::to_string)
29-
.collect::<Vec<String>>();
30-
31-
// part 1
32-
let re = Regex::new(r"(\-?\d+)").unwrap();
33-
let part1 = &data
34-
.iter()
35-
.map(|line| {
36-
re.find_iter(line)
37-
.map(|m| m.as_str().parse::<i32>().unwrap())
38-
.sum::<i32>()
39-
})
40-
.sum::<i32>();
41-
println!("{part1}");
40+
fn part2(data: &str) -> i32 {
41+
let json: Value = serde_json::from_str(data).expect("JSON was not well-formatted");
42+
sum(&json)
43+
}
4244

43-
// part 2
44-
let json: serde_json::Value =
45-
serde_json::from_str(&data[0]).expect("JSON was not well-formatted");
46-
println!("{}", sum(&json));
45+
#[cfg(test)]
46+
mod test {
47+
use super::*;
48+
49+
#[test]
50+
fn test1() {
51+
assert_eq!(part1("[1,2,3]"), 6);
52+
assert_eq!(part1(r#"{"a":2,"b":4}"#), 6);
53+
54+
assert_eq!(part1("[[[3]]]"), 3);
55+
assert_eq!(part1(r#"{"a":{"b":4},"c":-1}"#), 3);
56+
57+
assert_eq!(part1(r#"{"a":[-1,1]}"#), 0);
58+
assert_eq!(part1(r#"[-1,{"a":1}]"#), 0);
59+
60+
assert_eq!(part1("[]"), 0);
61+
assert_eq!(part1("{}"), 0);
62+
}
63+
64+
#[test]
65+
fn test2() {
66+
assert_eq!(part2("[1,2,3]"), 6);
67+
assert_eq!(part2(r#"[1,{"c":"red","b":2},3]"#), 4);
68+
assert_eq!(part2(r#"{"d":"red","e":[1,2,3,4],"f":5}"#), 0);
69+
assert_eq!(part2(r#"[1,"red",5]"#), 6);
70+
}
4771
}

2015/day13/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ edition = "2021"
55

66
[dependencies]
77
aoc = { path = "../../aoc" }
8-
regex = "1"
9-
permutator = "0.4"
10-
rustc-hash = "2.1.0"
8+
regex = "*"
9+
rustc-hash = "*"
10+
itertools = "*"
1111

1212
[[bin]]
1313
name = "day13"

2015/day13/day13.rs

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,82 @@
11
//! [Day 13: Knights of the Dinner Table](https://adventofcode.com/2015/day/13)
22
3-
use permutator::HeapPermutationIterator;
3+
use itertools::Itertools;
44
use regex::Regex;
55
use rustc_hash::{FxHashMap, FxHashSet};
66

7-
fn calc(names: &FxHashSet<String>, happiness: &FxHashMap<(String, String), i32>) -> i32 {
8-
let perm_names = &mut names.iter().collect::<Vec<&String>>();
9-
let permutator = HeapPermutationIterator::new(perm_names);
7+
fn calc<'a>(names: &FxHashSet<&'a str>, happiness: &FxHashMap<(&'a str, &'a str), i32>) -> i32 {
8+
let length = names.len();
109

11-
let mut happiness_max = i32::MIN;
10+
names
11+
.iter()
12+
.permutations(names.len())
13+
.map(|permutated| {
14+
(0..length)
15+
.map(|i| {
16+
let n1 = permutated[i];
17+
let n2 = permutated[(i + 1) % length];
1218

13-
for permutated in permutator {
14-
let mut happiness_sum = 0;
15-
for i in 0..permutated.len() {
16-
let n1 = permutated[i];
17-
let n2 = permutated[(i + 1) % permutated.len()];
18-
19-
happiness_sum += happiness.get(&(n1.to_string(), n2.to_string())).unwrap();
20-
happiness_sum += happiness.get(&(n2.to_string(), n1.to_string())).unwrap();
21-
}
22-
23-
if happiness_max < happiness_sum {
24-
happiness_max = happiness_sum;
25-
}
26-
}
27-
happiness_max
19+
happiness.get(&(n1, n2)).unwrap() + happiness.get(&(n2, n1)).unwrap()
20+
})
21+
.sum()
22+
})
23+
.max()
24+
.unwrap()
2825
}
2926

30-
/// main function
31-
fn main() {
32-
let args = aoc::parse_args();
33-
let data = args
34-
.input
35-
.lines()
36-
.map(std::string::ToString::to_string)
37-
.collect::<Vec<String>>();
38-
39-
let mut names: FxHashSet<String> = FxHashSet::default();
40-
let mut happiness: FxHashMap<(String, String), i32> = FxHashMap::default();
27+
fn solve<'a>(data: &'a str) -> (i32, i32) {
28+
let mut names: FxHashSet<&'a str> = FxHashSet::default();
29+
let mut happiness: FxHashMap<(&'a str, &'a str), i32> = FxHashMap::default();
4130

4231
let re =
4332
Regex::new(r"^(.+) would (gain|lose) (\d+) happiness units by sitting next to (.+)\.$")
4433
.unwrap();
4534

46-
for line in &data {
35+
for line in data.lines() {
4736
if let Some(op) = re.captures(line) {
48-
names.insert(op[1].to_string());
49-
names.insert(op[4].to_string());
37+
let name = op.get(1).unwrap().as_str();
38+
let neighbor = op.get(4).unwrap().as_str();
39+
40+
names.insert(name);
41+
names.insert(neighbor);
5042

5143
let mut gain: i32 = op[3].parse().unwrap();
52-
if op[2].to_string() == "lose" {
44+
if &op[2] == "lose" {
5345
gain = -gain;
5446
}
5547

56-
happiness.insert((op[1].to_string(), op[4].to_string()), gain);
48+
happiness.insert((name, neighbor), gain);
5749
}
5850
}
5951

6052
// part 1
61-
println!("{}", calc(&names, &happiness));
53+
let part1 = calc(&names, &happiness);
6254

6355
// part 2
6456
for name in &names {
65-
happiness.insert((name.to_string(), "me".to_string()), 0);
66-
happiness.insert(("me".to_string(), name.to_string()), 0);
57+
happiness.insert((name, "me"), 0);
58+
happiness.insert(("me", name), 0);
6759
}
68-
names.insert("me".to_string());
60+
names.insert("me");
61+
let part2 = calc(&names, &happiness);
6962

70-
println!("{}", calc(&names, &happiness));
63+
(part1, part2)
64+
}
65+
66+
/// main function
67+
fn main() {
68+
let mut args = aoc::parse_args();
69+
args.run(solve);
70+
}
71+
72+
#[cfg(test)]
73+
mod test {
74+
use super::*;
75+
76+
const TEST_INPUT: &str = include_str!("test.txt");
77+
78+
#[test]
79+
fn test1() {
80+
assert_eq!(solve(TEST_INPUT).0, 330);
81+
}
7182
}

2015/day13/test.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Alice would gain 54 happiness units by sitting next to Bob.
2+
Alice would lose 79 happiness units by sitting next to Carol.
3+
Alice would lose 2 happiness units by sitting next to David.
4+
Bob would gain 83 happiness units by sitting next to Alice.
5+
Bob would lose 7 happiness units by sitting next to Carol.
6+
Bob would lose 63 happiness units by sitting next to David.
7+
Carol would lose 62 happiness units by sitting next to Alice.
8+
Carol would gain 60 happiness units by sitting next to Bob.
9+
Carol would gain 55 happiness units by sitting next to David.
10+
David would gain 46 happiness units by sitting next to Alice.
11+
David would lose 7 happiness units by sitting next to Bob.
12+
David would gain 41 happiness units by sitting next to Carol.

2015/day14/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
aoc = { path = "../../aoc" }
8-
regex = "1"
8+
regex = "*"
99

1010
[[bin]]
1111
name = "day14"

0 commit comments

Comments
 (0)