Primeagen's Polyglot Class 3
- Published
- Tags
- #notes#typescript#rust#go
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);
}