Thevetats Ramblings

Explore

About
Blogs
Misc
Resume
Services
Testing
Tools

Blogs

Env

Freshstart

Aliases
How I config my System76
Un-F%ck your M2 for development
ZSH Functions

Toolbelt

What's usually in the workvan

Rust

Notes

Primeagen's Rust for TS Devs

RustGoTs

Primeagen's Polyglot Class 1
Primeagen's Polyglot Class 2
Primeagen's Polyglot Class 3

Tauri

Setting up Tauri with Vite

WebDev

Ai

TheBeast

Slaying the beast

ComponentLibary

Salt Life
Submodules in Git

Sql

Useful SQL

Unocss

Just one more...
Setting up UnoCSS in a Vite Project

Vue

Reference

Suspense
Transitions

Primeagen's Polyglot Class 2

Published
May 6, 2023
Tags
#notes#typescript#rust#go

Table of Contents

  • Typescript
  • Go
  • Rust

Typescript

ts
function getInput(): string {
  return `0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2`
}

interface Point {
  x: number
  y: number
}

interface Line {
  p1: Point
  p2: Point
}

function isHOrV(line: Line) {
  return line.p1.x === line.p2.x || line.p1.y === line.p2.y
}

function parsePoint(p: string) {
  const [x, y] = p.split(',')

  return {
    x: +x,
    y: +y,
  }
}

function parseLine(line: string) {
  const [p1, p2] = line.split(' -> ')
  return {
    p1: parsePoint(p1),
    p2: parsePoint(p2),
  }
}

const lines = getInput().split('\n').map(parseLine).filter(isHOrV)

console.log(lines)

Go

go
package main

import (
    "fmt"
    "log"
    "strconv"
    "strings"
)

func getInput() string {
    return `0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2`
}

type Point struct {
    x int
    y int
}

type Line struct {
    p1 *Point
    p2 *Point
}

func parsePoint(line string) (*Point, error) {
    parts := strings.Split(line, ",")
    x, err := strconv.Atoi(parts[0])
    if err != nil {
        return nil, err
    }

    y, err := strconv.Atoi(parts[1])
    if err != nil {
        return nil, err
    }

    return &Point{
        x: x,
        y: y,
    }, nil
}

func parseLine(line string) (*Line, error) {
    parts := strings.Split(line, " -> ")
    p1, err := parsePoint(parts[0])
    if err != nil {
        return nil, err
    }

    p2, err := parsePoint(parts[1])
    if err != nil {
        return nil, err
    }

    return &Line{
        p1: p1,
        p2: p2,
    }, nil
}

func main() {
    lines := []Line{}
    for _, l := range strings.Split(getInput(), "\n") {
        line, err := parseLine(l)
        if err != nil {
            log.Fatal("Could not parse line: ", l, err)
        }

        lines = append(lines, *line)

    }
    fmt.Printf("%+v", lines)
}

Rust

rust
use anyhow::{anyhow, Result};
use std::str::FromStr;

fn get_input() -> &'static str {
    "0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2"
}

#[derive(Debug)]
struct Point {
    x: i32,
    y: i32,
}

impl FromStr for Point {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self> {
        let result = s.split_once(',');
        if result.is_none() {
            return Err(anyhow!("expected a point to contain a comma"));
        }

        let (x, y) = result.unwrap();
        let x: i32 = str::parse(x)?;
        let y: i32 = str::parse(y)?;

        Ok(Point { x, y })
    }
}

#[derive(Debug)]
struct Line {
    p1: Point,
    p2: Point,
}
impl FromStr for Line {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self> {
        let result = s.split_once(" -> ");
        if result.is_none() {
            return Err(anyhow!("expected a line to contain -> "));
        }

        let (p1, p2) = result.unwrap();
        let p1: Point = str::parse(p1)?;
        let p2: Point = str::parse(p2)?;

        Ok(Line { p1, p2 })
    }
}

impl Line {
    fn is_horv(&self) -> bool {
        self.p1.x == self.p2.x || self.p1.y == self.p2.y
    }
}

fn main() {
    let lines = get_input()
        .lines()
        .flat_map(str::parse)
        .filter(|x: &Line| x.is_horv())
        .collect::<Vec<Line>>();

    println!("{:?}", lines)
}
Made from scratch with ❤️ by Thevetat