diff --git a/.gitignore b/.gitignore index 0f52a43..8b27ff4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ * !.gitignore !*.hs +!*.c !*.js !*.txt \ No newline at end of file diff --git a/23.c b/23.c new file mode 100644 index 0000000..c398dd6 --- /dev/null +++ b/23.c @@ -0,0 +1,37 @@ +#include +#include + +// unoptimized translation of input +unsigned int naiveCount() { + unsigned short h = 0; + for (long b = 108100; b < 125100; b += 17) { + bool f = true; + for (long d = 2; d < b; d++) { + for (long e = 2; e < b; e++) { + if (d * e == b) { + f = false; + } + } + } + h += !f + } + return h; +} + +unsigned int count() { + unsigned short h = 0; + for (long b = 108100; b < 125100; b += 17) { + bool f = true; + for (unsigned short d = 2; f && d * d <= b; d++) { + if (b % d == 0) { + f = false; + } + } + h += !f; + } + return h; +} + +int main() { + printf("%hu\n", count()); +} \ No newline at end of file diff --git a/23.hs b/23.hs index f4ae820..1900eb2 100644 --- a/23.hs +++ b/23.hs @@ -4,7 +4,6 @@ import Data.Char (ord) import Data.Sequence (Seq, fromList, index) import Data.Vector.Unboxed (Vector, (!), (//)) import qualified Data.Vector.Unboxed as V (replicate) -import Debug.Trace type Registers = Vector Int type Instruction = State -> State @@ -29,10 +28,10 @@ getValue r v = case v of c -> r ! (getIndex c) app :: Function -> Value -> Value -> Instruction -app (Function op fn) index value state@(State reg pos cnt) = - let i = getIndex index +app (Function op fn) x y (State reg pos cnt) = + let i = getIndex x in State { - registers = reg // [(i, reg ! i `fn` getValue reg value)], + registers = reg // [(i, reg ! i `fn` getValue reg y)], position = pos + 1, countMul = cnt + fromEnum (op == Mul) } @@ -56,12 +55,11 @@ parseLine str = Nothing -> Register $ head s runInstructions :: Seq Instruction -> State -> Int -runInstructions instructions state@(State reg pos cnt) = +runInstructions instructions state@(State _ pos cnt) = if pos >= length instructions then cnt else let !nextState = instructions `index` pos $ state in runInstructions instructions nextState main :: IO () main = do instructions <- fromList . map parseLine . lines <$> readFile "23.txt" - print $ runInstructions instructions (State (V.replicate 8 0) 0 0) - print $ runInstructions instructions (State (V.replicate 8 0 // [(0, 1)]) 0 0) \ No newline at end of file + print $ runInstructions instructions (State (V.replicate 8 0) 0 0) \ No newline at end of file diff --git a/README.md b/README.md index c218fb7..a7672c2 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ These are the runtimes of only one trial but the variances are fairly small and | 20 | 0.168 | | 21 | 4.013 | | 22 | 12.867 | 7.880 +| 23 | 0.274 | Problems that should be optimized further: 15