Advent of Code in your browser, in any language
Paste your puzzle input into the Input tab, write your solution and press Run. There is nothing to install and no sign-in, and all 46 languages are in one editor, which helps when you solve each day in a different one.
import re
import sys
# Paste your puzzle input into the Input tab, then press Run.
lines = sys.stdin.read().splitlines()
def ints(line):
"""Every whole number in a line, minus signs included."""
return [int(n) for n in re.findall(r"-?\d+", line)]
def part1(lines):
return len(lines)
def part2(lines):
return sum(sum(ints(line)) for line in lines)
print("Part 1:", part1(lines))
print("Part 2:", part2(lines)) 7 2 1 9 4 4
The Input tab holds a small sample of our own. Press Run and the starter prints the number of lines and the sum of every number in them:
Part 1: 3
Part 2: 27How to use it for a day's puzzle
- Open the day on Advent of Code, sign in there, and open your puzzle input. Each person's input is different.
- Copy all of it and paste it into the Input tab above, in place of the sample. The tab takes up to 100 KB.
- Replace
part1andpart2with your solution and press Run, then submit the answer on Advent of Code. For another language, pick it from the menu: it opens with its own example program and input, so paste your input again and start from its reader below.
The ints helper pulls every number out of a line, which is how many puzzle inputs are best read. Test
your code on the example from the puzzle text first: paste the example into the Input tab, check that you get the
answer the puzzle gives, then paste your real input.
Read your input in 30 languages
Each program reads the whole input from standard input into a list of lines and prints how many there are. Start from it, and keep the reading part when you replace the rest. Each one was run on our servers with and without a final newline after the last line, since a pasted input may or may not have one.
Python, JavaScript, TypeScript, Go, Rust, C++, C, Java, Kotlin, C#, F#, Ruby, Haskell, OCaml, Elixir, Zig, Julia, Lua, Swift, Scala, Clojure, Nim, Crystal, Dart, PHP, Perl, Bash, Common Lisp, Erlang, R
Python
import sys
lines = sys.stdin.read().splitlines()
print(len(lines))Run it on the Python online compiler, or pick Python in the editor above.
JavaScript
const lines = require("fs").readFileSync(0, "utf8").trimEnd().split("\n");
console.log(lines.length);Run it on the JavaScript online compiler, or pick JavaScript in the editor above.
TypeScript
const text = await new Response(Deno.stdin.readable).text();
const lines = text.trimEnd().split("\n");
console.log(lines.length);Run it on the TypeScript online compiler, or pick TypeScript in the editor above.
Go
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
var lines []string
sc := bufio.NewScanner(os.Stdin)
sc.Buffer(make([]byte, 1<<20), 1<<20) // allow lines up to 1 MB
for sc.Scan() {
lines = append(lines, sc.Text())
}
fmt.Println(len(lines))
}Run it on the Go online compiler, or pick Go in the editor above.
Rust
use std::io::{self, Read};
fn main() {
let mut input = String::new();
io::stdin().read_to_string(&mut input).unwrap();
let lines: Vec<&str> = input.lines().collect();
println!("{}", lines.len());
}Run it on the Rust online compiler, or pick Rust in the editor above.
C++
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> lines;
for (std::string line; std::getline(std::cin, line);)
lines.push_back(line);
std::cout << lines.size() << '\n';
}Run it on the C++ online compiler, or pick C++ in the editor above.
C
#include <stdio.h>
int main(void) {
char line[65536];
int count = 0;
while (fgets(line, sizeof line, stdin))
count++;
printf("%d\n", count);
return 0;
}Run it on the C online compiler, or pick C in the editor above.
Java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> lines = new BufferedReader(new InputStreamReader(System.in)).lines().toList();
System.out.println(lines.size());
}
}Run it on the Java online compiler, or pick Java in the editor above.
Kotlin
fun main() {
val lines = generateSequence(::readLine).toList()
println(lines.size)
}Run it on the Kotlin online compiler, or pick Kotlin in the editor above.
C#
var lines = new List<string>();
string? line;
while ((line = Console.ReadLine()) != null)
lines.Add(line);
Console.WriteLine(lines.Count);Run it on the C# online compiler, or pick C# in the editor above.
F#
let lines = stdin.ReadToEnd().TrimEnd().Split('\n')
printfn "%d" lines.LengthRun it on the F# online compiler, or pick F# in the editor above.
Ruby
lines = $stdin.readlines(chomp: true)
puts lines.sizeRun it on the Ruby online compiler, or pick Ruby in the editor above.
Haskell
main :: IO ()
main = do
input <- getContents
let ls = lines input
print (length ls)Run it on the Haskell online compiler, or pick Haskell in the editor above.
OCaml
let lines = In_channel.input_lines stdin
let () = Printf.printf "%d\n" (List.length lines)Run it on the OCaml online compiler, or pick OCaml in the editor above.
Elixir
lines = IO.read(:stdio, :eof) |> String.split("\n", trim: true)
IO.puts(length(lines))Run it on the Elixir online compiler, or pick Elixir in the editor above.
Zig
const std = @import("std");
pub fn main(init: std.process.Init) !void {
var in_buf: [4096]u8 = undefined;
var stdin = std.Io.File.reader(.stdin(), init.io, &in_buf);
const input = try stdin.interface.allocRemaining(init.gpa, .unlimited);
defer init.gpa.free(input);
var lines: usize = 0;
var it = std.mem.tokenizeScalar(u8, input, '\n');
while (it.next()) |_| lines += 1;
var out_buf: [256]u8 = undefined;
var stdout = std.Io.File.writer(.stdout(), init.io, &out_buf);
try stdout.interface.print("{d}\n", .{lines});
try stdout.interface.flush();
}Run it on the Zig online compiler, or pick Zig in the editor above.
Julia
lines = readlines(stdin)
println(length(lines))Run it on the Julia online compiler, or pick Julia in the editor above.
Lua
local lines = {}
for line in io.lines() do
lines[#lines + 1] = line
end
print(#lines)Run it on the Lua online compiler, or pick Lua in the editor above.
Swift
var lines: [String] = []
while let line = readLine() {
lines.append(line)
}
print(lines.count)Run it on the Swift online compiler, or pick Swift in the editor above.
Scala
import scala.io.Source
@main def run(): Unit =
val lines = Source.stdin.getLines().toList
println(lines.size)Run it on the Scala online compiler, or pick Scala in the editor above.
Clojure
(require '[clojure.string :as str])
(let [lines (str/split-lines (slurp *in*))]
(println (count lines)))Run it on the Clojure online compiler, or pick Clojure in the editor above.
Nim
var lines: seq[string]
for line in stdin.lines:
lines.add line
echo lines.lenRun it on the Nim online compiler, or pick Nim in the editor above.
Crystal
lines = STDIN.each_line.to_a
puts lines.sizeRun it on the Crystal online compiler, or pick Crystal in the editor above.
Dart
import 'dart:io';
void main() {
final lines = <String>[];
for (var line = stdin.readLineSync(); line != null; line = stdin.readLineSync()) {
lines.add(line);
}
print(lines.length);
}Run it on the Dart online compiler, or pick Dart in the editor above.
PHP
<?php
$lines = file('php://stdin', FILE_IGNORE_NEW_LINES);
echo count($lines), "\n";Run it on the PHP online compiler, or pick PHP in the editor above.
Perl
my @lines = <STDIN>;
chomp @lines;
print scalar(@lines), "\n";Run it on the Perl online compiler, or pick Perl in the editor above.
Bash
mapfile -t lines
echo "${#lines[@]}"Run it on the Bash online compiler, or pick Bash in the editor above.
Common Lisp
(defun read-lines (&optional (stream *standard-input*))
(loop for line = (read-line stream nil)
while line
collect line))
(format t "~a~%" (length (read-lines)))Run it on the Common Lisp online compiler, or pick Common Lisp in the editor above.
Erlang
main(_) ->
Lines = read_lines([]),
io:format("~p~n", [length(Lines)]).
read_lines(Acc) ->
case io:get_line("") of
eof -> lists:reverse(Acc);
Line -> read_lines([string:trim(Line, trailing, "\n") | Acc])
end.Run it on the Erlang online compiler, or pick Erlang in the editor above.
R
lines <- readLines(file("stdin"), warn = FALSE)
cat(length(lines), "\n", sep = "")Run it on the R online compiler, or pick R in the editor above.
A different language every day
A popular way to do Advent of Code is to use a new language each day. Installing a compiler for each one takes longer than many puzzles, and some, like COBOL, Prolog or Fortran, are awkward to set up. In the editor above they are all one menu away, and the editor keeps what you wrote in each language when you switch between them. The languages page lists every language with its version and compiler id.
Limits worth knowing
- 30 seconds a run. A program still running then is stopped with exit code 124. If part 2 times out, the puzzle usually wants a better algorithm, not more time.
- 512 MB of memory and 2 CPUs for each run.
- No network. The program can't download your input, so paste it in. Nothing persists between runs: each starts in a fresh sandbox.
- Output up to 64 KB for each of stdout and stderr, plenty for printing a grid while you debug.
Run your solutions from a script
If you keep each day's solution in a file, you can send it and your input to the API and read the output in your
terminal. Make a free API key (sign up, no card), put it in ONLINECOMPILER_API_KEY, and run:
# Run one day's solution against your input from a terminal
jq -n --rawfile code day01.py --rawfile input input.txt \
'{compiler: "python-3.14", code: $code, input: $input}' |
curl -s https://api.onlinecompiler.io/api/run-code-sync/ \
-H "Authorization: $ONLINECOMPILER_API_KEY" \
-H "Content-Type: application/json" \
-d @- | jq -r '.output, .error'Change python-3.14 to any compiler id from the compilers list. Through
the API an input can be up to 5 MB, and the free plan includes 100,000 runs a month. The API docs also cover running a solution against several test cases at once with /api/run-tests/, useful for checking every example of a puzzle in one call.
About your puzzle input
Advent of Code asks people not to publish their puzzle inputs. Runs in this editor are not public: each is kept in execution history with its output, as the security page describes, and is not shown to anyone else. Keep your input out of public repositories and posts, as the puzzle's creator asks.
Frequently asked questions
Can I solve Advent of Code online without installing anything?
Yes. Paste your puzzle input into the Input tab of the editor on this page, write your solution and press Run. The editor runs 46 languages on our servers, so nothing is installed on your computer, and it needs no account.
How do I read the puzzle input?
From standard input. Whatever is in the Input tab is sent to your program as stdin, so read all of it and split it into lines. This page has a reader for 30 languages that does exactly that.
Can my program download the input from adventofcode.com?
No. Programs run in a sandbox with no network access, so they cannot fetch the input or anything else. Open the day's input in your browser, copy it, and paste it into the Input tab.
Is there a time limit?
Each run can take up to 30 seconds and use 512 MB of memory. A program still running at 30 seconds is stopped with exit code 124, which usually means the approach needs a faster algorithm rather than more time.
Which languages can I use?
All 46 in the editor's language menu, including Python, JavaScript, TypeScript, Go, Rust, C++, Java, Kotlin, C#, Haskell, OCaml, Elixir, Zig, Julia, Nim, Crystal, Clojure, Common Lisp, Erlang, Prolog, Fortran and COBOL. Each has its own compiler page with its version and libraries.
Is this page part of Advent of Code?
No. Advent of Code is run by its creator at adventofcode.com, and OnlineCompiler.io is not affiliated with it. This page is a place to run your own solutions; the puzzles and your input stay on Advent of Code.