Skip to content

Instantly share code, notes, and snippets.

View royvanrijn's full-sized avatar
🙌

Roy van Rijn royvanrijn

🙌
View GitHub Profile
@royvanrijn
royvanrijn / gist:a865294955b8e2d5f9ac8a8a674fceed
Created April 30, 2024 12:06
Parsing a number using SWAR techniques in Java (branchless)
/**
* Takes a string with digits (max length 8) from "-9999999" to "99999999" and returns the integer value.
* @param input
* @return
*/
private static int parseInteger(final String input) {
byte[] bytes = input.getBytes();
// Read String as long:
long bits = UNSAFE.getLong(bytes, Unsafe.ARRAY_BYTE_BASE_OFFSET);
/*
* Copyright 2023 The original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
package com.royvanrijn.advent.aoc2023;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
package com.royvanrijn.advent.aoc2023;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
package com.royvanrijn.advent.aoc2023;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
package com.royvanrijn.advent.aoc2023;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
package com.royvanrijn.advent.aoc2023;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Day8 {
package com.royvanrijn.advent.aoc2023;
import java.math.BigInteger;
public class Day6 {
public static void main(String[] args) throws Exception {
new Day6().day6(); // No static context.
}
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Day5 {
public static void main(String[] args) throws Exception {
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
public class Day25 {
public static void main(String[] args) throws Exception {
new Day25().calculate();
}
public void calculate() throws Exception {