Skip to content

Commit ab23048

Browse files
2017: Day 25
1 parent 1a83ead commit ab23048

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

2017/day25/.vscode/settings.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"[rust]": {
3+
"editor.defaultFormatter": "rust-lang.rust-analyzer",
4+
"editor.formatOnSave": true
5+
},
6+
"rust-analyzer.check.command": "clippy"
7+
}

2017/day25/Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2017/day25/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "day25"
3+
version = "0.1.0"
4+
edition = "2021"

2017/day25/src/main.rs

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use std::{collections::HashMap, fs};
2+
3+
fn main() {
4+
let input = fs::read_to_string("input.txt").expect("Could not read file");
5+
let blocks = input.split("\n\n").collect::<Vec<_>>();
6+
7+
let steps = blocks[0]
8+
.split(' ')
9+
.nth(8)
10+
.unwrap()
11+
.parse::<usize>()
12+
.unwrap();
13+
14+
let mut instructions = Vec::new();
15+
for b in &blocks[1..] {
16+
let mut i = b.lines();
17+
let in_state = i
18+
.next()
19+
.unwrap()
20+
.strip_suffix(':')
21+
.unwrap()
22+
.split(' ')
23+
.last()
24+
.unwrap();
25+
for _ in 0..2 {
26+
let f = i
27+
.next()
28+
.unwrap()
29+
.strip_suffix(':')
30+
.unwrap()
31+
.split(' ')
32+
.last()
33+
.unwrap()
34+
.parse::<usize>()
35+
.unwrap();
36+
let f_write = i
37+
.next()
38+
.unwrap()
39+
.strip_suffix('.')
40+
.unwrap()
41+
.split(' ')
42+
.last()
43+
.unwrap()
44+
.parse::<usize>()
45+
.unwrap();
46+
let f_move = i
47+
.next()
48+
.unwrap()
49+
.strip_suffix('.')
50+
.unwrap()
51+
.split(' ')
52+
.last()
53+
.unwrap();
54+
let f_continue = i
55+
.next()
56+
.unwrap()
57+
.strip_suffix('.')
58+
.unwrap()
59+
.split(' ')
60+
.last()
61+
.unwrap();
62+
instructions.push((in_state, f, f_write, f_move, f_continue));
63+
}
64+
}
65+
66+
let mut tape = HashMap::new();
67+
let mut cursor = 0;
68+
let mut state = "A";
69+
70+
for _ in 0..steps {
71+
let current = tape.entry(cursor).or_insert(0);
72+
for i in &instructions {
73+
if state == i.0 && *current == i.1 {
74+
*current = i.2;
75+
if i.3 == "left" {
76+
cursor -= 1;
77+
} else {
78+
cursor += 1;
79+
}
80+
state = i.4;
81+
break;
82+
}
83+
}
84+
}
85+
86+
let sum: usize = tape.values().sum();
87+
println!("{}", sum);
88+
}

0 commit comments

Comments
 (0)