mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 12:50:32 +01:00
Reimplement day 2.
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
ULL
|
||||
RRDDD
|
||||
LURDL
|
||||
UUUUD
|
||||
@@ -1,85 +0,0 @@
|
||||
use std::cmp::min;
|
||||
use std::io::prelude::*;
|
||||
use std::io::BufReader;
|
||||
use std::fs::File;
|
||||
use std::path::Path;
|
||||
use std::env;
|
||||
|
||||
fn move_pos1(pos: i32, instruction: char) -> i32
|
||||
{
|
||||
match instruction {
|
||||
'U' => if pos > 3 { pos - 3 } else { pos },
|
||||
'D' => if pos < 7 { pos + 3 } else { pos },
|
||||
'L' => if pos % 3 != 1 { pos - 1 } else { pos },
|
||||
'R' => if pos % 3 != 0 { pos + 1 } else { pos },
|
||||
_ => panic!("Unsupported direction {}", instruction),
|
||||
}
|
||||
}
|
||||
|
||||
fn row_width(y: i32) -> i32
|
||||
{
|
||||
min(2 * y + 1, 9 - 2 * y)
|
||||
}
|
||||
|
||||
fn row_offset(y: i32) -> i32
|
||||
{
|
||||
(5 - row_width(y)) / 2
|
||||
}
|
||||
|
||||
fn is_legal(pos: (i32, i32)) -> bool {
|
||||
let (x, y) = pos;
|
||||
let width = row_width(y);
|
||||
let offset = row_offset(y);
|
||||
|
||||
y >= 0 && y < 5 && x >= offset && x < offset + width
|
||||
}
|
||||
|
||||
fn move_pos2(pos: (i32, i32), instruction: char) -> (i32, i32)
|
||||
{
|
||||
let (x, y) = pos;
|
||||
let new_pos = match instruction {
|
||||
'U' => (x, y - 1),
|
||||
'D' => (x, y + 1),
|
||||
'L' => (x - 1, y),
|
||||
'R' => (x + 1, y),
|
||||
_ => panic!("Unsupported direction {}", instruction),
|
||||
};
|
||||
|
||||
if is_legal(new_pos) { new_pos } else { pos }
|
||||
}
|
||||
|
||||
fn pos2char(pos: (i32, i32)) -> String
|
||||
{
|
||||
let (x, y) = pos;
|
||||
let mut num = x + 1 - row_offset(y);
|
||||
for i in 0..y {
|
||||
num += row_width(i);
|
||||
}
|
||||
|
||||
format!("{:X}", num)
|
||||
}
|
||||
|
||||
fn main()
|
||||
{
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let path = Path::new(&args[1]);
|
||||
let f = File::open(&path).expect("Could not open file");
|
||||
let reader = BufReader::new(f);
|
||||
|
||||
let mut pos1 = 5;
|
||||
let mut pos2 = (0, 2);
|
||||
let mut code1 = String::new();
|
||||
let mut code2 = String::new();
|
||||
for line in reader.lines() {
|
||||
for instruction in line.unwrap().trim().chars() {
|
||||
pos1 = move_pos1(pos1, instruction);
|
||||
pos2 = move_pos2(pos2, instruction);
|
||||
}
|
||||
|
||||
code1 += &pos1.to_string();
|
||||
code2 += &pos2char(pos2);
|
||||
}
|
||||
|
||||
println!("Code 1 is {}", code1);
|
||||
println!("Code 2 is {}", code2);
|
||||
}
|
||||
128
2016/src/day2.rs
Normal file
128
2016/src/day2.rs
Normal file
@@ -0,0 +1,128 @@
|
||||
use std::cmp::min;
|
||||
use std::io::prelude::*;
|
||||
use std::io;
|
||||
use common;
|
||||
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Day2 {
|
||||
}
|
||||
|
||||
impl Day2 {
|
||||
|
||||
pub fn new() -> Day2 {
|
||||
Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl common::Solution for Day2 {
|
||||
|
||||
fn part1(&mut self, input: &mut io::Read) -> String {
|
||||
let reader = io::BufReader::new(input);
|
||||
let mut pos = 5;
|
||||
let mut code = String::new();
|
||||
|
||||
for line in reader.lines() {
|
||||
for instruction in line.unwrap().trim().chars() {
|
||||
pos = move_pos1(pos, instruction);
|
||||
}
|
||||
code += &pos.to_string();
|
||||
}
|
||||
|
||||
code
|
||||
}
|
||||
|
||||
fn part2(&mut self, input: &mut io::Read) -> String {
|
||||
let reader = io::BufReader::new(input);
|
||||
let mut pos = (0, 2);
|
||||
let mut code = String::new();
|
||||
|
||||
for line in reader.lines() {
|
||||
for instruction in line.unwrap().trim().chars() {
|
||||
pos = move_pos2(pos, instruction);
|
||||
}
|
||||
code += &pos2char(pos);
|
||||
}
|
||||
|
||||
code
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn move_pos1(pos: i32, instruction: char) -> i32
|
||||
{
|
||||
match instruction {
|
||||
'U' => if pos > 3 { pos - 3 } else { pos },
|
||||
'D' => if pos < 7 { pos + 3 } else { pos },
|
||||
'L' => if pos % 3 != 1 { pos - 1 } else { pos },
|
||||
'R' => if pos % 3 != 0 { pos + 1 } else { pos },
|
||||
_ => panic!("Unsupported direction {}", instruction),
|
||||
}
|
||||
}
|
||||
|
||||
fn row_width(y: i32) -> i32
|
||||
{
|
||||
min(2 * y + 1, 9 - 2 * y)
|
||||
}
|
||||
|
||||
fn row_offset(y: i32) -> i32
|
||||
{
|
||||
(5 - row_width(y)) / 2
|
||||
}
|
||||
|
||||
fn is_legal(pos: (i32, i32)) -> bool {
|
||||
let (x, y) = pos;
|
||||
let width = row_width(y);
|
||||
let offset = row_offset(y);
|
||||
|
||||
y >= 0 && y < 5 && x >= offset && x < offset + width
|
||||
}
|
||||
|
||||
fn move_pos2(pos: (i32, i32), instruction: char) -> (i32, i32)
|
||||
{
|
||||
let (x, y) = pos;
|
||||
let new_pos = match instruction {
|
||||
'U' => (x, y - 1),
|
||||
'D' => (x, y + 1),
|
||||
'L' => (x - 1, y),
|
||||
'R' => (x + 1, y),
|
||||
_ => panic!("Unsupported direction {}", instruction),
|
||||
};
|
||||
|
||||
if is_legal(new_pos) { new_pos } else { pos }
|
||||
}
|
||||
|
||||
fn pos2char(pos: (i32, i32)) -> String
|
||||
{
|
||||
let (x, y) = pos;
|
||||
let mut num = x + 1 - row_offset(y);
|
||||
for i in 0..y {
|
||||
num += row_width(i);
|
||||
}
|
||||
|
||||
format!("{:X}", num)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use common::Solution;
|
||||
|
||||
const SAMPLE: &str = "ULL
|
||||
RRDDD
|
||||
LURDL
|
||||
UUUUD";
|
||||
|
||||
#[test]
|
||||
fn test_part1() {
|
||||
let mut instance = Day2::new();
|
||||
assert_eq!("1985", instance.part1(&mut SAMPLE.as_bytes()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part2() {
|
||||
let mut instance = Day2::new();
|
||||
assert_eq!("5DB3", instance.part2(&mut SAMPLE.as_bytes()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,6 +8,7 @@ use std::io;
|
||||
|
||||
pub mod common;
|
||||
pub mod day1;
|
||||
pub mod day2;
|
||||
pub mod day12;
|
||||
pub mod day15;
|
||||
pub mod day16;
|
||||
@@ -18,6 +19,7 @@ pub mod day25;
|
||||
fn get_impl(day: &str) -> Box<common::Solution> {
|
||||
match day.parse() {
|
||||
Ok(1) => { Box::new(day1::Day1::new()) }
|
||||
Ok(2) => { Box::new(day2::Day2::new()) }
|
||||
Ok(12) => { Box::new(day12::Day12::new()) }
|
||||
Ok(15) => { Box::new(day15::Day15::new()) }
|
||||
Ok(16) => { Box::new(day16::Day16::new()) }
|
||||
|
||||
Reference in New Issue
Block a user