Skip to content

Commit 5ed19f2

Browse files
committed
day25 🔑
1 parent 719f9c6 commit 5ed19f2

File tree

6 files changed

+236
-4
lines changed

6 files changed

+236
-4
lines changed

2024/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Advent of Code in Rust 🦀
22

33
![AoC2024](https://img.shields.io/badge/Advent_of_Code-2024-8A2BE2)
4-
![Stars: 48](https://img.shields.io/badge/Stars-48⭐-blue)
5-
![Rust: 25](https://img.shields.io/badge/Rust-25-cyan?logo=Rust)
6-
![Python: 9](https://img.shields.io/badge/Python-9-cyan?logo=Python)
4+
![Stars: 50](https://img.shields.io/badge/Stars-50⭐-blue)
5+
![Rust: 26](https://img.shields.io/badge/Rust-26-cyan?logo=Rust)
6+
![Python: 10](https://img.shields.io/badge/Python-10-cyan?logo=Python)
77

8-
## 2024 ([Calendar](https://adventofcode.com/2024)) ([Solutions](../2024/)) : 48
8+
## 2024 ([Calendar](https://adventofcode.com/2024)) ([Solutions](../2024/)) : 50
99

1010
Puzzle | Stars | Languages
1111
---------------------------------------------------------------------- | ----- | -----------
@@ -33,3 +33,4 @@ Puzzle | Stars |
3333
[Day 22: Monkey Market](https://adventofcode.com/2024/day/22) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2024/day22/day22.rs)
3434
[Day 23: LAN Party](https://adventofcode.com/2024/day/23) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2024/day23/day23.rs) [![Python](../scripts/assets/python.png)](../2024/day23/day23.py)
3535
[Day 24: Crossed Wires](https://adventofcode.com/2024/day/24) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2024/day24/day24.rs)
36+
[Day 25: Code Chronicle](https://adventofcode.com/2024/day/25) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2024/day25/day25.rs) [![Python](../scripts/assets/python.png)](../2024/day25/day25.py) [![Go](../scripts/assets/go.png)](../2024/day25/day25.go)

2024/day25/Cargo.toml

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

2024/day25/day25.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"strings"
8+
)
9+
10+
func main() {
11+
filename := "input.txt"
12+
if len(os.Args) >= 2 {
13+
filename = os.Args[1]
14+
}
15+
data, err := os.ReadFile(filename)
16+
if err != nil {
17+
log.Fatal(err)
18+
}
19+
20+
var keys, locks [][]int
21+
22+
schematicsList := strings.Split(string(data), "\n\n")
23+
24+
for _, schematics := range schematicsList {
25+
lines := strings.Split(schematics, "\n")
26+
27+
heights := []int{-1, -1, -1, -1, -1}
28+
for _, line := range lines {
29+
if len(line) == 5 {
30+
for x, c := range line {
31+
if c == '#' {
32+
heights[x]++
33+
}
34+
}
35+
}
36+
}
37+
38+
if lines[0] == "#####" {
39+
locks = append(locks, heights)
40+
} else {
41+
keys = append(keys, heights)
42+
}
43+
}
44+
45+
sum := 0
46+
for _, lock := range locks {
47+
for _, key := range keys {
48+
valid := true
49+
for i := range key {
50+
if key[i]+lock[i] > 5 {
51+
valid = false
52+
break
53+
}
54+
}
55+
if valid {
56+
sum++
57+
}
58+
}
59+
}
60+
61+
fmt.Println(sum)
62+
}

2024/day25/day25.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python3
2+
# [Day 25: Code Chronicle](https://adventofcode.com/2024/day/25)
3+
4+
5+
from argparse import ArgumentParser
6+
from pathlib import Path
7+
8+
parser = ArgumentParser()
9+
parser.add_argument("-v", "--verbose", action="store_true")
10+
parser.add_argument("-t", "--test", action="store_true")
11+
parser.add_argument("filename", nargs="?", type=Path, default="input.txt")
12+
args = parser.parse_args()
13+
if args.test:
14+
args.filename = Path("test.txt")
15+
16+
data = args.filename.read_text().strip()
17+
18+
19+
keys = []
20+
locks = []
21+
22+
for schematics in data.split("\n\n"):
23+
24+
schematics = schematics.splitlines()
25+
assert len(schematics) == 7
26+
27+
heights = [-1, -1, -1, -1, -1]
28+
for line in schematics:
29+
assert len(line) == 5
30+
for x, c in enumerate(line):
31+
if c == "#":
32+
heights[x] += 1
33+
34+
if schematics[0] == "#####":
35+
locks.append(heights)
36+
elif schematics[6] == "#####":
37+
keys.append(heights)
38+
else:
39+
assert False
40+
41+
42+
print(sum(all(a + b <= 5 for (a, b) in zip(key, lock)) for lock in locks for key in keys))

2024/day25/day25.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//! [Day 25: Code Chronicle](https://adventofcode.com/2024/day/25)
2+
3+
struct Puzzle {
4+
data: String,
5+
}
6+
7+
impl Puzzle {
8+
const fn new() -> Self {
9+
Self {
10+
data: String::new(),
11+
}
12+
}
13+
14+
/// Get the puzzle input.
15+
fn configure(&mut self, path: &str) {
16+
self.data = std::fs::read_to_string(path).unwrap_or_else(|_| {
17+
eprintln!("cannot read input file {path}");
18+
std::process::exit(1);
19+
});
20+
}
21+
22+
/// Solve part one.
23+
fn part1(&self) -> u32 {
24+
let mut locks = Vec::new();
25+
let mut keys = Vec::new();
26+
27+
for schematics in self.data.split("\n\n") {
28+
let heights: Vec<_> = (0..5)
29+
.map(|x| {
30+
schematics
31+
.lines()
32+
.filter(|row| row.chars().nth(x).unwrap() == '#')
33+
.count()
34+
- 1
35+
})
36+
.collect();
37+
38+
if schematics.starts_with("#####") {
39+
locks.push(heights);
40+
} else {
41+
keys.push(heights);
42+
}
43+
}
44+
45+
let mut answer = 0;
46+
47+
for key in &keys {
48+
for lock in &locks {
49+
if key.iter().zip(lock.iter()).all(|(a, b)| a + b <= 5) {
50+
answer += 1;
51+
}
52+
}
53+
}
54+
55+
answer
56+
}
57+
}
58+
59+
fn main() {
60+
let args = aoc::parse_args();
61+
let mut puzzle = Puzzle::new();
62+
puzzle.configure(args.path.as_str());
63+
println!("{}", puzzle.part1());
64+
}
65+
66+
/// Test from puzzle input
67+
#[cfg(test)]
68+
mod test {
69+
use super::*;
70+
71+
#[test]
72+
fn test_part1() {
73+
let mut puzzle = Puzzle::new();
74+
puzzle.configure("test.txt");
75+
assert_eq!(puzzle.part1(), 3);
76+
}
77+
}

2024/day25/test.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#####
2+
.####
3+
.####
4+
.####
5+
.#.#.
6+
.#...
7+
.....
8+
9+
#####
10+
##.##
11+
.#.##
12+
...##
13+
...#.
14+
...#.
15+
.....
16+
17+
.....
18+
#....
19+
#....
20+
#...#
21+
#.#.#
22+
#.###
23+
#####
24+
25+
.....
26+
.....
27+
#.#..
28+
###..
29+
###.#
30+
###.#
31+
#####
32+
33+
.....
34+
.....
35+
.....
36+
#....
37+
#.#..
38+
#.#.#
39+
#####

0 commit comments

Comments
 (0)