Skip to content

Commit 5ac7807

Browse files
committed
2019 rust
1 parent 801ec0c commit 5ac7807

File tree

15 files changed

+518
-8
lines changed

15 files changed

+518
-8
lines changed

2019/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# https://adventofcode.com/2018
22

33
[workspace]
4-
members = ["day1", "day16", "day22", "day18"]
4+
members = ["day1", "day2", "day3", "day4", "day6", "day16", "day22", "day18"]
55

66
resolver = "2"

2019/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
![AoC2019](https://img.shields.io/badge/Advent_of_Code-2019-8A2BE2)
44
![Stars: 50](https://img.shields.io/badge/Stars-50⭐-blue)
5-
![Rust: 4](https://img.shields.io/badge/Rust-4-cyan?logo=Rust)
5+
![Rust: 8](https://img.shields.io/badge/Rust-8-cyan?logo=Rust)
66
![Python: 23](https://img.shields.io/badge/Python-23-cyan?logo=Python)
77

88
## 2019 ([Calendar](https://adventofcode.com/2019)) ([Solutions](../2019/)) : 50⭐
99

1010
Puzzle | Stars | Languages
1111
-------------------------------------------------------------------------------- | ----- | -----------
1212
[Day 1: The Tyranny of the Rocket Equation](https://adventofcode.com/2019/day/1) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2019/day1/day1.rs) [![Python](../scripts/assets/python.png)](../2019/day1/day1.py)
13-
[Day 2: 1202 Program Alarm](https://adventofcode.com/2019/day/2) | ⭐⭐ | [![Python](../scripts/assets/python.png)](../2019/day2/day2.py)
14-
[Day 3: Crossed Wires](https://adventofcode.com/2019/day/3) | ⭐⭐ | [![Python](../scripts/assets/python.png)](../2019/day3/day3.py)
15-
[Day 4: Secure Container](https://adventofcode.com/2019/day/4) | ⭐⭐ | [![Python](../scripts/assets/python.png)](../2019/day4/day4.py)
13+
[Day 2: 1202 Program Alarm](https://adventofcode.com/2019/day/2) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2019/day2/day2.rs) [![Python](../scripts/assets/python.png)](../2019/day2/day2.py)
14+
[Day 3: Crossed Wires](https://adventofcode.com/2019/day/3) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2019/day3/day3.rs) [![Python](../scripts/assets/python.png)](../2019/day3/day3.py)
15+
[Day 4: Secure Container](https://adventofcode.com/2019/day/4) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2019/day4/day4.rs) [![Python](../scripts/assets/python.png)](../2019/day4/day4.py)
1616
[Day 5: Sunny with a Chance of Asteroids](https://adventofcode.com/2019/day/5) | ⭐⭐ | [![Python](../scripts/assets/python.png)](../2019/day5/day5.py)
17-
[Day 6: Universal Orbit Map](https://adventofcode.com/2019/day/6) | ⭐⭐ | [![Python](../scripts/assets/python.png)](../2019/day6/day6.py)
17+
[Day 6: Universal Orbit Map](https://adventofcode.com/2019/day/6) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2019/day6/day6.rs) [![Python](../scripts/assets/python.png)](../2019/day6/day6.py)
1818
[Day 7: Amplification Circuit](https://adventofcode.com/2019/day/7) | ⭐⭐ | [![Python](../scripts/assets/python.png)](../2019/day7/day7.py)
1919
[Day 8: Space Image Format](https://adventofcode.com/2019/day/8) | ⭐⭐ | [![Python](../scripts/assets/python.png)](../2019/day8/day8.py)
2020
[Day 9: Sensor Boost](https://adventofcode.com/2019/day/9) | ⭐⭐ | [![Python](../scripts/assets/python.png)](../2019/day9/day9.py)

2019/day2/Cargo.toml

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

2019/day2/day2.rs

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//! [Day 2: 1202 Program Alarm](https://adventofcode.com/2019/day/2)
2+
3+
struct Puzzle {
4+
program: Vec<u32>,
5+
}
6+
7+
const ADD: u32 = 1;
8+
const MUL: u32 = 2;
9+
10+
impl Puzzle {
11+
fn new() -> Puzzle {
12+
Puzzle {
13+
program: Vec::new(),
14+
}
15+
}
16+
17+
/// Get the puzzle input.
18+
fn configure(&mut self, path: &str) {
19+
let data = std::fs::read_to_string(path).unwrap();
20+
21+
self.load(&data);
22+
}
23+
24+
fn load(&mut self, data: &str) {
25+
self.program = data
26+
.trim_ascii()
27+
.split(',')
28+
.map(|x| x.parse::<_>().unwrap())
29+
.collect();
30+
}
31+
32+
#[cfg(test)]
33+
fn dump(&self) -> String {
34+
self.program
35+
.iter()
36+
.map(|x| x.to_string())
37+
.collect::<Vec<String>>()
38+
.join(",")
39+
}
40+
41+
fn run(memory: &mut [u32]) {
42+
let mut ip = 0;
43+
while memory[ip] != 99 {
44+
assert!(ip + 3 < memory.len());
45+
46+
let opcode = memory[ip];
47+
let noun = usize::try_from(memory[ip + 1]).unwrap();
48+
let verb = usize::try_from(memory[ip + 2]).unwrap();
49+
let result = usize::try_from(memory[ip + 3]).unwrap();
50+
51+
memory[result] = match opcode {
52+
ADD => memory[noun] + memory[verb],
53+
MUL => memory[noun] * memory[verb],
54+
_ => panic!("Invalid opcode"),
55+
};
56+
ip += 4;
57+
}
58+
}
59+
60+
/// Solve part one.
61+
fn part1(&self) -> u32 {
62+
let mut memory = self.program.clone();
63+
memory[1] = 12;
64+
memory[2] = 2;
65+
66+
Self::run(&mut memory);
67+
68+
memory[0]
69+
}
70+
71+
/// Solve part two.
72+
fn part2(&self) -> u32 {
73+
for noun in 0..100 {
74+
for verb in 0..100 {
75+
let mut memory = self.program.clone();
76+
memory[1] = noun;
77+
memory[2] = verb;
78+
79+
Self::run(&mut memory);
80+
81+
// clippy...
82+
if memory[0] == 19_690_720 {
83+
return 100 * noun + verb;
84+
}
85+
}
86+
}
87+
0
88+
}
89+
}
90+
91+
fn main() {
92+
let args = aoc::parse_args();
93+
let mut puzzle = Puzzle::new();
94+
puzzle.configure(args.path.as_str());
95+
println!("{}", puzzle.part1());
96+
println!("{}", puzzle.part2());
97+
}
98+
99+
/// Test from puzzle input
100+
#[cfg(test)]
101+
mod test {
102+
use super::*;
103+
104+
#[test]
105+
fn test01() {
106+
let mut puzzle = Puzzle::new();
107+
108+
puzzle.load("1,0,0,0,99");
109+
Puzzle::run(&mut puzzle.program);
110+
assert_eq!(puzzle.dump(), "2,0,0,0,99");
111+
112+
puzzle.load("2,3,0,3,99");
113+
Puzzle::run(&mut puzzle.program);
114+
assert_eq!(puzzle.dump(), "2,3,0,6,99");
115+
puzzle.load("2,4,4,5,99,0");
116+
Puzzle::run(&mut puzzle.program);
117+
assert_eq!(puzzle.dump(), "2,4,4,5,99,9801");
118+
119+
puzzle.load("1,1,1,4,99,5,6,0,99");
120+
Puzzle::run(&mut puzzle.program);
121+
assert_eq!(puzzle.dump(), "30,1,1,4,2,5,6,0,99");
122+
}
123+
}

2019/day3/Cargo.toml

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

2019/day3/day3.rs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
//! [Day 3: Crossed Wires](https://adventofcode.com/2019/day/3)
2+
3+
use std::collections::HashSet;
4+
5+
#[derive(Debug)]
6+
struct Instr {
7+
delta: (i32, i32),
8+
distance: u32,
9+
}
10+
11+
fn draw(instrs: &[Instr]) -> HashSet<(i32, i32)> {
12+
let mut line = HashSet::new();
13+
14+
let (mut x, mut y) = (0, 0);
15+
16+
for i in instrs {
17+
(0..i.distance).for_each(|_| {
18+
x += i.delta.0;
19+
y += i.delta.1;
20+
21+
line.insert((x, y));
22+
});
23+
}
24+
25+
line
26+
}
27+
28+
fn steps(instrs: &[Instr], target: (i32, i32)) -> u32 {
29+
let mut count = 0u32;
30+
let (mut x, mut y) = (0, 0);
31+
32+
for i in instrs {
33+
for _ in 0..i.distance {
34+
x += i.delta.0;
35+
y += i.delta.1;
36+
count += 1;
37+
38+
if (x, y) == target {
39+
return count;
40+
}
41+
}
42+
}
43+
44+
0
45+
}
46+
47+
fn manhattan(p: (i32, i32)) -> i32 {
48+
p.0.abs() + p.1.abs()
49+
}
50+
51+
struct Puzzle {
52+
paths: Vec<Vec<Instr>>,
53+
}
54+
55+
impl Puzzle {
56+
fn new() -> Puzzle {
57+
Puzzle { paths: Vec::new() }
58+
}
59+
60+
/// Get the puzzle input.
61+
fn configure(&mut self, path: &str) {
62+
let data = std::fs::read_to_string(path).unwrap();
63+
64+
for line in data.lines() {
65+
self.paths.push(
66+
line.split(',')
67+
.map(|s| Instr {
68+
delta: match s.chars().nth(0).unwrap() {
69+
'U' => (0, 1),
70+
'D' => (0, -1),
71+
'L' => (-1, 0),
72+
'R' => (1, 0),
73+
_ => panic!(),
74+
},
75+
distance: s[1..].parse().unwrap(),
76+
})
77+
.collect::<Vec<_>>(),
78+
);
79+
}
80+
}
81+
82+
/// Solve part one.
83+
fn part1(&self) -> i32 {
84+
let wire0 = draw(&self.paths[0]);
85+
let wire1 = draw(&self.paths[1]);
86+
87+
wire0
88+
.intersection(&wire1)
89+
.map(|&p| manhattan(p))
90+
.min()
91+
.unwrap()
92+
}
93+
94+
/// Solve part two.
95+
fn part2(&self) -> u32 {
96+
let wire0 = draw(&self.paths[0]);
97+
let wire1 = draw(&self.paths[1]);
98+
99+
wire0
100+
.intersection(&wire1)
101+
.map(|&target| steps(&self.paths[0], target) + steps(&self.paths[1], target))
102+
.min()
103+
.unwrap()
104+
}
105+
}
106+
107+
fn main() {
108+
let args = aoc::parse_args();
109+
let mut puzzle = Puzzle::new();
110+
puzzle.configure(args.path.as_str());
111+
println!("{}", puzzle.part1());
112+
println!("{}", puzzle.part2());
113+
}
114+
115+
/// Test from puzzle input
116+
#[cfg(test)]
117+
mod test {
118+
use super::*;
119+
120+
#[test]
121+
fn test01() {
122+
let mut puzzle = Puzzle::new();
123+
puzzle.configure("test1.txt");
124+
assert_eq!(puzzle.part1(), 159);
125+
assert_eq!(puzzle.part2(), 610);
126+
}
127+
128+
#[test]
129+
fn test02() {
130+
let mut puzzle = Puzzle::new();
131+
puzzle.configure("test2.txt");
132+
assert_eq!(puzzle.part1(), 135);
133+
assert_eq!(puzzle.part2(), 410);
134+
}
135+
}

2019/day3/test1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
R75,D30,R83,U83,L12,D49,R71,U7,L72
2+
U62,R66,U55,R34,D71,R55,D58,R83

2019/day3/test2.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51
2+
U98,R91,D20,R16,D67,R40,U7,R15,U6,R7

2019/day4/Cargo.toml

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

0 commit comments

Comments
 (0)