Day 3: Squares With Three Sides

Reference

library(tidyverse)

input <- read_table(file = "inputs/2016/03.txt", col_names = c("x", "y", "z"))

Part One

is_triangle <- function(x, y, z) {
    max(x, y, z) < x + y + z - max(x, y, z)
}

triangles <- input |> 
    mutate(is_triangle = pmap_lgl(.l = list(x, y, z), .f = is_triangle))

triangles |> 
    summarise(n = sum(is_triangle)) |> 
    pull(n) |> 
    cat()
## 993

Part Two

triangles <- tibble(sides = c(pull(input, x), pull(input, y), pull(input, z))) |> 

    mutate(triangle = rep(x = 1:nrow(input),    each = 3),
           side     = rep(x = c("x", "y", "z"), times = nrow(input))) |> 

    pivot_wider(values_from = sides, names_from = side) |> 

    mutate(is_triangle = pmap_lgl(.l = list(x, y, z), .f = is_triangle), .keep = "used")

triangles |> 
    summarise(n = sum(is_triangle)) |> 
    pull(n) |> 
    cat()
## 1849