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 3

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

Table of Contents

  • Typescript
  • Go
  • Rust

Typescript

ts
function getInput(): string {
  return `..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#`
}

enum Thing {
  Tree,
  Snow,
}

const things = getInput()
  .split('\n')
  .map(x => x.split('').map(x => x === '.' ? Thing.Snow : Thing.Tree))

const colLen = things[0].length
let treeCount = 0

things.forEach((thingRow, i) => {
  if (thingRow[i * 3 % colLen] === Thing.Tree) {
    treeCount++
  }
})

console.log('treeCount: ', treeCount)

Go

go
package main

import (
    "fmt"
    "strings"
)

type Thing = int

const (
    Tree Thing = iota
    Snow
)

func getInput() string {
    return `..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#`
}

func main() {
    treeCount := 0
    for row, line := range strings.Split(getInput(), "\n") {
        if string(line[row*3%len(line)]) == "#" {
            treeCount += 1
        }
    }
    fmt.Printf("treecount %v\n", treeCount)
}

Rust

rust
fn get_input() -> &'static str {
    "..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#"
}

fn main() {
    let result = get_input()
        .lines()
        .enumerate()
        .flat_map(|(idx, line)| line.chars().nth(idx * 3 % line.len()))
        .filter(|&x| x == '#')
        .count();

    println!("Result: {}", result);
}
Made from scratch with ❤️ by Thevetat