Skip to content

Instantly share code, notes, and snippets.

View zoldar's full-sized avatar

Adrian Gruntkowski zoldar

View GitHub Profile
import std/sequtils, std/strutils, std/strformat
type
Operation = enum AddX, Noop
Instruction = object
op: Operation
args: seq[int]
State = object
import std/strutils, std/sequtils, std/strformat, std/sets
type
Position = object
x: int
y: int
Move = Position
Rope = object
import std/sequtils, std/strutils, std/sets, std/sugar, std/strformat, std/tables, std/algorithm
proc countVisible*(input: seq[seq[int]]): int =
runnableExamples:
let input = @[@[2, 2, 1, 2],
@[2, 1, 3, 2],
@[2, 2, 5, 2]]
assert countVisible(input) == 11
import std/sequtils, std/strutils, std/sugar, std/strformat
type
NodeKind = enum File, Dir
Node = object
kind: NodeKind
name: string
size: int
children: seq[Node]
import std/strformat, std/sets
proc findMarker(input: string, length: int): int =
for idx in 0..<input.len:
if input[idx..<idx+length].toHashSet.len == length:
result = idx + length
break
proc findStartOfPacket*(input: string): int =
findMarker(input, 4)
import std/strformat, std/sequtils, std/strscans, std/strutils, sugar
proc extractInput*(input: seq[string]): (seq[string], seq[string]) =
runnableExamples:
let (s, i) = extractInput(@["foo", "bar", "", "baz", "bax"])
assert s == @["foo", "bar"]
assert i == @["baz", "bax"]
var storage: seq[string]
var instructions: seq[string]
import std/sequtils, std/os, std/strformat, std/strscans, std/sets, sugar
proc countPairs(input: seq[string], fun: (HashSet[int], HashSet[int]) -> bool): int =
var count = 0
for line in input:
let (ok, lower1, upper1, lower2, upper2) = scanTuple(
line, "$i-$i,$i-$i")
if ok:
import std/sequtils, std/sets, std/tables, std/os, std/strformat
let priorities = concat(
zip(toSeq('a'..'z'), toSeq(1..26)),
zip(toSeq('A'..'Z'), toSeq(27..52))
).toTable
iterator chunks[T](a: seq[T], size: int): seq[T] =
var chunk: seq[T] = @[]
import std/tables
var points = 0
let choicePoints = {
'R': 1,
'P': 2,
'S': 3
}.toTable

Advent of Code 2022

Day 1

input = """
2494
8013
1055
5425