2020: day08: part 1

This commit is contained in:
Antoine Martin 2020-12-08 16:03:33 +01:00
parent 76bd7d9c54
commit 71a100179e
6 changed files with 724 additions and 0 deletions

View file

@ -7,6 +7,7 @@ use aoc2020::day04;
use aoc2020::day05; use aoc2020::day05;
use aoc2020::day06; use aoc2020::day06;
use aoc2020::day07; use aoc2020::day07;
use aoc2020::day08;
fn aoc2020_all(c: &mut Criterion) { fn aoc2020_all(c: &mut Criterion) {
c.bench_function("day01", |b| b.iter(|| day01::run().unwrap())); c.bench_function("day01", |b| b.iter(|| day01::run().unwrap()));
@ -16,6 +17,7 @@ fn aoc2020_all(c: &mut Criterion) {
c.bench_function("day05", |b| b.iter(|| day05::run().unwrap())); c.bench_function("day05", |b| b.iter(|| day05::run().unwrap()));
c.bench_function("day06", |b| b.iter(|| day06::run().unwrap())); c.bench_function("day06", |b| b.iter(|| day06::run().unwrap()));
c.bench_function("day07", |b| b.iter(|| day07::run().unwrap())); c.bench_function("day07", |b| b.iter(|| day07::run().unwrap()));
c.bench_function("day08", |b| b.iter(|| day08::run().unwrap()));
} }
criterion_group! { criterion_group! {

605
aoc2020/input/day08.txt Normal file
View file

@ -0,0 +1,605 @@
acc +14
acc +11
nop +422
acc +14
jmp +443
acc +21
nop +524
acc -2
jmp +279
jmp +1
acc +28
acc +11
jmp +576
acc +32
acc -12
acc -8
jmp +291
nop +542
acc +41
jmp +320
acc +40
jmp +96
jmp +85
acc +38
acc +8
jmp +333
acc +44
nop +231
acc +40
jmp +323
acc +18
jmp +251
acc -1
jmp +385
acc -9
acc +48
acc +20
acc +34
jmp +150
nop +203
acc +4
acc +32
acc +44
jmp +168
acc +26
acc +46
acc +40
jmp -30
jmp +182
acc +18
jmp +404
nop +142
jmp +84
acc +30
acc +10
jmp +1
acc +40
jmp +370
jmp +381
jmp +239
acc -2
acc +47
acc -4
jmp +295
jmp -38
acc +40
acc +44
acc +4
acc +4
jmp +156
acc +31
acc +20
acc +0
acc -12
jmp -48
acc +32
acc +38
jmp +1
acc -6
jmp +375
acc +33
acc +27
acc +28
jmp +107
acc +1
acc +6
nop +136
jmp +85
acc +31
acc +49
acc +46
jmp +167
acc +5
acc -5
jmp +148
acc +22
acc +44
acc -8
acc -2
jmp -60
nop +354
jmp +59
acc +48
nop +473
acc -7
acc +4
jmp +105
jmp +456
acc +16
acc +33
acc +24
jmp -4
acc +36
acc +10
nop +441
jmp +268
jmp +388
acc +0
acc +27
acc -1
jmp -60
nop +90
jmp -90
acc +48
acc +30
jmp +284
acc +4
acc +6
acc +1
acc -10
jmp +95
acc +35
jmp +235
acc +31
acc -19
jmp -96
jmp +326
acc -7
acc +0
acc -1
jmp +53
acc +15
acc -14
jmp +450
nop +8
acc -2
acc -1
acc +17
jmp -25
nop +444
jmp +65
jmp -86
acc +44
acc +16
acc +32
acc -11
jmp +32
acc +14
acc +28
jmp +123
jmp +127
jmp -44
acc +42
acc +24
acc -3
acc +4
jmp +219
acc +28
acc +30
acc -14
acc -11
jmp +67
acc +5
acc +43
acc +23
nop +73
jmp +176
acc +28
acc +8
acc +42
acc +44
jmp +278
acc +9
acc +46
acc +0
acc +30
jmp +72
jmp +317
jmp +352
jmp +273
jmp +137
nop +364
jmp +249
nop +79
jmp +1
jmp -147
acc -10
acc -1
acc +12
acc +27
jmp +147
acc -5
acc +7
jmp +63
acc +33
acc +32
nop +81
jmp -185
acc +44
jmp +215
jmp +187
acc +14
acc +38
jmp -113
jmp +267
acc -9
acc +21
acc -5
jmp +143
nop -57
nop +281
jmp -170
jmp +267
nop -131
jmp -83
acc -6
jmp -95
acc -9
acc -8
jmp +184
acc +32
acc -16
jmp +171
acc +5
acc +22
acc -7
acc +20
jmp +45
acc +48
jmp +239
acc -4
jmp +75
acc -18
jmp -178
nop +349
acc -12
nop +313
jmp -57
acc +7
acc +6
jmp -241
acc +19
jmp +320
acc +13
jmp -61
acc +0
nop +337
jmp +66
acc +27
acc -11
acc -7
jmp +315
acc +23
acc +26
acc -5
jmp +132
acc +45
acc +21
acc -12
jmp +158
acc +19
jmp +176
acc +43
jmp +124
nop +227
nop -236
acc +11
jmp +1
jmp -67
acc +21
jmp +161
jmp +86
acc +26
acc +7
jmp +246
acc +0
jmp +215
jmp +1
acc +16
jmp -257
acc +2
jmp +281
nop -10
acc +46
jmp +124
acc +13
acc +24
jmp +204
jmp +1
acc +23
jmp +225
nop -243
jmp +167
jmp +1
jmp -142
acc -15
jmp -113
acc +27
acc -18
acc +12
jmp -259
nop +74
acc +35
acc +42
acc -4
jmp -166
nop +87
nop +86
acc +18
acc -2
jmp +212
acc -8
jmp -313
acc +36
acc -11
jmp -233
jmp +237
nop +67
acc +16
nop -57
jmp -92
acc +48
acc +2
acc +21
jmp +33
acc -15
jmp +145
acc +26
jmp -254
acc +30
acc +4
acc -1
acc -14
jmp -64
acc +32
acc +8
jmp -131
acc -13
jmp +138
acc +5
acc +4
jmp -4
acc +37
nop -278
acc +28
acc +17
jmp -215
jmp -104
nop -241
jmp -43
jmp -2
acc +5
acc -1
jmp +151
jmp +1
acc +21
jmp +19
acc +40
jmp +91
acc +50
nop +202
acc -12
jmp -333
nop -66
acc +42
acc +7
jmp +1
jmp +47
acc +32
acc +29
acc +42
nop -8
jmp +52
jmp -299
jmp +40
acc +36
acc -5
acc +39
jmp -116
acc +19
acc +30
acc +39
acc -1
jmp -276
jmp -245
acc +6
jmp -185
acc +50
acc +14
acc -7
jmp -325
acc +33
jmp -279
nop +173
acc +15
acc -17
jmp -33
acc +20
jmp -101
acc -17
jmp -335
nop -8
jmp +22
acc +0
acc +4
jmp -133
nop -81
jmp +64
jmp -306
acc -19
acc +31
acc +47
acc +26
jmp +55
jmp -402
acc +13
jmp -375
acc +6
acc -1
acc -6
acc +49
jmp -28
acc -7
jmp -203
jmp -395
acc +5
acc +38
acc +10
jmp +130
jmp +161
jmp -382
acc +45
jmp +113
acc +38
acc +48
acc +46
jmp +126
acc -1
acc -10
acc +4
acc +2
jmp -425
acc +0
jmp -80
acc +4
jmp -202
acc +25
acc +8
jmp -398
jmp -307
acc +3
jmp +17
acc +13
acc +33
acc +7
jmp -381
acc +5
acc +12
jmp -308
jmp +1
acc +3
acc -14
acc +46
jmp -415
acc +31
acc +7
acc +28
jmp -419
jmp -175
jmp +1
jmp -141
acc +20
nop -35
jmp -36
acc -6
jmp +108
nop +1
jmp +8
jmp -49
jmp -389
acc +24
nop -482
acc +41
acc +25
jmp -167
nop -26
jmp -198
nop -199
acc +23
acc -19
jmp -202
jmp +58
acc +3
jmp -237
acc +44
acc +42
acc +22
acc +5
jmp -307
acc +45
nop -418
acc +41
nop -88
jmp +63
acc +12
nop -56
acc -19
jmp +55
acc -13
acc -7
jmp -213
acc +42
jmp -88
acc +20
jmp -115
acc +6
jmp -57
acc +25
acc +49
jmp -43
jmp -322
jmp -456
acc +7
acc +40
acc +35
jmp -518
nop -461
acc +43
acc +33
jmp +7
acc +27
jmp +5
acc -15
acc -19
acc -2
jmp -238
acc +49
acc +48
acc -16
jmp +34
acc -6
acc +49
acc -4
acc +4
jmp +1
acc +35
nop -264
jmp -234
jmp -365
jmp -436
acc +20
acc +36
jmp -426
acc +39
acc +20
jmp -343
nop -443
jmp -325
jmp -127
nop -560
acc +10
jmp -511
jmp -455
acc -16
acc +18
jmp -61
acc +26
jmp -285
jmp +1
nop -397
acc +12
nop -67
jmp -371
acc +27
acc +13
jmp -395
acc +44
jmp -565
acc +1
jmp -21
nop -428
acc -4
jmp -265
acc +48
acc +10
acc +46
jmp -202
acc -4
acc -10
jmp -152
acc +17
acc -10
acc +22
acc +10
jmp +1

View file

@ -0,0 +1,9 @@
nop +0
acc +1
jmp +4
acc +3
jmp -3
acc -99
acc +1
jmp -4
acc +6

105
aoc2020/src/day08.rs Normal file
View file

@ -0,0 +1,105 @@
use std::collections::HashSet;
use std::fmt::Write;
use aoc::err;
const INPUT: &str = include_str!("../input/day08.txt");
pub fn run() -> aoc::Result<String> {
let mut res = String::with_capacity(128);
writeln!(res, "part 1: {}", part1(INPUT)?)?;
Ok(res)
}
fn part1(input: &str) -> aoc::Result<i64> {
let instructions = input
.lines()
.map(|line| line.parse())
.collect::<aoc::Result<Vec<Instruction>>>()?;
let mut interpreter = Interpreter::new(instructions);
let mut set = HashSet::new();
loop {
if !set.insert(interpreter.idx) {
break;
}
interpreter.step();
}
Ok(interpreter.accumulator)
}
struct Interpreter {
idx: usize,
accumulator: i64,
memory: Vec<Instruction>,
}
impl Interpreter {
fn new(instructions: Vec<Instruction>) -> Self {
Self {
idx: 0,
accumulator: 0,
memory: instructions,
}
}
fn step(&mut self) {
match self.memory[self.idx] {
Instruction::Acc(arg) => {
self.accumulator += arg;
self.idx += 1;
}
Instruction::Jmp(offset) => self.idx = self.idx.wrapping_add(offset as usize),
Instruction::Nop => self.idx += 1,
}
}
}
enum Instruction {
Acc(i64),
Jmp(i64),
Nop,
}
impl std::str::FromStr for Instruction {
type Err = aoc::Error;
fn from_str(s: &str) -> aoc::Result<Self> {
let space = s.find(' ').ok_or_else(|| err!("couldn't split on space"))?;
let inst = &s[..space];
let arg = s[(space + 1)..]
.parse()
.map_err(|e| err!("couldn't parse argument for instruction: {}", e))?;
Ok(match inst {
"acc" => Self::Acc(arg),
"jmp" => Self::Jmp(arg),
"nop" => Self::Nop,
_ => return Err(err!("unrecognized instruction `{}`", inst)),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
const PROVIDED: &str = include_str!("../input/day08_provided.txt");
#[test]
fn part1_provided() {
assert_eq!(part1(PROVIDED).unwrap(), 5);
}
#[test]
fn part1_real() {
assert_eq!(part1(INPUT).unwrap(), 1675);
}
}

View file

@ -5,3 +5,4 @@ pub mod day04;
pub mod day05; pub mod day05;
pub mod day06; pub mod day06;
pub mod day07; pub mod day07;
pub mod day08;

View file

@ -8,6 +8,7 @@ use aoc2020::day04;
use aoc2020::day05; use aoc2020::day05;
use aoc2020::day06; use aoc2020::day06;
use aoc2020::day07; use aoc2020::day07;
use aoc2020::day08;
fn main() -> Result<()> { fn main() -> Result<()> {
let days: &[DayFunc] = &[ let days: &[DayFunc] = &[
@ -18,6 +19,7 @@ fn main() -> Result<()> {
day05::run, day05::run,
day06::run, day06::run,
day07::run, day07::run,
day08::run,
]; ];
aoc::run(days) aoc::run(days)